title: Login servlet & jsp with boolean admin in User class
body: <p>The login doesn't work when i get access with all privileges and if i do login with a normal user it redirect me to /admin instead of /app, i tried to solve the problem but i don't know what am i doing wrong. 
  I want to create an unique admin where the username: admin and password: admin,
  I have an User class with boolean attribute called admin.
  Here the code:</p>
  
  <p>WebFilter for Users:</p>
  
  <pre><code>@WebFilter("/app/*")
  public class FiltroRegistro implements Filter {
  
  
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
              throws IOException, ServletException {
          HttpServletRequest req = (HttpServletRequest) request;
          HttpServletResponse res = (HttpServletResponse) response;
          HttpSession session = req.getSession(false);
          String loginURI = req.getContextPath() + "/login.jsp";
  
  
          if (session != null &amp;&amp; session.getAttribute("usuario") != null) {
  
              chain.doFilter(req, res);
  
          } else {
              res.sendRedirect(loginURI);
          }
  
      }
  </code></pre>
  
  <p>WebFilter for admin:</p>
  
  <pre><code>@WebFilter("/admin/*") public class FiltroAdministrador implements
     Filter {
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
              throws IOException, ServletException {
  
          HttpServletRequest req = (HttpServletRequest) request;
          HttpServletResponse res = (HttpServletResponse) response;
          HttpSession session = req.getSession(false);
          String loginURI = req.getContextPath() + "/login.jsp";
  
          if (session != null &amp;&amp; session.getAttribute("administrador") != null) {
  
                  chain.doFilter(req, res);
  
          } else {
              res.sendRedirect(loginURI);
          }
  
      }
  </code></pre>
  
  <p>Login servlet:</p>
  
  <pre><code>@WebServlet("/FiltroServlet")
  public class FiltroRegistroServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          // TODO Auto-generated method stub
          request.setCharacterEncoding("UTF-8");
  
      String nombreUsuario = request.getParameter("nombreUsuario");
      String contrasenia = request.getParameter("contrasenia");
  
      GestionUsuarios gestionUsuarios = new GestionUsuarios();
  
      Usuario admin = gestionUsuarios.registrarAdministrador(nombreUsuario, contrasenia);
      //Usuario admin = gestionUsuarios.registrarAdministrador(nombreUsuario, contrasenia);
      Usuario usuario = gestionUsuarios.registrarUsuario(nombreUsuario, contrasenia);
  
      if (admin != null) {
          HttpSession session = request.getSession(true);
          session.setAttribute("administrador", admin);
  
          response.sendRedirect("admin/TablaEventosAdmin");
  
  
      } else if (usuario != null) {
          HttpSession session = request.getSession(true);
          session.setAttribute("usuario", usuario);
          response.sendRedirect("app/TablaEventosServlet");
          response.sendRedirect("loginError.jsp");
      } else {
          response.sendRedirect("login.jsp");
      }
  
  }
  </code></pre>
  
  <p>Login method in business layer for Users and Admin, and admin create by default (all in the same class)</p>
  
  <pre><code>public GestionUsuarios() {
          daoUsuario = new DaoUsuarioHibernate();
  
          Usuario admin = new Usuario("admin", "admin", true);
  
          daoUsuario.add(admin);
      } 
  public Usuario registrarUsuario(String nombreUsuario, String contrasenia) {
          return daoUsuario.ObtenerUsuarioPorNombreYContrasenia(new Usuario(nombreUsuario, contrasenia, false));
  
      }
  
  public Usuario registrarAdministrador(String usuario, String contrasenia){
  
          return daoUsuario.ObtenerUsuarioPorNombreYContrasenia(new Usuario(usuario,contrasenia, true));
      }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/GVlYb.png" rel="nofollow">The jsp structure, /app for normal users and /admin for all privileges</a></p>