字符拦截器(Filter过滤器)

字符过滤器:
import java.io.*;
import javax.servlet.*;
/**
 * 
 * @author qinjie
 * @File_name EncodingFilter.java
 * @create_time 2010-03-08
 *
 */
public class EncodingFilter implements Filter {
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (Exception e) {
		}

		chain.doFilter(request, response);
	}

	public void destroy() {

	}
};


在web.xml下加入一下代码
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>
			EncodingFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

即可

你可能感兴趣的:(java,xml,Web,servlet)