zoukankan      html  css  js  c++  java
  • JAVA WEB 解决编码问题的filter

    最近使用Springmvc,GET传输参数的时候,经常出错误,下面介绍添加一个Filter的方法,可以解决这个问题。既支持POST方式也支持GET方式。

    首先写一个Filter

    package com.qunar.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;
    
    public class CharacterEncodingFilter implements Filter {
        private String encode = "UTF-8";// 默认UTF-8编码
    
        public void init(FilterConfig filterConfig) throws ServletException {
            String encoding = filterConfig.getInitParameter("encode");
            if (encoding != null) {
                encode = encoding;
            }
        }
    
        public void doFilter(ServletRequest req, ServletResponse resp,
                FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
            // 设置request编码
            request.setCharacterEncoding(encode);
            // 设置相应信息
            response.setContentType("text/html;charset=" + encode);
            response.setCharacterEncoding(encode);
            chain.doFilter(new CharacterEncodingRequest(request), response);
        }
    
        public void destroy() {
    
        }
    
    }
    
    /**
     * 对Get方式传递的请求参数进行编码
     */
    class CharacterEncodingRequest extends HttpServletRequestWrapper {
        private HttpServletRequest request = null;
    
        public CharacterEncodingRequest(HttpServletRequest request) {
            super(request);
            this.request = request;
        }
    
        /**
         * 对参数重新编码
         */
        @Override
        public String getParameter(String name) {
            String value = super.getParameter(name);
             if(value == null)
                   return null;
             String method = request.getMethod();
            if ("get".equalsIgnoreCase(method)) {
                try {
                    value = new String(value.getBytes("ISO8859-1"), request.getCharacterEncoding());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return value;
        }
    
    }

    然后在xml里面添加这个filter

    <filter>
          <filter-name>characterEncodingFilter</filter-name>
          <filter-class>com.qunar.filter.CharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>encode</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>characterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

    当然有一些人为了单纯解决GET的编码问题,修改整个站点的编码,直接修改Connector也可以。

    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
  • 相关阅读:
    day4递归原理及实现
    day4装饰器
    day4迭代器&生成器&正则表达式
    open()函数文件操作
    Python中的内置函数
    function(函数)中的动态参数
    copy深浅拷贝
    collections模块
    set集合
    字典dict常用方法
  • 原文地址:https://www.cnblogs.com/liqiu/p/3796983.html
Copyright © 2011-2022 走看看