struts2中文乱码解决方法(post请求)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2中文乱码解决方法(post请求)相关的知识,希望对你有一定的参考价值。

 

package com.platform.common.web.servlet;

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;


public class SetCharEncodingFilter implements Filter {

    protected FilterConfig filterConfig = null;
    protected String encoding = "UTF-8";
    public void destroy() {
        filterConfig = null;
        encoding = null;
    }
 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
       /* if(encoding != null && !"".equals(encoding))
            servletRequest.setCharacterEncoding(encoding);*/
        
        servletRequest.setCharacterEncoding("utf-8");
        //servletResponse.setContentType("text/html;charset=utf-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }
 
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }

}

 

web.xml:

<filter>
      <filter-name>Set Character Encoding</filter-name> 
      <filter-class>com.platform.common.web.servlet.SetCharEncodingFilter</filter-class> 
     </filter> 
     <filter-mapping> 
         <filter-name>Set Character Encoding</filter-name>
         <url-pattern>/*</url-pattern> 
     </filter-mapping>

 

以上是关于struts2中文乱码解决方法(post请求)的主要内容,如果未能解决你的问题,请参考以下文章

使用httpclient post请求中文乱码解决办法

get/post请求中文出现乱码解决

GET和POST中文乱码的解决方法

OkHTTP、Retrofit 中文乱码解决方法

iOS 开发 post 提交数据 中文乱码 一串问号,怎么解决

get请求和post的请求的乱码解决