I’m new to servlets and jsp, but i can write simple ones.
currently on all pages of my webapp, there is a login form, added with jsp:include.
i want to create login mechanism, so that after a user is validated, i dont get redirected( simple servlet mechanism ), but login form disappears(like in most normal websites).
all the examples i find, they teach me how to validate+redirect. How can i do what i specified instead? if its complicated, then at least general direction, right now i don’t even really know what to google for exactly.
Thanks
You can use a session variable to check if any user is currently logged in. If a user is logged in, then don’t display the login form, simply display a logout button maybe. And if a user is not logged in, display the login form.
A session variable can be set like this :
HttpSession ses=request.getSession();ses.setAttribute("name",user);
A session variable can be referred like this :
HttpSession ses=request.getSession(false);
if(ses.getAttribute("name")== null)
//set flag to default value and check flag to know no one is currently logged in
else
//set flag to some other value to show someone is currently logged in
EDIT
Use the following codes in your validation servlet and in your logout servlet(you would need one). For other things, you can use simple if-else conditions.
HttpSession ses=request.getSession();ses.setAttribute("name",user);
And:
HttpSession ses=request.getSession();
ses.invalidate();