Redirect with login.jsp if it is already registered

If the registered user goes to login.jsp, I want to redirect him to youAreLoggedIn.jsp. Can this be done with <navigation-rule>? I know how to do this in PHP, but not in JSP. When a user visits login.jsp, he needs to check if the user is registered in this; I know how to check. But how to cause this action? Should I make a new servlet or what?

Thank!

+3
source share
1 answer

Common practice is to use Filterfor this. Just make it javax.servlet.Filter, define it in web.xml, map it to url-patternfrom, /login.jspand write something like the following in doFilter():

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);
}

Simply. He, of course, suggests that the registered Userwas placed in the session area in accordance with normal practices.

However, it <navigation-rule>is specific to JSF, but you did not mention anything about JSF in your question and tags. Aren't you bewildering?

+5
source

Source: https://habr.com/ru/post/1734061/


All Articles