I am using XMLHttpRequest to create a simple form submitand get session variable from the servlet. But nothing seems to work. Can somebody point out where am I going wrong?
Here is the Form
<form method="post" target="_self" action="/temp/Welcome.html">
<table>
<tr>
<td>Email</td>
<td><input type="text" id="email" name="email" value="a@a.com"></td>
</tr>
<tr>
<td>Project ID</td>
<td><input type="text" id="projectid" name="projectid" value="1111"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="button" id="submitbutton" value="Submit" onclick="submitLogin()"></td>
</tr>
</table>
</form>
here is the submitLogin function
function submitLogin()
{
var url_action="/temp/Login";
var client;
var dataString;
if (client.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
client=new XMLHttpRequest();
} else { // IE6, IE5
client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function(){
alert(client.responseText);
if(client.readyState==4&&client.status==200)
{
alert(client.responseText);
}
else
alert("Error: return status code "+client.status+" "+client.statusText);
};
dataString="email="+document.getElementById("email").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(dataString);
}
and my post method in Login.java servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
paramMap=request.getParameterMap();
if (paramMap == null)
throw new ServletException(
"getParameterMap returned null in: " + getClass().getName());
iterator=paramMap.entrySet().iterator();
//System.out.println(paramMap.size());
String str="";
while(iterator.hasNext())
{
Map.Entry me=(Map.Entry)iterator.next();
String[] arr=(String[])me.getValue();
emailId=arr[0];
//System.out.println(me.getKey()+" > "+emailId);
}
rand=new Random();
randomInt=rand.nextInt(1000000);
emailId=randomInt+emailId;
System.out.println(emailId);
out.println(emailId);
/*creates a new session if a session does not exist already*/
session=request.getSession();
session.setAttribute("uid", emailId);
out.close();
}
None of the alerts and the System.out seems to show any response. please guide me. Thanks
Note: in IE 8 it shows xmlhttprequest is null or not an object, login.js at Line no: 9
Looks like you have a simple typo in your Java Script
You are declaring some variable called client and asking it for it’s XMLHttpRequest member. You probably meant to use window.XMLHttpRequest.