Filter实现浏览器是否缓存页面信息,和控制编码

2 篇文章 0 订阅
1 篇文章 0 订阅
后台配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>jqueryeasyui</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>



	<!-- 设置浏览器缓存 -->
	<filter>
		<filter-name>ResponseHeaderFilter</filter-name>
		<filter-class>tk.Martin.filterDemo.filter.ResponseHeaderFilter</filter-class>
		<init-param>
			<param-name>Cache-Control</param-name>
			<param-value>max-age=1000</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>ResponseHeaderFilter</filter-name>
		<url-pattern>*.css</url-pattern>
	</filter-mapping> 


	<!-- 设置浏览器不缓存 -->
	<!--  <filter>
		<filter-name>ResponseHeaderFilter</filter-name>
		<filter-class>tk.Martin.filterDemo.filter.ResponseHeaderFilter</filter-class>
		<init-param>
			<param-name>Cache-Control</param-name>
			<param-value>private,no-cache,no-store,must-revalidate</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>ResponseHeaderFilter</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>ResponseHeaderFilter</filter-name>
		<url-pattern>*.css</url-pattern>
	</filter-mapping> 
	<filter-mapping>
		<filter-name>ResponseHeaderFilter</filter-name>
		<url-pattern>*.jpg</url-pattern>
	</filter-mapping> 
 -->


	<servlet>
		<servlet-name>UserServlet</servlet-name>
		<servlet-class>tk.Martin.filterDemo.servlet.UserServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>UserServlet</servlet-name>
		<url-pattern>/userOper.do</url-pattern>
	</servlet-mapping>






</web-app>



这是控制是否浏览器缓存的代码

package tk.Martin.filterDemo.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseHeaderFilter implements Filter {
	FilterConfig filterConfig;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) res;
		
		for(Enumeration e=filterConfig.getInitParameterNames();e.hasMoreElements();){
			
			String headerName=(String) e.nextElement();
			System.out.println("========================="+headerName+"===================================");
			response.addHeader(headerName, filterConfig.getInitParameter(headerName));
			
		}
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		this.filterConfig=filterConfig;
	}

}



这是控制上传的编码的Filter过滤类

package tk.Martin.filterDemo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {
	private String characterEncoding;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding(characterEncoding);
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		this.characterEncoding = filterConfig.getInitParameter("encoding");
		System.out.println("页面编码方式===============" + this.characterEncoding
				+ "=================================");
	}

}

这是前台的jsp的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@include file="/common/tag.jsp"%>
<%@include file="/common/jquery.jsp"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<form action="${pageContext.request.contextPath}/userOper.do"
		method="post">

		<table>
			<tr>
				<td>用户名:</td>
				<td><input name="name" type="text"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="提交"></td>
			</tr>
		</table>

	</form>
	<img alt="" src="${pageContext.request.contextPath}/1.jpg">
</body>
</html>






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值