首页 > 其他 > 详细

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

时间:2016-02-22 19:10:45      阅读:243      评论:0      收藏:0      [点我收藏+]

 

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请求)

原文:http://www.cnblogs.com/yushouling/p/5207758.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!