当前位置:首页 > JSP教程 > JSP常见问题

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

jsp 开发之servlet解决网页缓存问题

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

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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐