可以防止出现中文乱码,并且可以更改编码方式

解决方案 »

  1.   

    <filter>
        <filter-name>encodingFilter</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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  2.   

    。package dw05prj.util.filter; 
    import javax.servlet.Filter; 
    import javax.servlet.FilterConfig; 
    import javax.servlet.ServletException; 
    import javax.servlet.ServletRequest; 
    import javax.servlet.ServletResponse; 
    import javax.servlet.FilterChain; 
    import java.io.IOException; 
    /*页面字符集过滤流*/ 
    /* 
    在web.xml中配制的例子 
    <filter> 
    <filter-name>CharEncodingFilter</filter-name> 
    <filter-class>dw05prj.util.filter.CharEncodingFilter</filter-class> 
    <init-param> 
    <param-name>charset</param-name> 
    <param-value>GBK</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>CharEncodingFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    */ 
    public class CharEncodingFilter implements Filter { 
    private String charSet ; 
    public void init(FilterConfig parm1) throws ServletException { 
    charSet = parm1.getInitParameter("charset") ; 
    if (charSet == null && charSet.length() < 1) 

    charSet = "UTF-8" ; 

    System.out.println ("CharEncodingFilter--字符集: " + this.getCharSet()) ; 
    } public void doFilter(ServletRequest parm1, ServletResponse parm2, 
    FilterChain parm3) throws IOException, ServletException { 
    // 设定字符集 
    parm1.setCharacterEncoding(this.getCharSet()); 
    parm3.doFilter(parm1, parm2); 
    //页面中每个向服务器的请求都被过滤 -- 如 图片,js,HTML/JSP等等 
    //System.out.println ("---CharEncodingFilter: " + this.getCharSet()) ; 

    public void destroy() { 
    // TODO: Add your code here 
    this.setCharSet(null) ; 
    } public void setCharSet(String charSet) { 
    this.charSet = charSet; 

    public String getCharSet() { 
    return (this.charSet); 

    }本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/15982.htm
      

  3.   

    public void doFilter(ServletRequest req,
    ServletResponse res,
    FilterChain chain) 
    throws IOException, ServletException {
    //过滤请求
    req.setCharacterEncoding("utf-8");
    chain.doFilter(req, res);//将请求转向下一个过滤器
    //过滤响应
    }在web.xml中的配置
    <filter>
       <filter-name>Gbk</filter-name>
       <filter-class>filter.Gbk</filter-class>
      </filter>
      

  4.   

    http://blog.csdn.net/jinchun1234/archive/2009/06/18/4280998.aspx