• [技巧篇]12.从Spring的编码过滤器说起


    有一枚学生问问了我一个问题,突然灵感爆发,他使用的Spring的过滤器,前台利用GET方式向后端发出一个请求,由于里面含有中文数据,结果在后端显示的是乱码,他问我为什么?明明在Spring里面也配了字符过滤器,却出现了乱码,所以就看了一下spring实现的该过滤器,下面是过滤器的实现代码org.springframework.web.filter.CharacterEncodingFilter.java

    public class CharacterEncodingFilter extends OncePerRequestFilter {
    
        private String encoding;
    
        private boolean forceEncoding = false;
    
    
        /**
         * Set the encoding to use for requests. This encoding will be passed into a
         * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
         * <p>Whether this encoding will override existing request encodings
         * (and whether it will be applied as default response encoding as well)
         * depends on the {@link #setForceEncoding "forceEncoding"} flag.
         */
        public void setEncoding(String encoding) {
            this.encoding = encoding;
        }
    
        /**
         * Set whether the configured {@link #setEncoding encoding} of this filter
         * is supposed to override existing request and response encodings.
         * <p>Default is "false", i.e. do not modify the encoding if
         * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
         * returns a non-null value. Switch this to "true" to enforce the specified
         * encoding in any case, applying it as default response encoding as well.
         */
        public void setForceEncoding(boolean forceEncoding) {
            this.forceEncoding = forceEncoding;
        }
    
    
        @Override
        protected void doFilterInternal(
                HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
    
            if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
                request.setCharacterEncoding(this.encoding);  //此处设置是处理POST方式的编码参数问题
                if (this.forceEncoding) {
                    response.setCharacterEncoding(this.encoding);
                }
            }
            filterChain.doFilter(request, response);
        }
    
    }

    在web.xml该过滤器是这样配置的:需要设置的两个参数为encoding、forceEncoding,分别设置字符集及是否设置字符集,该filter也非常简单

        <filter>
            <filter-name>characterEncodingFilter</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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>

    有的时候,看到源码才知道一些真理,所有才知道spring只是利用request.setCharacterEncoding(this.encoding);帮助我们处理了POST方式的乱码问题,碰到GET方式的提交,还是会出现乱码。


    注意:自从Tomcat5.x开始,就对GET方式和POST方式的提交分别给予不同的处理方式[所以在二阶段学习的时候就应该严格区分get和post请求的处理情况,养成良好的习惯,想想是否做的到]。POST方式是利用request.setCharacterEncoding()来进行设置编码,如果没有设置的话,就是按照默认的ISO-8859-1来进行编码GET方式提交总是利用默认的ISO-8859-1来进行编码参数


    中文乱码解决方案


    1.利用String[也是最常用的方式]

    String username = new String(username.getBytes("ISO-8859-1"), "UTF-8"); //通过默认的编码获取到byte[],然后进行UTF-8再次编码  

    2.在tomcat中的server.xml进行配置URIEncoding="UTF-8"

    <Connector URIEncoding="UTF-8" port="8080" protocol="HTTP/1.1"  
                     connectionTimeout="20000"  
                     redirectPort="8443" /> 

    3.使用JavaScript对传递的参数进行编码

    额外阅读

    Js编码的几种方式区别:

    1.window.escape()与HttpUtility.UrlEncodeUnicode()编码格式一样:将一个汉字编码为%uxxxx格式
    不会被window.escape编码的字符有:@ _ - . * / +  这与http://www.w3school.com.cn/js/jsref_escape.asp上的解释不符合

    2.window.encodeURIComponent()[我推荐使用这种方式]与HttpUtility.UrlEncode()编码格式一样:将一个汉字编码为%xx%xx%xx的格式

    不会被window.encodeURIComponent编码的字符有:'  (  )  *  -  . _   ! ~   这与http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp解释相符合

    不会被HttpUtility.UrlEncode编码的字符有:'  (  )  *  -  .  _  ! 相比较而言,HttpUtility.UrlEncode比window.encodeURIComponent多一个 ~ 编码

    3.不会被window.encodeURI编码的字符有: -  _  .  !  * (  )  ;  /  ?  :  @  &  =  $  ,  #,与encodeURIComponent对比,发现encodeURI不对:;/?:@&=+$,#这些用于分隔 URI 组件的标点符号进行编码

    第一种: 事例演示说明
    JavaScript代码:
    window.self.location="searchbytext.action?searchtext="+encodeURIComponent(encodeURIComponent(seartext));
    
    java后台处理代码:
    
    searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8");
    
    /*
    
    为什么要两次编码的原因:后台java代码给searchtext赋值的时候,本身已经使用了一次解码,不过解码的结果依然不对。所以我们可以在页面上进行两次编码操作,这样后台自动的那次就可以抵消掉一次,然后在使用searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8");进行一次解码就好了。【这种方式还是用的比较多的,我个人使用的比较少】
    */

    我个人来说还是比较喜欢第一种情况,但是有的时候服务器有兼容问题,我曾经就遇到在tomcat中好用,放置到Weblogic后不好使了,之后我就只用第三种方式,对于第三种方式我不过多解释,希望大家有印象

    我写了这么多,为什么你们总是不回复我呢!<( ̄▽ ̄)> 哇哈哈…!

     

  • 相关阅读:
    第一次个人编程作业
    第一次博客作业
    动态规划 01背包学习中
    学习dijk最短路径中
    蓝桥杯 小盆友排队
    蓝桥杯 地宫取宝
    简单的BFS学习笔记
    C趣味100道之58.拉丁方的一些想法。
    蓝桥杯 错误票据--!偶然间发现,然后呵呵!
    函数实现计算标准差
  • 原文地址:https://www.cnblogs.com/pangxiansheng/p/4723330.html
Copyright © 2020-2023  润新知