I have made this web application in Java using Netbeans as the IDE.
I was mainly working over my AJAX code, but when i hit and trial over the code using System.out.println(..); The code was not able to go through the Servlet.I have attached a zip file which contains the whole code.
The application was mainly to check the validity whether the value entered in the textfield is a number or not.I do want to know where i went wrong.
Thank you

The servlet
package Serv; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ValidationServlet */ @WebServlet(description = "Validation through ajax", urlPatterns = { "/ValidationServlet" }) public class ValidationServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ValidationServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); boolean passed = validateInteger(request.getParameter("number")); response.setContentType("text/xml"); response.setHeader("cache-Control","no-cache"); System.out.print("YES"); String message = "You have entered an invalid number."; if(passed){ message = "You have entered a valid number."; } out.println("<response>"); out.println("<passed>" + Boolean.toString(passed) + "</passed>"); out.println("<message>" + message + "</message>"); out.println("</response>"); out.close(); } private boolean validateInteger(String number){ boolean isValid = true; if(number!=null){ try{ Integer.parseInt(number); } catch(NumberFormatException e){ isValid = false; } } isValid = false; return isValid; } }
The Javascript code
var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } function validate(){ createXMLHttpRequest(); var number = document.getElementById("number"); var url = "ValidationServlet?number=" + escape(number.value); xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = callback; xmlHttp.send(null); } function callback(){ if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ var mes = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data; var val = xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data; setMessage(mes,val); } } } function setMessage(){ var messageArea = document.getElementById("numMessage"); var fontColor = "red"; if(isValid =="true"){ fontColor = "green"; } messageArea.innerHTML = "<font color ="+fontColor +">" + message + "</font>"; }
The HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type ="text/javascript" src="validationScript.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Ajax Validation Example</h1> Number:<input type="text" size="10" id="number" onchange= "validate();"/> <div id = 'numMessage'></div> </body> </html>
The XML code
<?xml version="1.0" encoding="UTF-8"?> <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:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Valid</display-name> <servlet> <servlet-name>ValidationServlet</servlet-name> <servlet-class> ValidationServlet</servlet-class> </servlet> <welcome-file-list> <welcome-file>validation.html</welcome-file> </welcome-file-list> <servlet-mapping> <servlet-name>ValidationServlet</servlet-name> <url-pattern>/ValidationServlet</url-pattern> </servlet-mapping> </web-app>