title: Incorrect data redirects user to a blank page from a Servlet Login page
body: <p>In my project a user logs in by providing his/her <code>emailid</code> and <code>password</code>. If both match, one can successfully log-in. If not he will be redirected to <code>UserHome.jsp</code> page. Here is my code:</p>
  
  <pre><code>import getset.Getset;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import accessdb.Dao;
  
  public class LoginAuthentication extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // Authentication and Logging in The Registered User
          Getset g=new Getset();
          Dao dao=new Dao();
          String userid="";
          String fname="";
          //    PrintWriter pw=response.getWriter();
          String loginemail=request.getParameter("loginemail");
          String loginpassword=request.getParameter("loginpassword");
          if (loginemail.equals("") || 
              loginemail.equals(" ") || 
              loginpassword.equals("") || 
              loginpassword.equals(" "))
  
              response.sendRedirect("WelcomePage.jsp");
  
          g.setloginemail(loginemail);
          g.setloginpassword(loginpassword);
          try {
              ResultSet rs=dao.loginauthentication(g);
              while(rs.next())
              {
                  String regemail=rs.getString("regemail");
                  String regpassword=rs.getString("regpassword");
                  if(loginemail.equals(regemail) &amp;&amp; 
                     (loginpassword.equals(regpassword))==true)
                  {
                      ResultSet rs1=dao.getnameid(g);
                      while(rs1.next())
                      {
                           userid=rs1.getString("USERID");
                           fname=rs1.getString("FNAME");
                      }
                      HttpSession session = request.getSession(true);
                      session.setAttribute("USERID", userid);
                      session.setAttribute("FNAME", fname);
                      response.sendRedirect("UserHome.jsp");
                      break;
                  }
                  else if(loginemail.equals(regemail) &amp;&amp; (loginpassword.equals(regpassword))==false)
                  {
                      response.sendRedirect("WelcomePage.jsp");
                      return;
                  }
              }
          } 
          catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
          catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I have tried to cover all the cases that may arise during log-in. The user can not access if</p>
  
  <ol>
  <li>both the fields remain empty.</li>
  <li><code>email</code> is filled (with correct or incorrect data), but <code>password</code> is not filled.</li>
  <li><code>password</code> is filled (with correct or incorrect data), but <code>emailid</code> is not filled.</li>
  </ol>
  
  <p>The area which I tried to cover, but is not happening, is don't let a user access if any of the fields are filled with incorrect data. For this part I have written in the code:</p>
  
  <pre><code> if(loginemail.equals(regemail) &amp;&amp; (loginpassword.equals(regpassword))==true)
       //user accesses
   else 
       if(loginemail.equals(regemail) &amp;&amp; (loginpassword.equals(regpassword))==false)
           //user cannot access
  </code></pre>
  
  <p>But I don't know why it's not showing the expected behaviour and when both the fields are filled with incorrect data, user is <strong>redirected</strong> to a blank page!</p>
  
  <p><strong>Addition:</strong> I also tried</p>
  
  <pre><code>if(loginemail.equals(regemail) &amp;&amp; (loginpassword.equals(regpassword)))
     //User accesses
   else if(!loginemail.equals(regemail) || (!loginpassword.equals(regpassword)))
    //User cannot access
  </code></pre>
  
  <p>But it too could not help!</p>