标签:
1 *自定义的: 2 >代码: 3 public class EncodingFilter implements Filter{ 4 private String charset="utf-8"; 5 public void destroy(){ 6 } 7 public void doFilter(ServletRequest req,ServletResponse resp, 8 FilterChain chain){ 9 HttpServletRequest request=(HttpServletRequest)req; 10 if(request.getMethod().equalsIgnoreCase("GET")){ 11 if(!request instanceof GetRequest){ 12 //处理get请求编码 13 request=new GetRequest( requset,charset); 14 } 15 }else{ 16 //设置POST请求编码为默认编码utf-8 17 request.setCharacterEncoding(); 18 } 19 chain.doFilter(request,resp); 20 } 21 public void init(FilterConfig fConfig) throws Exception{ 22 String charsetfConfig.getInitParameter("charset"); 23 if(charset!=null && !charset.isEmpty()){ 24 this.charset=charset; 25 } 26 } 27 } 28
1 *Spring框架提供的的CharacterEncodingFilter 2 >配置: 3 <filter> 4 <filter-name>characterEncodingFilter</filter-name> 5 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 6 <init-param> 7 <param-name>encoding</param-name> 8 <param-value>utf-8</param-value> 9 </init-param> 10 </filter> 11 <filter-mapping> 12 <filter-name>characterEncodingFilter</filter-name> 13 <url-pattern>/*</url-pattren> 14 </filter-mapping>
标签:
原文地址:http://www.cnblogs.com/liaowanzhong/p/4781015.html