zoukankan      html  css  js  c++  java
  • 关于Tomcat中接收Get与Post请求编码格式与乱码相关问题

    我发现最近我是被各种乱码搞到怀疑人生。。。今天在学习请求转发的时候偶然研究了一哈Tomcat服务器URL的默认编码格式与Gett与Post请求的相关联系。。。

    Tomcat将使用ISO-8859-1作为整个URL的默认字符编码

    web项目使用Tomcat作为服务器时,会遇到GET或者POST请求乱码的问题。

    、GET请求乱码

    由于GET请求是将参数拼接到URL上向服务器发起请求的。在Tomcat接收到请求时,会先将URL解码。而默认的编码方式为"ISO-8859-1",解码后自然会产生乱码。

    解决方法:(server.xml文件在Tomcat安装目录的conf目录下。。。Tomcat//conf//server.xml)

    1、将server.xml中的<Connector>元素上的URIEncoding属性设置为特定的值(例如,URIEncoding="UTF-8")。

    2、将服务器server.xml中的<Connector>元素上的useBodyEncodingForURI属性设置为true。

    同时注意响应编码格式的设置

    二、POST请求乱码

    关于POST请求方式乱码的问题,这个比较。。。无解一点。。。我遇到的乱码问题是通过对前台数据进行重新编码解决的。。

    遇到POST请求乱码的码友们可以借鉴一哈。。。

    设置完响应编码格式,获取请求信息之后,对获取到的前台数据用String进行重新编码即可。。。

    关于Tomcat服务器GET与POST请求乱码问题还有官方文档可以借鉴!!!

    Tomcat will use ISO-8859-1 as the default character encoding of the entire URL, including the query string ("GET parameters") (though see Tomcat 8 notice below).
    There are two ways to specify how GET parameters are interpreted:

    Set the URIEncoding attribute on the <Connector> element in server.xml to something specific (e.g. URIEncoding="UTF-8").

    Set the useBodyEncodingForURI attribute on the <Connector> element in server.xml to true. This will cause the Connector to use the request
     body's encoding for GET parameters.
    In Tomcat 8 starting with 8.0.0 (8.0.0-RC3, to be specific), the default value of URIEncoding attribute on the <Connector> element depends on "strict servlet compliance"
     setting. The default value (strict compliance is off) ofURIEncoding is now UTF-8. If "strict servlet compliance" is enabled, the default value is ISO-8859-1.

    POST requests should specify the encoding of the parameters and values they send. Since many clients fail to set an explicit encoding, the default is used (ISO-8859-1).


    官方建议:

    Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.
    In order to completely switch to using UTF-8, you need to make the following changes:

    Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP
     Connector, AJP Connector.

    Use a character encoding filter with the default
     encoding set to UTF-8
    Change all your JSPs to include charset name in their contentType.
    For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and <jsp:directive.page contentType="text/html; charset=UTF-8" /> for the pages in
     XML syntax (aka JSP Documents).
    Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.
    Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").
    Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.
    Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see
     

  • 相关阅读:
    webstorm11.0下载地址和webstorm11.0破解程序patcher.exe下载使用方法说明 前端IDE工具的利器
    20151224今天发现到的两篇关于CSS架构、可复用可维护CSS和CSS学习提升能有改变思想观念意识的文章 分别是CSS架构目标和说说CSS学习中的瓶颈
    GOF提出的23种设计模式是哪些 设计模式有创建形、行为形、结构形三种类别 常用的Javascript中常用设计模式的其中17种 详解设计模式六大原则
    HTML过滤器,用于去除XSS漏洞隐患。
    springboot的快速集成多数据源的启动器
    Springboot根据url后缀返回json或者xml或者html
    Springboot打包成War包并使其可以部署到Tomcat中直接运行
    数据脱敏工具类(包含手机号,银行卡号,邮箱,中文名称等)
    MySQL函数find_in_set介绍
    防止同一IP多次请求攻击
  • 原文地址:https://www.cnblogs.com/itjiangpo/p/14181253.html
Copyright © 2011-2022 走看看