title: How to authenticate users using servlet
body: <p>I'm new to Java EE. I created a login form and servlet to authenticate a user's credentials, but when I check the remote user with <code>getRemoteUser()</code> method after authentication, my code doesn't display the user name.</p>
  
  <p>My login servlet is :</p>
  
  <pre><code>String uname,pass;
  PreparedStatement ps=null;
  uname=request.getParameter("uname");
  pass=request.getParameter("pass");
  
  ResultSet rs = null;
  Connection con = null;
  try {
      con = prepareConnection();
  
      String Query="select uname,email from passmanager where pass=?";
      ps=con.prepareStatement(Query);
  
      ps.setString(1,pass);
      rs=ps.executeQuery();
  
      while(rs.next())
      {
          if (uname.equals(rs.getString("uname")) || uname.equals(rs.getString("email"))) 
          {
              rs.close();                                                              
              ps.close();                                                            
              ps = null;                                                             
              con.close();                                                            
              con = null;  
              HttpSession session = request.getSession(true); 
              session.setAttribute("currentSessionUser",uname); 
              RequestDispatcher dis = request.getRequestDispatcher("/user.html");
              dis.forward(request, response);
              break;
          }
      }
  }
  catch(Exception e)
  {
      System.out.println(e);
  }
  </code></pre>
  
  <p>If anything need to change/modify in it please tell me and help me..... thanks in advance.</p>