
hi all i want to create a login page in jsp which should be validated using ajax. please help me dong that with code...

Here is a jsp example that accepts username and password from the user and check whether the user is valid or not by using ajax.
1)ajaxLogin.jsp
<html>
<head>
<script language="javascript">
function postRequest(strURL){
var xmlHttp;
if(window.XMLHttpRequest){
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('POST', strURL, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
updatepage(xmlHttp.responseText);
}
}
xmlHttp.send(strURL);
}
function updatepage(str){
if(str=="yes"){
alert("Welcome User");
}else{
alert("Invalid Login! Please try again!");
}
}
function call_login(){
var username = window.document.f1.username.value;
var password = window.document.f1.password.value;
var url = "login.php?username=" + username + "&password=" +password ;
postRequest(url);
}
</script>
</head>
<body>
<Center>
<form name="f1" onsubmit="return call_login();">
<table border="0" bgcolor="#CCCCFF" cellspacing="1" cellpadding="3" width="287">
<tr>
<td align="left" colspan="2" width="275"><b><font size="5" color="#000080">Login</font></b></td>
</tr>
<tr>
<td align="right" width="81"><b><font color="#000080">User
Name:</font></b></td>
<td width="184"><input type="text" name="username" id="user" size="20" value="" /></td>
</tr>
<tr>
<td align="right" width="81"><b><font color="#000080">Password:</font></b></td>
<td width="184"><input type="password" name="password" size="20" value="" /></td>
</tr>
<tr>
<td colspan="2" align="center" width="275"><input type="button" name="a1" value="Login" onclick="call_login()"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
2)check.jsp:
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username=="admin" && password=="admin"){
out.println("Valid!");
}else{
out.println("Not Valid!");
}
?>
For more information, visit the following link:

hi,thank you... i get invalid user name and password though i give username and password properly..

does it pass parameters to the server i.e. check.jsp?
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.