zoukankan      html  css  js  c++  java
  • tomcat设置编码格式utf8

    利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效!

    要设置GET的编码,可以修改server.xml文件中,相应的端口的Connector的属性:URIEncoding="UTF-8",这样,GET方式提交的数据才会被正确解码。

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

    tomcat8以后默认编码格式是utf-8;7之前的都是iso8859-1

    如果默认情况下,tomcat使用的的编码方式:iso8859-1

    修改tomcat下的conf/server.xml文件

    找到如下代码:    
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    这段代码规定了Tomcat监听HTTP请求的端口号等信息。

    可以在这里添加一个属性:URIEncoding,将该属性值设置为UTF-8,即可让Tomcat(默认ISO-8859-1编码)以UTF-8的编码处理get请求。

    修改完成后:

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

    form提交数据中文乱码问题总结

      一:form在前台以post方式提交数据:

        浏览器将数据(假设为“中国”)发送给服务器的时候,将数据变成0101的二进制数据(假设为98 99)时必然要查码表,浏览器以哪个码表打开网页,浏览器就以哪个码表提交数据。数据到达服务器后,数据(98 99)要封装到request中,在servlet中调用Request的getParameter方法返回的是字符串(“中国”),方法内部拿到数字后要转成字符,一定要查码表,由于request的设计者是外国人,所以默认查的是他们常用的ISO8859-1,这就是请求数据产生乱码的根源。

    复制代码
    复制代码
    package com.yyz.request;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    //以post方式提交表单
    public class RequestDemo extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
             //请求数据的中文乱码问题
            request.setCharacterEncoding("UTF-8");//客户端网页我们控制为UTF-8
            String username = request.getParameter("username");
            //获取数据正常,输出数据时可以查阅不同码表
            response.setCharacterEncoding("gb2312");//通知服务器发送数据时查阅的码表
            response.setContentType("text/html;charset=gb2312");//通知浏览器以何种码表打开
            PrintWriter out = response.getWriter();
            out.write(username);
    }  
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request,response);
    }
    
    }
    复制代码
    复制代码

    二:form在前台以get方式提交数据: 

       get方式提交的数据依然是浏览器用什么码表打开就用什么码表发送。不同的是,以get方式提交数据时,request设置编码无效。即使设置了UTF-8还是会去查ISO8859-1。得到(? ?),要解决这个问题,需要拿着(??)反向查ISO8859-1,拿到(98 99)后,再去查正确码表。

    复制代码
    复制代码
     1 package com.yyz.request;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 //以get方式提交表单
    11 public class RequestDemo extends HttpServlet {
    12 
    13     public void doGet(HttpServletRequest request, HttpServletResponse response)
    14             throws ServletException, IOException {
    15          //请求数据的中文乱码问题
    16         request.setCharacterEncoding("UTF-8");//以get方式提交数据时,request设置编码无效。即使设置了UTF-8还是会去查ISO8859-1
    17         String username = request.getParameter("username");
    18        System.out.println(username);
    19         byte source [] = username.getBytes("iso8859-1");
    20         username = new String (source,"UTF-8");
    21         System.out.println(username);
    22         
    23 }  
    24 
    25     public void doPost(HttpServletRequest request, HttpServletResponse response)
    26             throws ServletException, IOException {
    27         doGet(request,response);
    28 }
    29 
    30 }
    复制代码
    复制代码

    三:提交数据中文乱码问题总结:

    1.如果提交方式为post,想不乱码,只需要设置request对象的编码即可。

          注意:客户机数据是以哪种方式提交的,request就应该设成什么编码。

    2.如果提交方式为get,设置request对象的编码是无效的,想不乱码,只能手工转换。

         String data = "???????";//乱码字符串
         byte source [] = data.getBytes("iso8859-1");//得到客户机提交的原始数据
         data = new String (data.getBytes("iso8859-1"),"UTF-8");//解决乱码

         //等同于

        data = new String (source,"UTF-8");

    3.get方式的乱码,还可以通过更改服务器配置的方式实现。更改Tomact的conf目录下的server.xml文件。

          3.1    这种方式并不推荐,因为更改了服务器且并不灵活。

          3.2这么设置后,request的setCharacterEncoding设置什么编码,连接器就用什么编码,虽然比上一种更改灵活,但依然会导致我们的应用程序牢牢依赖于服务器,也不被推荐。

    四:最后的最后,提一个小细节:URL地址后面如果跟了中文数据,一定要经过URL编码。表单提交的参数有中文数据,浏览器会自动帮我们编码,但如果是通过链接直接带中文参数,浏览器是不会帮我们编码的,这时想通过上述第二种方式解决中文乱码问题就时灵时不灵了,应该通过URLEncoding.encode(,"UTF-8")先编码。

    新配置一个spring的MVC项目,发现对Get请求的中文参数出现了乱码:

    查看了SpingMVC中关于编码的配置(在web.xml中),如下

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <filter>  
    2.     <filter-name>encodingFilter</filter-name>  
    3.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    4.     <init-param>  
    5.         <param-name>encoding</param-name>  
    6.         <param-value>utf-8</param-value>  
    7.     </init-param>  
    8.     <init-param>  
    9.         <param-name>forceEncoding</param-name>  
    10.         <param-value>true</param-value>  
    11.     </init-param>  
    12. </filter>  
    13.   
    14. <filter-mapping>  
    15.     <filter-name>encodingFilter</filter-name>  
    16.     <servlet-name>appkit</servlet-name>  
    17. </filter-mapping>  



    应该不是Spring的问题,应该是Tomcat的问题,然后去修改Tomcat的配置文件server.xml,添加URIEncoding="UTF-8"

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <Connector port="8080" protocol="HTTP/1.1"  
    2.            connectionTimeout="20000"  
    3.            redirectPort="8443"  
    4.            URIEncoding="UTF-8"/>  

    然后就好了

  • 相关阅读:
    Swizzling的使用
    Kubelet
    Windows 下 LaTeX 手动安装宏包(package)以及生成帮助文档的整套流程
    Leetcode-Day Three
    吴裕雄--天生自然Linux操作系统:Linux 用户和用户组管理
    吴裕雄--天生自然Linux操作系统:Linux 文件与目录管理
    吴裕雄--天生自然Linux操作系统:Linux 文件基本属性
    吴裕雄--天生自然Linux操作系统:Linux 远程登录
    吴裕雄--天生自然Linux操作系统:Linux 忘记密码解决方法
    吴裕雄--天生自然Linux操作系统:Linux 系统目录结构
  • 原文地址:https://www.cnblogs.com/panxuejun/p/6837677.html
Copyright © 2011-2022 走看看