标签:
web.xml
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.lyq.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
过滤器CharacterEncodingFilter.java
public class CharacterEncodingFilter implements Filter {
protected String encoding =null;
protected FilterConfig filterConfig=null;
@Override
public void destroy() {
this.filterConfig=null;
this.encoding=null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (encoding!=null) {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
this.encoding=filterConfig.getInitParameter("encoding");
}
}
标签:
原文地址:http://www.cnblogs.com/ganjun/p/5045467.html