Servlet struts2 clear page Cache

Source: Internet
Author: User

What is page cache? Simply put, try the forward/backward button on IE or Firefox toolbar and you will know. that is to say, the browser will cache the page first. When it is used for the next time, it will retrieve data directly from the cache without re-retrieving data from the web server. this reduces the pressure on the server.

But there are disadvantages in everything. For example, how do I log out when logging on to a web page? Of course, the servlet can use session. removeattribute ("LOGNAME"); Session. invalidate (); however, this alone is not perfect. If the user does not close the browser in time, pressing the "back" button may cause an insecure factor (because the user does not actually exit ). Fortunately, the HTTP protocol can customize the cache page. Example:

Generally, the web page has a login button. Login button ing servlet or struts action and so on. That is to say, after the login action, it usually jumps to the home page. Therefore, add the clear cache code before the sendredirect or forward action:

Servlet Session. setattribute ("LOGNAME", LOGNAME); // The user writes the session </P> <p> // clear the page </P> <p> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setheader ("cache-control", "No-store"); <br/> response. setdateheader ("expires", 0); </P> <p> // jump to the page </P> <p> requestdispatcher = NULL; <br/> requestdispatcher = request. getrequestdispatcher ("index. JSP "); <br/> requestdispatcher. forward (request, response); <br/>Struts2 Httpservletresponse response = servletactioncontext. getresponse (); <br/> // clear the cache on the login page <br/> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setheader ("cache-control", "No-store"); <br/> response. setdateheader ("expires", 0); </P> <p> // page relocation, countdown. <Br/> string url = "index. JSP "; <br/> string content = 3 +"; url = "+ URL; // enter the home page 3 seconds later <br/> response. setheader ("refresh", content); </P> <p>In this way, when session. invalidate () is called, and then press IE or Firefox to exit, a prompt indicating that the page has expired will appear. Response. setheader ("refresh", content); implements countdown jump page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.