日期:2014-05-16 浏览次数:20361 次
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
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 SetCharacterEncodingFilter implements Filter { private String encoding; public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub this.encoding=filterConfig.getInitParameter("encoding"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub request.setCharacterEncoding(this.encoding); response.setCharacterEncoding(this.encoding); chain.doFilter(request,response); } public void destroy() { // TODO Auto-generated method stub } }
<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>