Content of index.html
<html>
<head>
<title>Userdetails</title>
<script type="text/javascript">
var ajaxRequest = null; // The variable that makes Ajax possible!
function validate() {
alert("Trying to validate username");
alert("Trying to check whether Ajax can run")
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
alert("try---1");
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
alert("try---2");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
alert("try---3");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
var target = document.getElementById("username");
var url = "ValidateServ?username=" + target.value;
alert(url);
ajaxRequest.open("GET", url, true);
ajaxRequest.send(null);
ajaxRequest.onreadystatechange = processRequest;
}
function processRequest() {
if (ajaxRequest.readyState == 4) {
alert(ajaxRequest.responseText);
var body = document.getElementsByTagName("body")[0];
if (ajaxRequest.status == 200) {
if (ajaxRequest.responseText == true)
var textNode = document
.createTextNode("This username is validated");
else
var textNode = document
.createTextNode("This username is not validated");
} else {
alert("Error");
}
domUpdate("%%%%%%");
body.appendChild(textNode);
}
}
function domUpdate(message) {
alert(message)
}
</script>
</head>
<body>
<form name="username">
<input type="text" id="username" name="username" onblur="validate();">
</form>
</body>
</html>
Content of ValidateServ.java
package com.t4b.ajax.test;
import java.io.IOException;
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 ValidateServ
*/
@WebServlet("/ValidateServ")
public class ValidateServ extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ValidateServ() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("username");
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
if (name.equals("anindita"))
response.getWriter().write("true");
else
response.getWriter().write("false");
System.out.println("*****************************************");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.