zoukankan      html  css  js  c++  java
  • ServletWeb缓存解决问题

    (1)为什么我们要防止这个问题的浏览器页面缓存:


    所以在不须要缓存的页面中须要实现不缓存页面。

    代码例如以下:

    package com.lc.HttpTest;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CacheJiejue extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("text/html;charset=utf-8");
    		//指定该页面不缓存
    		response.setDateHeader("Expires",-1); //IE游览器支持的
    		
    		//保证兼容性
    		response.setHeader("Cache-Control", "no-cache");
    		response.setHeader("Pragme", "no-cache");
    		
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		this.doGet(request, response);
    	}
    
    }
    

    (2)可是假设要实现特定时间内的页面缓存 则代码例如以下:


    package com.lc.HttpTest;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CacheJiejue extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("text/html;charset=utf-8");
    		//指定该页面不缓存
    		//response.setDateHeader("Expires",-1); //IE游览器支持的
    		
    		//缓存一定的时间  缓存 一天的时间
    		response.setDateHeader("Expires",System.currentTimeMillis()+3600*1000*24); 
    		//保证兼容性
    		response.setHeader("Cache-Control", "no-cache");
    		response.setHeader("Pragme", "no-cache");
    		
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		this.doGet(request, response);
    	}
    
    }
    




    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    LeetCode题解(14)--Longest Common Prefix
    LeetCode题解(12)--Integer to Roman
    LeetCode题解(13)--Roman to Integer
    LeetCode题解(9)--Palindrome Number
    LeetCode题解(8)--String to Integer (atoi)
    LeetCode题解(7)--Reverse Integer
    LeetCode题解(6)--ZigZag Conversion
    从并发和索引说说innodb和myisam的区别
    thrift基本概念和实例
    fastcgi+lighttpd+c语言 实现搜索输入提示
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4749075.html
Copyright © 2011-2022 走看看