• 全局编码过滤器


    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;
     
    public class EncodingFilter implements Filter {
         
        private static String encoding;
        private static final String DEFAULT_CHARSET="UTF-8";
        @Override
        public void destroy() {
            System.out.println("--------destroy---------");
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("--------doFilter---------");
            HttpServletRequest httpRequest = (HttpServletRequest) request;  
            HttpServletResponse httpResponse=(HttpServletResponse)response;
            if("GET".equals(httpRequest.getMethod())){
                EncodingHttpServletRequest wrapper = new EncodingHttpServletRequest(httpRequest, encoding);   
                chain.doFilter(wrapper, response);  
            }else{
                httpRequest.setCharacterEncoding(encoding);
                httpResponse.setContentType("text/html;charset="+encoding);
                chain.doFilter(request, response);  
            }
        }
        private static class  EncodingHttpServletRequest extends HttpServletRequestWrapper{
             private HttpServletRequest request;
             public EncodingHttpServletRequest(HttpServletRequest request,String encoding) {
               super(request);
               this.request = request;
             }
             @Override
            public String getParameter(String name) {
                   String value = request.getParameter(name);
                   try {
                    value = new String(value.getBytes("iso8859-1"), encoding);
                   } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                   }
                   return super.getParameter(name);
            }
        }
        @Override
        public void init(FilterConfig config) throws ServletException {
             System.out.println("--------init---------");  
             encoding = config.getInitParameter("encoding");
             if(encoding==null||"".equals(encoding))
                 encoding=DEFAULT_CHARSET;
        }
     
    }
    <!-- 设置全局的编码格式,针对get,post请求,默认编码为UTF-8 -->
      <filter>
          <filter-name>characterFilter</filter-name>
          <filter-class>com.mscncn.jsd1404.EncodingFilter</filter-class>
          <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>characterFilter</filter-name>
          <!-- 拦截所有的请求,为所有的请求设置编码 -->
          <url-pattern>/*</url-pattern>
      </filter-mapping>
  • 相关阅读:
    JS调用App方法及App调用JS方法
    提升用户体验之 选用合适的鼠标光标
    js仿QQ拖拽删除
    Linux下安装 mongodb
    QQ分享-定制分享卡片
    js判断浏览器语言实现网站国际化
    js复制内容到剪切板
    为什么会有堆内存和栈内存之分
    Avro实现RPC
    zookeeper学习day01
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/5390677.html
Copyright © 2020-2023  润新知