The so-called browser caching means that when you access a web page for the first time, the browser will cache these web pages locally. When you access these cached web pages next time, the browser will directly read the content of these web pages from the local area without having to obtain them from the network.
Although the caching function provided by the browser can effectively improve the loading speed of web pages, for some web pages that require real-time updates, this caching mechanism will affect the normal display of web pages. Fortunately, there are three fields in the HTTP response message header to turn off the cache function of the client browser. The following three statements use these three fields to close the browser's cache:
response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-cache");response.setHeader("Pragma", "no-cache");Although the above three HTTP response message header fields can all close the browser cache. However, not all browsers support these three response message header fields, so it is best to use the above three response message header fields to close the browser's cache.
Example: Disable browsers from cache current web pages
1. Example description
This program demonstrates the performance when submitting request messages through form when the browser cache is not closed and the browser cache is closed.
2. Write a Cache class
The browser cache is closed using the above three response message header fields in the Cache class and outputs a piece of HTML code to the client to test the effect of closing the cache and not closing the cache. The implementation code of the Cache class is as follows:
public class Cache extends HttpServlet{ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String cache = request.getParameter("cache"); if (cache != null) { if (cache.equals("false")) { // Close the browser cache response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); } } // Define HTML code String html = "<form id = 'form', action='test' method='post'>" + "Name: <input type='text' name = 'name'/>" + "<input type='submit' value='submit' />" + "</form>"; PrintWriter out = response.getWriter(); out.println(html); // Output HTML code to the client}}As can be seen from the above code, the browser's cache is closed when the cache request parameter value is false.
3. Configure the Cache class
The configuration code of the Cache class is as follows:
<servlet> <servlet-name>Cache</servlet-name> <servlet-class>chapter5.Cache</servlet-class></servlet><servlet-mapping> <servlet-name>Cache</servlet-name> <url-pattern>/Cache</url-pattern></servlet-mapping>
4. Test the situation where the browser cache is not closed
Enter the following URL in the browser address bar:
http://localhost:8080/demo/Cache?cache=true
Enter any string in the [Name] text box and click the [Submit] button. At this time, the browser will display an exception (this exception is caused by the submitted test that does not exist, so we don’t have to worry about it), and then click the browser’s return button to return to the page where the data was entered just now. We can see that the string entered just now still exists. This means that when returning, the browser does not regain the page from the server, but reloads the current page from the local cache.
5. Test the situation of closing browser cache
Enter the following URL in the browser address bar to close the browser cache:
http://localhost:8080/demo/Cache?cache=false
Submit and return in the previous step, and found that the data entered just now was gone. This means that after closing the browser cache, the browser will always regain the current page from the server every time it returns. Therefore, the current page always maintains the initial value.
6. Procedure summary
When closing browser cache, in order to ensure that it is effective in most browsers as much as possible, I recommend using the above three HTTP response message header fields to close browser cache at the same time.
Summarize
The above is the method of JavaWeb prohibiting browsers from cacheing current web pages. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!