Hi all,
I am implementing Ajax on a simple
jsp page.Code snippet of the jsp is as shown below:
<form name="myForm">
Enter name: <input type="text" name="username" onblur="ajaxFunction()"/>
<span id="message"></span>
Enter location:<input type="text" name="location" onblur="ajaxFunction1()"/>
<span id="locationdetails"></span>
</form>
I have also included the JS functions for creating theXMLHTTRequest object. The code snippets of "ajaxFunction()" and "ajaxFunction1()" are as follows:
function ajaxFunction()
{
var params = "username="+document.myForm.username.value;
alert("username:"+params);
xmlHttp.open("POST","AjaxServlet",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);
xmlHttp.onreadystate = function()
{
if(xmlHttp.readyState == 4)
{
alert("Into ready state");
if(xmlHttp.status == 200)
{
alert("xmlHttp.responseText");
document.getElementById("message").innerHTML = xmlHttp.responseText;
}
}
}
}
ajaxFunction1()
function ajaxFunction1()
{
var params = "location="+document.myForm.location.value;
alert("location:"+params);
xmlHttp.open("POST","AjaxServlet",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);
xmlHttp.onreadystate = function()
{
alert("Into ready state");
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
alert("xmlHttp.responseText");
document.getElementById("locationdetails").innerHTML = xmlHttp.responseText;
}
}
}
}
In the
servlet's doPost method I have included a method which returns the greeting message and location details.Code snippet is as follows:
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String username = null;
String location = null;
response.setContentType("text/html");
username = request.getParameter("username");
location = request.getParameter("location");
System.out.println("USERNAME:"+username);
System.out.println("LOCATION:"+location);
displaymessage(response,username);
displaylocationdetails(response,location);
}
private void displaymessage(HttpServletResponse response ,
String username)throws IOException
{
String message = null;
PrintWriter out = response.getWriter();
if(username!=null)
{
System.out.println("Into display message if");
message = "Welcome..."+username;
}
else
{
System.out.println("Into display message else");
message = "Please enter the username";
}
out.println(message);
out.close();
}
private void displaylocationdetails(HttpServletResponse response,
String location)throws IOException{
PrintWriter out = response.getWriter();
String locationdetails = null;
if(location!=null)
{
System.out.println("Into location display message if");
if(location.equalsIgnoreCase("TCO"))
{
locationdetails = "Techno Campus";
}
else if(location.equalsIgnoreCase("GMR"))
{
locationdetails = "GMR office";
}
else if(location.equalsIgnoreCase("SRI"))
{
locationdetails ="Siruseri";
}
else
{
locationdetails = "New location";
}
}
else
{
System.out.println("Into location display message else");
locationdetails = "Please enter the location details";
}
out.println(locationdetails);
out.close();
}
If you go through the code you will get to know that when the name is entered a wecome message shud be displayed or "Please enter username" message when its left blank.
But when I run this application it does not display any message if we leave the field blank or enter any data same thing holds good for locationdetails also.
Looking forward for your reply.
Regards
Janu