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?