post请求中文乱码

2017-10-12 19:31:15来源:作者:人点击

分享

 

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
            cn.mgr.util.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/rest/*</url-pattern>
    </filter-mapping>

 

package cn.mgr.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;

/**
 * 过滤请求编码(只对post请求有效)
 * get需在Tomcat的server.xml中的:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"

URIEncoding="GBK" />

)加入URIEncoding="GBK",解决get请求乱码问题
 * @author lenovo
 *
 */
public class CharacterEncodingFilter implements Filter {

 private FilterConfig config;
 
 @Override
 public void destroy() {

 }

 @Override
 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
  String code = config.getInitParameter("encoding");
  req.setCharacterEncoding(code);
  chain.doFilter(req, res);
 }

 @Override
 public void init(FilterConfig conf) throws ServletException {
  this.config = conf;
 }

}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台