- 在web.xml 配置
<!--编码过滤 utf-8 --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.yongjie.Zlxypt.util.FilterEncoding</filter-class> <init-param> <param-name>enconding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2. 过滤器代码
package com.yongjie.Zlxypt.util; 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; /** * * @ClassName: FilterEncoding * @Description: TODO(编码过滤器) * @date 2017年9月28日 下午4:13:38 * */ public class FilterEncoding implements Filter{ private String enconding; @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(enconding); response.setCharacterEncoding(enconding); chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { enconding = config.getInitParameter("enconding"); } }
时间: 2024-11-05 18:47:26