您好!欢迎来天下评测网-推荐优质主机-揭露骗子垃圾主机免费收录IDC商家
首页 >> 编程语言

java解决全站乱码filter

package com.wordyun.web.filter;
  
import java.io.IOException;
import java.io.UnsupportedEncodingException;
  
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
/**
 * 全站乱码
 * @author Administrator
 *
 */
public class CharacterEncodingFilter implements Filter {
    private FilterConfig filterConfig;
      
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
    }
  
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
          
        HttpServletRequest request=(HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse) resp;    
          
        String charset=this.filterConfig.getInitParameter("charset");
        if(charset==null || charset.trim().equals("")){
            charset="utf-8";
        }
        request.setCharacterEncoding(charset);
        response.setCharacterEncoding(charset);
        response.setContentType("text/html;charset="+charset);
          
        chain.doFilter(new MyRequest(request), response);
    }
    class MyRequest extends HttpServletRequestWrapper{
        private HttpServletRequest request;
        public MyRequest(HttpServletRequest request) {
            super(request);
            this.request=request;
        }
        @Override
        public String getParameter(String name) {
            String value=this.request.getParameter(name);
              
            if(value==null) return null;
              
            if(!this.request.getMethod().equalsIgnoreCase("get")){
                return value;
            }
            //System.out.println(request.getCharacterEncoding());
            try {
                  
                value=new String(value.getBytes("iso8859-1"),this.request.getCharacterEncoding());
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            //System.out.println(value);
            return value;
        }
          
    }
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
          
    }
  
}

在web.xml配置过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.wordyun.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>charset</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>