Get请求,Post请求乱码问题解决方案
下面以两种常见的请求方式为例讲解乱码问题的解决方法。
1.Post方式请求乱码。
自从Tomcat5.x以来,Get方式和Post方式提交的请求,tomcat会采用不同的方式来处理编码。
对于Post请求,Tomcat会使用request.setCharacterEncoding和response.setCharacterEncoding方法设置的编码格式进行处理。
如果未设置,则默认都采用iso-8859-1编码。因此如果在发送Post请求的时候出现乱码,常见的解决方法如下:
a) request.setCharacterEncoding("utf-8");
b) String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
(当然response的编码也不要忘记设置成utf-8的)
【注】:上面提供的是两种不同的方法,并不是一种方法的两个步骤!!!
我们可以看出来第一种方式写法固定,非常很适合通过写成过滤器的方式进行编码的统一转换。
- publicclass EncodingFilter extends HttpFilter {
- publicvoid doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
- Stringcharset = this.getInitParameter("charset");
- if(charset == null ||charset.isEmpty()) {
- charset ="UTF-8";
- }
- request.setCharacterEncoding(charset);
- response.setContentType("text/html;charset=" + charset);
- chain.doFilter(request, response);
- }
- }
2.Get方式请求乱码
我们刚谈到Tomcat对Post请求的编码处理策略,并且从字里行间读出了Tomcat对Post和Get的编码策略有所不同,
那么Tomcat对Get请求的编码策略到底是如何不同的呢?答案就是Tomcat对于Get请求并不会考虑使用request.setCharacterEncoding方法设置的编码
言外之意就是我们无法通过request.setCharacterEncoding方式来改变编码,因为最终Tomcat会永远使用iso-8859-1进行编码(但是要注意一下,
respon还是可以通过调用response.setCharacterEncoding方式进行编码的设置的),既然是这样的话下面这个方法还是勉强可行的
a)
步骤一:response.setCharacterEncoding("UTF-8")
步骤二:String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
通过这两步,可以看到我们的request的字符编码格式是默认的“iso-8859-1" 我们的response字符编码是自己设置的”utf-8"
显然两个是不相容的,因此我们在获取请求参数的时候才进行了一下字符集的转换即步骤二:
String name=new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
既然有a),那肯定还有b)咯?当然!而且方案b更加干净,利落,彻底。让我们一起看一下吧:
b)
修改Tomcat安装目录下conf子目录下的server.xml文件(解决get方式) 【注:记得先停掉服务器】
找到文件中类似这样的段落:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
在里面加上这样一段:URIEncoding="UTF-8" 即变成下面这段
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />
最后重启Tomcat即可。
对于Get请求编码的统一处理也可以写成过滤器的形式,只不过稍微有些复杂。
EncodingRequest.java
- public class EncodingRequest extends HttpServletRequestWrapper {
- private String charset;
- public EncodingRequest(HttpServletRequest request, String charset) {
- super(request);
- this.charset = charset;
- }
- public String getParameter(String name) {
- HttpServletRequest request = (HttpServletRequest) getRequest();
- String method = request.getMethod();
- if(method.equalsIgnoreCase("post")) {
- try {
- request.setCharacterEncoding(charset);
- } catch (UnsupportedEncodingException e) {}
- } else if(method.equalsIgnoreCase("get")) {
- String value = request.getParameter(name);
- try {
- value = new String(name.getBytes("ISO-8859-1"), charset);
- } catch (UnsupportedEncodingException e) {
- }
- return value;
- }
- return request.getParameter(name);
- }
- }
EncodingFilter.java
- public class EncodingFilter extends HttpFilter {
- public void doFilter(HttpServletRequest request,
- HttpServletResponse response, FilterChain chain)
- throws IOException, ServletException {
- String charset = this.getInitParameter("charset");
- if(charset == null || charset.isEmpty()) {
- charset = "UTF-8";
- }
- response.setCharacterEncoding(charset);
- response.setContentType("text/html;charset=" + charset);
- EncodingRequest res = new EncodingRequest(request, charset);
- chain.doFilter(res, response);
- }
- }
web.xml
- <filter>
- <filter-name>EncodingFilter</filter-name>
- <filter-class>cn.itcast.filter.EncodingFilter</filter-class>
- <init-param>
- <param-name>charset</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>EncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
【注】为了避免您的测试与我给的解决方案结果不符,请在项目编码格式为utf-8,jsp页面 pageEncoding="utf-8"的环境下测试。
Get请求,Post请求乱码问题解决方案的更多相关文章
- java web项目get,post请求参数中文乱码解决
[转载]原文地址:https://www.cnblogs.com/tom-plus/p/6392279.html 在开发过程中,有时候会碰到get,post请求参数中文乱码. 原因: Http请求传输 ...
- SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...
- [转]解决GET请求时中文乱码的问题
原文地址:http://www.cnblogs.com/liukemng/p/4178882.html 之前项目中的web.xml中的编码设置: <filter> <filter-n ...
- Ajax详解及其案例分析------如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表
本节主要内容预览: 1 获得Ajax对象 2 使用Ajax对象发送GET请求 3 使用Ajax对象发送POST请求 4 使用Ajax校验用户名 5 POST请求时的乱码处理 6 GET请求时的乱码处理 ...
- JSP中解决获取请求参数中文乱码问题
分两种情况: 1.获取访问请求参数时乱码 解决方法:构造一个新的String String user = new String(request.getParameter("user" ...
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
前述: 在写这篇笔记之前,对笔记中的设计模式进行介绍: 本篇笔记中将要使用到的设计模式是:装饰(包装)设计模式 (1)装饰(包装)设计模式口诀: ...
- 详解get请求和post请求参数中文乱码的解决办法
首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...
- post和get请求的参数乱码
对于做Java WEB项目同学来说,中文乱码问题是一个经常遇到而又非常头痛的问题,而最容易出现乱码的环节就是在浏览器向服务器发送请求的过程,至于出现乱码的原因不是本文的关注的重点,想了解的朋友可以参考 ...
- tomcat解决GET请求中文参数乱码
通常,在使用Spring MVC框架的应用程序中,为了解决中文参数乱码的问题,都会添加如下过滤器配置: <filter> <filter-name>encodingFilter ...
随机推荐
- python Polygon模块安装
pip install Polygon这样会安装不了 只能使用pip install Polygon2 或者 pip install Polygon3,也就是必须带版本号
- PAT A1012 The Best Rank (25 分)——多次排序,排名
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- python关联eureka实现高并发
弥补网上python关联微服务空白,结合多个pythonweb框架实践,找到一个可行的方案python加入到eureka,实现java和python的完美结合 # coding:utf- import ...
- python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)
昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...
- MySQL 基础三 函数(聚合、字符串、时间、条件判断)
1.聚合 其它:GROUP_CONCAT.avg.sum.count.max.min SELECT typeid,GROUP_CONCAT(goodsname) FROM `goods` GROUP ...
- Zephyr的Power Management
1 关于Zephyr Zephyr是Linux基金会维护的微内核项目,来源于WindRiver向Zephyr捐赠的Rocket RTOS内核.主要用于开发针对物联网设备的实时操作系统. Zephyr操 ...
- 关于PCB开窗
如果走220V,那么线宽一点,一般高电压下面不覆铜 https://blog.csdn.net/zhy295006359/article/details/77412566 假设感觉需要走大电流,那么就 ...
- 【第196期】Drupal7 Features模块与 Drupal8 Configuration Management 模块对比
Drupal 8 最好和最受欢迎的部分之一是新的配置管理系统. 该系统使开发人员很容易将配置导出到代码中.在此之前,开发人员不得不依赖于由Features.Strongarm.UUID.Feature ...
- Java 中 LinkedList 和 ArrayList 的区别
引自:https://www.cnblogs.com/huzi007/p/5550440.html ArrayList和LinkedList的大致区别如下:1.ArrayList是实现了基于动态数组的 ...
- 安卓自动化测试案例(跑在MonkeyRunner上)
首先文件所在目录: MonkeyRunner所在目录: 运行命令(通过cd 命令 进入Tools目录下): 运行脚本:monkeyrunner.bat ..\honeywell\jsq.py 源文件 ...