Monday 15 March 2010

javascript - How to call a servlet from a jQuery's $.ajax() function -


I am trying to call servlet from jQuery's .jax () function.

At this time I do not think I call the servlet or passing it to the parameter, although a lot of Googling has not helped. any idea?

This is my html:

  & lt; Html & gt; & Lt; Top & gt; & Lt; Meta http-equiv = "content-type" content = "text / html; charset = ISO-885 9-1" & gt; & Lt; Script type = "text / javascript" src = "jquery.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Function Login () {$ ("# Loading") Hide (); Var email = document.nameForm.email.value; $ .ajax ({type: "GET", url: "processform", data: "email =" + email, success: work (results) {warning (result);}}); } & Lt; / Script & gt; & Lt; Title & gt; My AJAX & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P & gt; This time it's going to work & lt; / P & gt; & Lt; Form name = "nameForm" id = "nameForm" method = "post" action = "javascript: login ()" & gt;  

Email loading

  & lt; / Body & gt; & Lt; / Html & gt;  

and my web.xml

  & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: Schema Location =" http://java.sun.com/xml/ns/javaee http: //java.sun Com. / Xml / ns / javaee / web-app_2_5.xsd "id =" WebApp_ID "version =" 2.5 "& gt; & Lt; Display-name & gt; Ajaxtry & lt; / Display-name & gt; & Lt; Welcome-file-list & gt; & Lt; Welcome-file & gt; Index.html & lt; / Welcome-file & gt; & Lt; Welcome-file & gt; Index.htm & lt; / Welcome-file & gt; & Lt; Welcome-file & gt; Index.jsp & lt; / Welcome-file & gt; & Lt; Welcome-file & gt; Default.html & lt; / Welcome-file & gt; & Lt; Welcome-file & gt; Default.htm & lt; / Welcome-file & gt; & Lt; Welcome-file & gt; Default.jsp & lt; / Welcome-file & gt; & Lt; / Welcome-file-list & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; ProcessForm & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Com.ajaxtry.web.ProcesFormServlet & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; ProcessForm & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / ProcessForm & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; / Web application & gt;  

Servlet is currently just a template:

  package com.ajaxtry.web; // Import Public Segment ProcessFormServlet {Public Zero doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {response.setContentType ("text / html"); PrintWriter out = response.getWriter (); Println (request.getParameter ("email")); }}  

Here are some problems:

Again Calling .out.println, which is currently sending output to the standard - not the browser. Just try to replace "system.out.println" in "out.println"

It seems that you have defined doPost () in your topmost code, but using your Javascript "GET" method Repeat DoGost (), or define both.

That is being said, you probably should not bother with javascript until you are actually getting servlet work, it is simple to keep it. You should be able to check it by loading / process email = testing in your browser and see some output. Once you receive it, you can start worrying about the front-end code.

Hope that helps you get started.


No comments:

Post a Comment