title: Passing session attributes through forward and redirect
body: <p>I am trying to figure out how to use sessions using JSTL, so I began by trying to set a session attribute in a Jsp and passing that attribute to another Jsp within the same application and retrieving it there.</p>
  
  <p>I set the attribute and used the dispatcher to get to the other servlet, in another attempt I used sendRedirect to forward the request to the other servlet.</p>
  
  <p>Here is the first Jsp (where I set the attribute):</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;c:set var="session" scope="session" value="test"&gt;&lt;/c:set&gt;
  &lt;%
  response.sendRedirect("session.jsp"); // I know I could use the JSTL tag forward
  %&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In the other Jsp ("session.jsp"):</p>
  
  <pre><code>&lt;%
  
  HttpSession s = request.getSession();
  if(s.isNew())
      out.print("new session " );
  
  out.print(s.getAttribute("session"));  
  %&gt;
  </code></pre>
  
  <p>When I use sendRedirect the result is <code>"test"</code>, meaning that the session wasn't newly created.
  However, when I use forward (dispatcher) the result is <code>"new session test"</code>.</p>
  
  <p>I don't know the reason for such behavior, although it would make sense if the results were reversed.</p>