JSP 의 Post 와 Request 중국어 난 장 판 문 제 를 필터(Filter)로 해결 합 니 다.

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>

좋은 웹페이지 즐겨찾기