views:

289

answers:

1

Hello

If registered user jumps to login.jsp I wish to redirect him to youAreLoggedIn.jsp. Can this be made with <navigation-rule>? I know how to do this in PHP but not in JSP. When user visits login.jsp it must check if user is logged in this I know how to check. But how to trigger this action? Should I make a new servlet or what?

Thanks!

+2  A: 

A common practice is to use a Filter for this. Just implement javax.servlet.Filter, define it in web.xml, map it on an url-pattern of /login.jsp and write something like following in doFilter() method:

if (((HttpServletRequest) request).getSession().getAttribute("user") != null) {
    // User is logged in, redirect to desired page.
    ((HttpServletResponse) response).sendRedirect("youAreLoggedIn.jsp");
} else {
    // Do nothing, continue request.
    chain.doFilter(request, response);
}

Simple as that. It of course assumes that the logged-in User is been put in the session scope as per the normal practices.

That said, <navigation-rule> is JSF specific, but you didn't state anything about JSF in your question nor the tags. Aren't you confusing things up?

BalusC
Works perfectly! Thank you! `<navigation-rule>` I use for main menu and for errors. So I thought it would work with it.
Zlatoroh
In other words, you're actually using JSF? It's however strange that you keep mentioning JSP all the time. Please understand what JSP is for and what JSF is for and take this into account in future questions. There may be different ways to solve a particular problem depending on the fact whether you're using JSF or not.
BalusC