Servlet/JSP caching problem - Back button issue


Web tier: servlets, JSP, Web frameworks: Servlet/JSP caching problem - Back button issue

  1. Servlet/JSP caching problem - Back button issue (6 messages)

    I use Servlet to control the JSP flow. When user press 'Back' button at the browser, it will load the page from cache (not retrieve info from the server) i.e. the wrong (not updated) info will be shown.

    I already set the JSP page as following:

    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

    But it doesn't work for my case.
    I am using tomcat 3, IE 5.5

    Can anyone kindly give me suggestion how to solve it ? Thanks a lot.
  2. usually when you push 'back' in the browser software, the browser (and not the server) usually displays a previously requested page and not the results of a new request. setting no-cache in the response simply guarantees that the browser regenerates the page on a new request for the page. Hitting back doesn't qualify as a new request.

    Therefore, you'd have to hit back and press refresh or add a back button on your site to make it a new request.

    i hope this clarifies things.
  3. Thanks a lot to your info. I know it will work when press 'back' and press 'refresh' to update the content.

    However, in some big shopping site (e.g. amazon), even you keep pressing Back button, the information in the shopping basket is the most updated one (without loading the cache version from your browser and you don't need to press 'refresh'). How can they do this ?
  4. I believe they have a small Javascript function at the beginning of their page maybe an onLoad() function? that refreshes the page.
  5. I have used this and it works perfectly for me !!

    response.setHeader( "Pragma", "no-cache");
    response.setHeader( "Cache-Control", "no-store, no-cache");

    The no-store directive does not let the page be stored. So browser has to make a new request even on pressing a back button.
  6. One more note,

    I think Expires should be -1. 0 means it never expires !!
  7. hi sachin sahasrabuddhe,

    Is the statement you mentioned all put at the beginning of a JSP ? Is it necessary to put a set on Servlet side as i use the Servlet acts as a controller and forward JSP to client. Many Thanks.