Hey users we all knows benefits of jquery and this is used by most of developers.Here now this tutorial is related
to show jquery magic with jsp (Java Server Pages). Actually i'm posting this article because it took a long time when when i was finishing my project.
to show jquery magic with jsp (Java Server Pages). Actually i'm posting this article because it took a long time when when i was finishing my project.
So come to main point what things we required ?
- JSP page from where request goes to server to check user authintication.
- Servlet where cheking is been done.
- A jquery code to check the response and perform its task to validate user.
Front End Code (JSP):
Jquery Code:
You can put this code either on seprate file and call it in head section or put in the same jsp file with its script tag.
$(document).ready(function(){
//global vars
var userName = $("#username"); //user name field
var userPass = $("#password"); //password field
//function to check name and comment field
function checkCommentsForm()
{
if(userName.attr("value") && userPass.attr("value"))
return true;
else
return false;
}
//When form submitted
$("#formL").submit(function(){
var url = "adminhome.jsp";
if(checkCommentsForm())
{
$.ajax({
type: "post"
, url: "login_check"
,data: "username="+userName.val()+"&password="+userPass.val(),
success: function(msg) {if(msg=="Welcome")
{
$(location).attr('href',url);
}
else if(msg=="Wrong Username Or Password")
{
$('#targetDiv').hide();$("#targetDiv").html ("
" + msg + "
").fadeIn("slow");
}
}
});
}
else
{
alert("Please fill UserName & Password!");
}
return false;
});
});
Servlet Code:
i had used EJB technique and method is written in session bean in which query is written. This method is called from servlet which is shown below.
List login =sessionbean.login(name,password);
int t=login.size();
if(t==0)
{
response.getWriter().write("Wrong Username Or Password");
}
else
{
HttpSession session=request.getSession(true);
session.setAttribute("currentSessionUser", name);
response.getWriter().write("Welcome");
}
Comments
Post a Comment
your Comment is sent for moderation, Thankyou