当前位置: 代码迷 >> Web前端 >> 解决中文乱码,在web.xml中配备核心过滤器
  详细解决方案

解决中文乱码,在web.xml中配备核心过滤器

热度:135   发布时间:2012-09-10 11:02:33.0
解决中文乱码,在web.xml中配置核心过滤器
方法一、手动编写中文乱码过滤器
public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException, ServletException { 
request.setCharacterEncoding("UTF-8"); 
response.setCharacterEncoding("UTF-8"); 
chain.doFilter(request,response); 
System.out.println("过滤器utf-8"); 
} 

<!-- 手动编写中文乱码过滤器--> 
<filter> 
<filter-name>characterEncoding</filter-name> 
<filter-class>dlr.co.util.CharacterEncodingFilter</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>characterEncoding</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 


方法二、使用核心过滤器
<!-- 配置核心过滤器 --> 
<filter> 
<filter-name>SetCharacterEncoding</filter-name> 
<filter-class> 
org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>UTF-8</param-value> 
</init-param> 
<init-param> 
<param-name>forceEncoding</param-name> 
<param-value>true</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>SetCharacterEncoding</filter-name> 
<url-pattern>/*</url-pattern> 
  <dispatcher>REQUEST</dispatcher>  
  <dispatcher>FORWARD</dispatcher>  
  <dispatcher>INCLUDE</dispatcher> 
    </filter-mapping> 
  <filter-mapping> 
  <filter-name>SetCharacterEncoding</filter-name> 
  <url-pattern>*.jsp</url-pattern> 
</filter-mapping> 
  相关解决方案