How to redirect the user to the home page if the user is already registered. I use the Filter class for the login page, but it does not work correctly. my code is:
@WebFilter(filterName = "loginFilter", urlPatterns ={"/login.xhtml"}) public class LoginFilter implements Filter{ private FilterConfig filterconfig; @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterconfig = filterconfig; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest =(HttpServletRequest) request; HttpServletResponse httpresponse =(HttpServletResponse) response; try{ if(httprequest.getUserPrincipal() != null){ System.out.printf("User authenticated with " + httprequest.getRemoteUser() + " username conected."); httprequest.getRequestDispatcher("/home.xhtml").forward(request, response); } else{ chain.doFilter(request, response); } }catch(Exception){
Here is my problem: the user registered on the Internet and made navigation and much more, and then returned to the login page using the return button of the browser without logging out and again enter the username, and then click the login button. Then it throws an IndexOutOfBoundsException exception. I just need to check when the user goes to the login page using the link or link back, and redirect to the home page. any suggestion?
source share