title: How to retain login information for subsequent use
body: <p>Im fairly new to programming in JSP and I am making this web application where users need to log-in. I've done the registration of users but I am having problems when users are logging-in.</p>
  
  <p>The main problem I am having is that, even though a user is able to successfully log-in, the information from the login form is lost. I need to retain the login information so that I can access the user's information during his/her session using the web application.</p>
  
  <p>Here is the code that I currently have:</p>
  
  <p><strong>index.jsp (this is where the login form is shown)</strong></p>
  
  <pre><code>&lt;form name="Login Form" action="login.jsp"&gt;&lt;table&gt;
     &lt;tbody&gt;
     &lt;tr&gt;
         &lt;td&gt;&lt;input type="text" name="emaillogin" value="email"/&gt;&lt;/td&gt;
         &lt;td&gt;&lt;input type="text" name="passlogin" value="password"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
          &lt;td colspan="2" align="RIGHT&gt;&lt;input type="submit" value="login" name="Login"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;&lt;/form&gt;
  </code></pre>
  
  <p><strong>login.jsp (this performs checking whether the user is valid or not, and redirects it to the user homepage if valid)</strong></p>
  
  <pre><code>&lt;%!
          String email = "";
          String password = "";
      %&gt;
      &lt;%
  
          List users = new ArrayList();
  
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "pass");
          Statement stmt = con.createStatement();
  
          ResultSet result = stmt.executeQuery("SELECT email,password  FROM users");
  
          while (result.next()) {
              users.add(result.getString(1));
              users.add(result.getString(2));
          }
          con.close();
  
          email = request.getParameter("emaillogin");
          password = request.getParameter("passlogin");
  
          int hasUser = 0;
          Iterator&lt;String&gt; it = users.iterator();
          while (hasUser == 0 &amp;&amp; it.hasNext()) {
              if (email.equals(it.next()) &amp;&amp; password.equals(it.next())) {
                  hasUser = 1;
              }
          }
  
          if (hasUser == 1) {
              response.sendRedirect("homepage.jsp");
          } else {
              response.sendRedirect("index.jsp");
          }
  
      %&gt;  
  </code></pre>
  
  <p><strong>homepage.jsp (this is where the user is redirected to after logging in, showing his/her email address)</strong></p>
  
  <pre><code>.
  .
  &lt;% out.println("Logged in as: " + request.getParameter("email")); %&gt;
  .
  .
  </code></pre>
  
  <p>The current problem is that, null value is what I am getting from homepage.jsp. How can I solve this problem?</p>
  
  <p>Thank you!</p>