当前位置 : 主页 > 编程语言 > c++ >

EncodingFilter编码过滤器

来源:互联网 收集:自由互联 发布时间:2021-07-03
编码过滤器 package cn.cx.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
编码过滤器
package cn.cx.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;
import javax.servlet.http.HttpServletRequest;

public class NewFilter implements Filter {
	private String CharSet = null;
	@Override
	public void destroy() {
		System.out.println(" 销毁 !");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)Response;
		req.setCharacterEncoding(this.CharSet);
		System.out.println("doFilter()执行之前!");
		chain.doFilter(request, response);//下传页面 激活下一个过滤器
		System.out.println("doFilter()执行之后!");
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.CharSet = config.getInitParameter("CharSet");
		System.out.println("初始化成功 !");
	}

}
网友评论