首页 > 代码库 > servletResponse 控制浏览器缓存
servletResponse 控制浏览器缓存
//当访问一些资源文件时,我们希望,访问一次后,资源文件能够在缓存在浏览器中,当我们再次访问该资源时
//直接从缓存中去取,这样可以减少服务器的压力
package response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo6 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置头信息,控制浏览器缓存
response.setDateHeader("expires",System.currentTimeMillis()+1000*3600);//时间值一定得是当前时间值加上要缓存的时间
//模拟场景
String data = "http://www.mamicode.com/aaaaa";
response.getWriter().write(data);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}