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

用注解定义的java过滤器

来源:互联网 收集:自由互联 发布时间:2021-07-03
CharsetEncodingFilter package com.jxnd.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletReques
CharsetEncodingFilter
package com.jxnd.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.annotation.WebFilter;

/**
 *
 使用注解定义编码过滤器
 *
 @author 陈刚
 */
/**
 *
 @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器组件
 *
 属性filterName声明过滤器的名称,可选
 *
 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 */
@WebFilter(filterName="CharsetEncodingFilter",urlPatterns="/*")
public class CharsetEncodingFilter implements Filter {

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
网友评论