title: prevent back to login page after user is logged in jsp servlet
body: <p>i am using jsp servlet to make a web application, I want to prevent the user to show login page, if he already logged in, I make a filter that check that, but it still show login page even user has a valid session, Here is the code in the Filter.</p>
  
  <pre><code>  HttpSession session = httpreq.getSession(false);
  
      if(session == null){
          System.out.println("not logged, redirect ");
            httpres.sendRedirect("../Login.jsp");
  
      }
      else{
              System.out.println("could be logged");
             String logged = (String) session.getAttribute("Login");
             if(logged != null){
                     System.out.println(" logged  "+logged);
                 if (!logged.equals("ok")) { // user is not logged
                      System.out.println("not logged, redirect ");
                      httpres.sendRedirect("../Login.jsp");
                      return;
                  } else {  // if user has a session redirect his to the page he was opened
                         System.out.println("redirect to the  same page");
                      chain.doFilter(request, response);
                      System.out.println("redirect to the  same page");
                      httpres.setCharacterEncoding("UTF-8");
                      httpres.sendRedirect(httpreq.getRequestURI());
                  }
             }else
             {
                   System.out.println("not logged, redirect login ");
                      httpres.sendRedirect("../Login.jsp");
                      return;
             }
  
      }
  </code></pre>
  
  <p>I make the session only on folders that located outside the WEB-INF folder.</p>
  
  <p><strong>Edit</strong> : Here's the code to check the validity of user and add attributes to session</p>
  
  <pre><code>  isVaild = StudentManagement.isValidUser(connection, studentUserName, password);
                 //     I have more than one roles in the system..
              }
              if (isVaild) {
  
                  System.out.println("create session");
                  HttpSession session = request.getSession();
                  session.setAttribute("Login", "ok");
                  session.setAttribute("userName", userName);
                  session.setAttribute("role", role);
                  if (role == UserRole.STUDENT) {  //student role
                      url = "/ParentManagementServlet?pageName=StudentActivationPage";
  
                      forward(request, response, url);
                  } else if (role == UserRole.ADMIN) {  //admin role
                      url = "/Admin/MainPage.jsp";
                      forward(request, response, url);
                  }
  </code></pre>
  
  <p><strong>Edit 2:</strong>
  here's the URL mapping in web.xml file</p>
  
  <pre><code> &lt;filter-mapping&gt;
      &lt;filter-name&gt;AuthenticationFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/Admin/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>since Admin is the folder that locates outside the WEB-INF folder.</p>