无为清净楼资源网 Design By www.qnjia.com

jsp代码:

import javax.servlet.*;
import javax.servlet.http.*;

public class CharsetFilter implements Filter
{
  public void destroy() 
  {
    
  }
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
  {
    try
    {
      HttpServletRequest httpRequest = (HttpServletRequest)request;
      String method = httpRequest.getMethod().toLowerCase();
      if(method.equals("post"))
      {
        //如果是post,即表单方法,直接设置charset即可
        request.setCharacterEncoding("UTF-8");
      }
      else if(method.equals("get"))
      {
        //如果是get方法
        request.setCharacterEncoding("UTF-8");
        request = new HttpServletRequestWrapper((HttpServletRequest)request)
        {
          public String getParameter(String str)
          {
            try
            {
              return new String(super.getParameter(str).getBytes("iso-8859-1"),"GBK");
            }
            catch(Exception e)
            {
              return null;
            }
   
          }
        };
      }
      
      chain.doFilter(request, response);
    }
    catch(Exception e){}
  }
  
  public void init(FilterConfig filterConfig) 
  {
    
  }
}

过滤器配置:

<filter>
    <filter-name>CharFilter</filter-name>
    <filter-class>CharsetFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
标签:
JSP,Post,Request,中文乱码

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com