title: Storing and retrieving values from JSP to servlet using Sessions
body: <p>I have login.jsp page where I try to username and password from text boxes are passed to servlet though session. The code o login.jsp is given below:</p>
  
  <pre><code>&lt;form name="frm" action="DisplayData" method="post" onsubmit="return Validate()" &gt;
      &lt;table align="center"&gt;
          &lt;tr&gt;
              &lt;td align="left"&gt;UserName:&lt;/td&gt;
              &lt;td&gt;&lt;input type="text"  name="username" value=""&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td align="left"&gt;Password:&lt;/td&gt;
              &lt;td&gt;&lt;input type="password"  name="password" value=""&gt;&lt;/input&gt; &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;%
         String name = request.getParameter("username");
         String password = request.getParameter("password");
         session.setAttribute("name",name);
         session.setAttribute("pass", password);
      %&gt;
      &lt;table align="center"&gt;
          &lt;tr&gt;
              &lt;input type="hidden" name="hiddenname" value="login"&gt;
              &lt;td&gt;&lt;input align="middle" type="submit" name="Sign_in" value="Sign_in"  onclick="return Validate()"&gt;&lt;/input&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="button" value="Signup" onClick="javascript:window.location='Signup.jsp';"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I would like to retrieve the username and password that is set in login.jsp to servlet. I tried using the following code but it gives me null value. The java code I used in servlet is given below:</p>
  
  <pre><code> HttpSession session = request.getSession(true);
      String name=(String)session.getAttribute("name");
      System.out.println("Welcome"+name);
  </code></pre>
  
  <p>Could anyone tell me where I am making mistake. I Need the username to be stored in session so that I can make use of this for multiple request in that servlet</p>