解决请求参数的中文乱码问题

GET方式提交

在tomcat服务器的server.xml如下位置添加URIEncoding="UTF-8"
解决请求参数的中文乱码问题_第1张图片

POST方式提交

在web项目里的web.xml配置spring编码过滤器

<filter>
    <filter-name>characterEncodingfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
        <param-name>encodingparam-name>
        <param-value>UTF-8param-value>
    init-param>
filter>

<filter-mapping>
    <filter-name>characterEncodingfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

Struts2解决乱码问题

1.自定义Filter实现类:MyCharacterEncodingFilter

//ctrl+shift+o   导入各种包 即可
/**
 * 过滤器的使用方法  
 * 在创建类的时候 要用到接口Filter
 * 要自己在web.xml文件中配置过滤器
 */
public class MyCharacterEncodingFilter extends HttpServlet implements Filter {

    private String encoding=null;

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding"); //获得配置文件中的encoding
    }
}

2.配置web.xml


<filter>
    <filter-name>MyCharacterEncodingFilterfilter-name>
    <filter-class>com.zyj.filter.MyCharacterEncodingFilterfilter-class>
    <init-param>
        <param-name>encodingparam-name>
        <param-value>utf-8param-value>
    init-param>
filter>
<filter-mapping>
    <filter-name>MyCharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

你可能感兴趣的:(【问题总结】)