I am trying to do some AJAX stuff.
index.jsp
<html&rt;
<head&rt;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&rt;
<style type="text/css"&rt;
.bp_invalid {
color:white;
background:red;
}
.bp_valid {
color:green;
}
</style&rt;
<script type="text/javascript"&rt;
function AJAXInteraction(url, callback) {
var req = init();
req.onreadystatechange = processRequest;
function init() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function processRequest () {
if (req.readyState == 4) {
if (req.status == 200) {
if (callback) callback(req.responseXML);
}
}
}
this.doGet = function() {
req.open("GET", url, true);
req.send(null);
}
}
function validateUserId() {
var target = document.getElementById("userid");
var url = "validate?id=" + encodeURIComponent(target.value);
var target = document.getElementById("userid");
var ajax = new AJAXInteraction(url, validateCallback);
ajax.doGet();
}
function validateCallback(responseXML) {
var msg = responseXML.getElementsByTagName("valid")[0].firstChild.nodeValue;
if (msg == "false"){
var mdiv = document.getElementById("userIdMessage");
// set the style on the div to invalid
mdiv.className = "bp_invalid";
mdiv.innerHTML = "Invalid User Id";
var submitBtn = document.getElementById("submit_btn");
submitBtn.disabled = true;
} else {
var mdiv = document.getElementById("userIdMessage");
// set the style on the div to valid
mdiv.className = "bp_valid";
mdiv.innerHTML = "Valid User Id";
var submitBtn = document.getElementById("submit_btn");
submitBtn.disabled = false;
}
}
</script&rt;
<title&rt;Form Data Validation using AJAX</title&rt;
</head&rt;
<body onkload="disableSubmitBtn()"&rt;
<h1&rt;Form Data Validation using AJAX</h1&rt;
<form name="updateAccount" action="validate" method="post"&rt;
<input type="hidden" name="action" value="create"/&rt;
<table border="0" cellpadding="5" cellspacing="0"&rt;
<tr&rt;
<td&rt;<b&rt;User Id:</b&rt;</td&rt;
<td&rt;
<input type="text"
size="20"
id="userid"
name="id"
autocomplete="off"
onkkeyup="validateUserId()"&rt;
</td&rt;
<td&rt;
<div id="userIdMessage"&rt;</div&rt;
</td&rt;
</tr&rt;
<tr&rt;
<td align="right" colspan="2"&rt;
<input id="submit_btn" type="Submit" value="Create Account"&rt;
</td&rt;
<td&rt;</td&rt;
</tr&rt;
</table&rt;
</form&rt;
</body&rt;
</html&rt;
ValidationServlet.java
package com.form;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ValidationServlet extends HttpServlet {
private ServletContext context;
private HashMap accounts = new HashMap();
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.context = config.getServletContext();
accounts.put("greg","account data");
accounts.put("duke","account data");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
String targetId = request.getParameter("id");
if ((targetId != null) && !accounts.containsKey(targetId.trim())) {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid&rt;true</valid&rt;");
} else {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid&rt;false</valid&rt;");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String targetId = request.getParameter("id");
if ((targetId != null) && !accounts.containsKey(targetId.trim())) {
accounts.put(targetId.trim(), "account data");
request.setAttribute("targetId", targetId);
context.getRequestDispatcher("/success.jsp").forward(request, response);
} else {
context.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
}
I give
http://localhost:7001/TestProject/jsp/ and the page loads up fine with the form field. I fill the form and click on "Create Account" and I land on
http://localhost:7001/TestProject/jsp/validate with the following error - "10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.
If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.
"
Please provide pointers.
Thanks,
Varadh