Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting to home page if user is already logged in [duplicate]

Possible Duplicate:
How to redirect to another page when already authenticated user accesses login page

If a logged in user clicks on login.jsp page then he should be automatically redirected to his home.jsp. Before authentication user goes to login.jsp and after successfully authenticated by a servlet (doPost) he is redirected to home.jsp, but when again the same user clicks on login page then he should be automatically redirected to home.jsp instead of logging again. How can I do it in JSP/Servlet? I am setting session from that servlet after successfully authenticated.

Before login, user clicks on login.jsp and goes to doPost() method of servlet and goes to home.jsp. After logged in succesfully, if user clicks again on login.jsp then instead of going to login.jsp, control should go directly to home.jsp. How can I do it?

If user is signed out then only after clicking login.jsp control will go to login.jsp and then doPost() of servlet and finally home.jsp will come.

like image 834
sujit Avatar asked Dec 25 '11 11:12

sujit


2 Answers

try this code in your login.jsp

if(session.getAttribute("authenticated")!=null && session.getAttribute("authenticated").equals(true))
{
   response.sendRedirect("home.jsp");
}
like image 50
Kashif Khan Avatar answered Oct 23 '22 06:10

Kashif Khan


Make the login page/action check if the user is logged in, and redirect to the home page if logged in:

if (alreadyLoggedIn) {
    response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/home"));
    return;
}

But you should probably not have a link to the login page in the first place if the user is already logged in.

like image 37
JB Nizet Avatar answered Oct 23 '22 05:10

JB Nizet