码迷,mamicode.com
首页 > Web开发 > 详细

Servlet解决网页缓存问题

时间:2014-09-02 22:56:25      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:servlet解决网页缓存问题   servlet   缓存   cache   httpservlet   

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

bubuko.com,布布扣

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

代码如下:

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   缓存   cache   httpservlet   

原文地址:http://blog.csdn.net/xlgen157387/article/details/39012993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!