title: Spring-MVC 3.1: Preventing A JSP From Being Cached In A Browser
body: <p>I'm using Spring 3.1 for my webapp.   I'm trying to stop just one JSP from being cached in the user's browser to prevent them from reaching it via their back button.</p>
  
  <p>I've had good luck using these settings in Servlets that print out an HTML page:</p>
  
  <pre><code>response.setHeader("Cache-control", "no-cache, no-store");
  response.setHeader("Pragma", "no-cache");                
  response.setHeader("Expires", "-1");                     
  </code></pre>
  
  <p>I haven't been able to get those some calls to work in controller function( which is usually invoked from a redirect):</p>
  
  <pre><code>  @RequestMapping(value = "/error")
      public String error(HttpSession session,HttpServletResponse response, ModelMap model){
          logger.debug("started");
  
          response.setHeader("Cache-control", "no-cache, no-store");
          response.setHeader("Pragma", "no-cache");                
          response.setHeader("Expires", "-1"); 
  
          String status_message = (String)session.getAttribute("status_message");
          status_message = (status_message == null) ? "" : status_message.trim();
          session.removeAttribute("status_message");
          model.put("status_message",status_message);
  
          return "error";
      }
  </code></pre>
  
  <p>I also tried putting the calls in my JSP like this( above and below the include for the header file ):</p>
  
  <pre><code>&lt;%@ include file="header.jsp" %&gt;
  &lt;%
     response.setHeader("Cache-control", "no-cache, no-store");
     response.setHeader("Pragma", "no-cache");                
     response.setHeader("Expires", "-1"); 
   %&gt;
       &lt;!-- content --&gt;
  
  &lt;%@ include file="footer.jsp" %&gt;
  </code></pre>
  
  <p>I also tried putting the calls in my header file that is included in every JSP, though I only want one JSP not stored....just to see if I could make it work.  It did not work:</p>
  
  <pre><code>&lt;%@ page language = "java" session = "true" import = "java.util.*, java.text.*" %&gt;
  &lt;%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;%@ taglib prefix = "f" uri="http://www.springframework.org/tags/form"%&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  
  &lt;head&gt;
  &lt;meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"&gt;
  &lt;meta http-equiv="Pragma" content="no-cache"&gt;
  &lt;meta http-equiv="Expires" content="0"&gt;
  
  &lt;title&gt;Acme Customer Service&lt;/title&gt;
      &lt;link rel = "StyleSheet"  href = "../nsd/css/nsd.css"   type = "text/css"/&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>I also tried experimenting with toggling the "Expires" setting between 0 and -1.  No dice.</p>
  
  <p>Any ideas on how I can get a single JSP to being cached in the user's browser?</p>
  
  <p>Thanks much in advance</p>
  
  <p>Steve</p>