帝王谷资源网 Design By www.wdxyy.com

JSP 开发之Servlet解决网页缓存问题

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

JSP 开发之Servlet解决网页缓存问题

所以在不需要缓存的页面中需要实现不缓存页面;

代码如下:

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); 
  } 
 
} 

以上就是Servlet解决网页缓存的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:
Servlet解决网页缓存,Servlet网页缓存问题解决办法

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com