title: Passing attribute when redirecting from a filter to a JSP: Session vs Request
body: <p>Inside a filter I'm trying to send expired sessions back to a login page with a message that the user must login. I prefer to attach the message to the request object, rather than to the session object, so that I don't have to worry about erasing the message later.</p>
  
  <p>When I use inside the filter: </p>
  
  <pre><code>catch (NullPointerException exc)
  
  {
  
      String message = "You must log in to access this site.";
      url += "/login.jsp";
      request.setAttribute("message", message);
      httpResponse.sendRedirect(url); 
  }
  </code></pre>
  
  <p>The login page does NOT display the message (message is null). However, when I use:</p>
  
  <pre><code>session.setAttribute("message", message);
  </code></pre>
  
  <p>The message is displayed nicely on the login page.</p>
  
  <p>In servlets, I have no problem attaching and displaying messages using the request object. It's only with filters that I'm noticing this difficulty.</p>
  
  <p>Why is this happening, and can it be prevented? If I must use session when redirecting from a filter, what's the best way to erase the message on the JSP side?</p>