解决全站乱码过滤器

中文乱码真的很讨厌,如果每次请求都使用request设置一下编码真的麻烦,我们可以使用过滤器filter来显示全站的乱码问题

package web.filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//解决全站乱码过滤器
public class CharacterEncodingFilter implements Filter {

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

     }

     public void doFilter(ServletRequest req, ServletResponse resp,
               FilterChain chain) throws IOException, ServletException {
         
          HttpServletRequest request = (HttpServletRequest) req;
          HttpServletResponse response = (HttpServletResponse) resp;
         
          request.setCharacterEncoding("UTF-8");
          response.setCharacterEncoding("UTF-8");
          response.setContentType("text/html;charset=UTF-8");
         
         
          MyCharacterEncodingRequest requestWrapper = new MyCharacterEncodingRequest(request);
          chain.doFilter(requestWrapper, response);
         

     }

     public void init(FilterConfig filterConfig) throws ServletException {
          // TODO Auto-generated method stub

     }
}

/*
1.实现与被增强对象相同的接口
2、定义一个变量记住被增强对象
3、定义一个构造器,接收被增强对象
4、覆盖需要增强的方法
5、对于不想增强的方法,直接调用被增强对象(目标对象)的方法
*/

class MyCharacterEncodingRequest extends HttpServletRequestWrapper{
    
     private HttpServletRequest request;
     public MyCharacterEncodingRequest(HttpServletRequest request) {
          super(request);
          this.request = request;
     }
     @Override
     public String getParameter(String name) {
         
          try{
               String value= this.request.getParameter(name);
               if(value==null){
                    return null;
               }
              
               if(!this.request.getMethod().equalsIgnoreCase("get")) {
                    return value;
               }
              
               value = new String(value.getBytes("ISO8859-1"),this.request.getCharacterEncoding());
               return value;
              
          }catch (Exception e) {
               throw new RuntimeException(e);
          }
     }
}

然后在web.xml中配置filter拦截所有请求

<filter>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<filter-class>web.filter.CharacterEncodingFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页