当前位置: 代码迷 >> JavaScript >> jsp 用 过滤器 解决 中文乱码 的有关问题(超简单的步骤)
  详细解决方案

jsp 用 过滤器 解决 中文乱码 的有关问题(超简单的步骤)

热度:116   发布时间:2012-11-10 10:48:50.0
jsp 用 过滤器 解决 中文乱码 的问题(超简单的步骤)
1jsp页头设置为UTF-8   如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


2
过滤器,,也就一个java类而已:代码如下:
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 SetCharacterEncodingFilter implements Filter {
     private String encoding;
     public void init(FilterConfig filterConfig) throws ServletException {
      // TODO Auto-generated method stub
      this.encoding=filterConfig.getInitParameter("encoding");
     }

     public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain chain) throws IOException, ServletException {
      // TODO Auto-generated method stub
      request.setCharacterEncoding(this.encoding);
      response.setCharacterEncoding(this.encoding);
      chain.doFilter(request,response);
     }

     public void destroy() {
      // TODO Auto-generated method stub

     }

    }





3web.xml中添加过滤器的配置:
 <filter>
  <filter-name>SetCharacterEncodingFilter</filter-name>
  <filter-class>SetCharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>utf-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>SetCharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>


很简单吧。。。嘿嘿。初级的东西,虽然绝大多数人都知道,不过写出来以后自己也会用到。。大家多多支持哈。。。。
1 楼 褚晓娜(0511) 2010-12-17  
  相关解决方案