对于请求参数的编码处理基本上分为get和post两种情况。

1、POST

index.html

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTML范例</title>
</head>
<body>
<form method="post" action="hello.do" name="sample">
姓名:
<input type="text" name="name">
<br />
<button>发送</button>
</form>
</body>
</html>

如果客户端没有在Content-Type标头中设置字符编码信息,此时使用HttpServletRequest的getCharacterEncoding返回值是null。

在这个情况下,容器若使用默认的编码ISO-8859-1,而客户端使用UTF-8发送非ASCII字符的请求,Servlet直接使用getParameter等方法

取得请求参数值,就会是不正确的结果也就是乱码。

解决办法:

在取得请求参数前添加:

request.setCharacterEncoding("UTF-8");

String name = request.getParameter("name");

 @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>post</title>");
out.println("</head>");
out.println("<body>");
//取得请求参数
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
//输出字符编码信息
out.println(request.getCharacterEncoding());
out.println("</body>");
out.println("<h1>您的姓名是:" + name + "</h1>");
out.println("</html>");
out.close();
}

2、GET

index.html

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTML范例</title>
</head>
<body>
<form method="get" action="hello.do" name="sample">
姓名:
<input type="text" name="name">
<br />
<button>发送</button>
</form>
</body>
</html>

利用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"/>

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容格式
response.setContentType("text/html;charset=utf-8");
//取得输出对象
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>get</title>");
out.println("</head>");
out.println("<body>");
//取得请求参数
//request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
out.println("</body>");
out.println("<h1>您的姓名是:" + name + "</h1>");
out.println("</html>");
out.close();
}

或者

<Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" 

  useBodyEncodingForURI="true"

/>

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容格式
response.setContentType("text/html;charset=utf-8");
//取得输出对象
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>get</title>");
out.println("</head>");
out.println("<body>");
//取得请求参数
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
out.println("</body>");
out.println("<h1>您的姓名是:" + name + "</h1>");
out.println("</html>");
out.close();
}

又或者(不用去修改server.xml):

String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");

完整测试用例:

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; /**
* Created by N3verL4nd on 2017/1/4.
*/
@WebServlet("/hello.do")
public class HelloServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>post</title>");
out.println("</head>");
out.println("<body>");
//取得请求参数
request.setCharacterEncoding("UTF-8");
String usr = request.getParameter("usr");
String psd = request.getParameter("psd");
//输出字符编码信息
//out.println(request.getCharacterEncoding());
out.println("<h1>账号:" + usr + "<br />密码:" + psd + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容格式
response.setContentType("text/html;charset=utf-8");
//取得输出对象
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>get</title>");
out.println("</head>");
out.println("<body>");
//取得请求参数
request.setCharacterEncoding("UTF-8");
String usr = request.getParameter("usr");
String psd = request.getParameter("psd");
usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");
out.println("<h1>账号:" + usr + "<br />密码:" + psd + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}

request.setCharacterEncoding("UTF-8");
String usr = request.getParameter("usr");
String psd = request.getParameter("psd");
usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");

当把

request.setCharacterEncoding("UTF-8");

改为:

request.setCharacterEncoding("ISO-8859-1");

或者删除(Tomcat容器默认编码为ISO-8859-1),则显示正确。

这对于get/post都是一样的。

两种方法的区别:

对于 URL 提交的数据和表单中 GET 方式提交的数据,在接收数据的 JSP 中设置 request.setCharacterEncoding 参数是不行的,因为在 Tomcat5.0 中,默认情况下使用ISO-8859-1 对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码(解码),而不使用该参数对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码(解码)。要解决该问题,应该在 Tomcat 的配置文件的 Connector
标签中设置useBodyEncodingForURI 或者 URIEncoding 属性,其中 useBodyEncodingForURI 参数表示是否用 request.setCharacterEncoding 参数对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码,在默认情况下,该参数为 false (Tomcat4.0 中该参数默认为true );URIEncoding 参数指定对所有 GET 方式请求(包括 URL 提交的数据和表单中 GET 方式提交的数据)进行统一的重新编码(解码)的编码。URIEncoding
和 useBodyEncodingForURI 区别是,URIEncoding 是对所有 GET 方式的请求的数据进行统一的重新编码(解码),而 useBodyEncodingForURI 则是根据响应该请求的页面的request.setCharacterEncoding 参数对数据进行的重新编码(解码),不同的页面可以有不同的重新编码(解码)的编码。所以对于 URL 提交的数据和表单中 GET 方式提交的数据,可以修改 URIEncoding 参数为浏览器编码或者修改 useBodyEncodingForURI
为true ,并且在获得数据的 JSP 页面中 request.setCharacterEncoding参数设置成浏览器编码。

http://www.cnblogs.com/x_wukong/p/3651853.html?utm_source=tuicool

http://zhuhuide2004.iteye.com/blog/562739

Servlet乱码问题解决的更多相关文章

  1. Servlet中文乱码问题解决办法

    首先对于源jsp网站和servlet里面的字符集要一样,一般支持中文的字符集为UTF-8最好采用这个字符集(除此之外还有gb2312); 对于源jsp文件的代码中需要设置 设置你的page里面的字符集 ...

  2. Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决

    servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...

  3. Servlet中response、request乱码问题解决

    Java Web(二) Servlet中response.request乱码问题解决   三月不减肥,五月徒伤悲,这就是我现在的状态,哈哈~ 健身.博客坚持. --WH 一.request请求参数出现 ...

  4. Java Web之Servlet中response、request乱码问题解决

    Java Web之Servlet中response.request乱码问题解决   一.request请求参数出现的乱码问题 get请求: get请求的参数是在url后面提交过来的,也就是在请求行中, ...

  5. Spring MVC3返回JSON数据中文乱码问题解决(转)

    Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用两种很简单的办法解决了中文乱码问题. Spring版本:3.2.2.RELEASE Jack ...

  6. spring mvc json 返回乱码问题解决(vestion:3.x.x)

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...

  7. WingIDE中文乱码问题解决方法

    WingIDE中文乱码问题解决方法 安装完WingIDE后,首次运行python脚本时,若脚本中含有UTF-8中文,在Debug I/O输出框中,全部变成了乱码. 这时其实我们设置下WingIDE的编 ...

  8. servlet乱码问题总结

    在学习时servlet乱码问题还是挺严重的,总结一下有三种情况 1.新建HTML页面后浏览出现乱码 2.以post形式请求时出现乱码 3.以get形式请求时出现乱码 让我们一个一个来解决吧 1.新建H ...

  9. ASP 编码转换(乱码问题解决)

    ASP 编码转换(乱码问题解决) 输出前先调用Conversion函数进行编码转换,可以解决乱码问题. 注,“&参数&”为ASP的连接符,这里面很多是直接调用的数据库表字段,实际使用请 ...

随机推荐

  1. $[NOIp2008]$双栈排序 栈/二分图/贪心

    \(Sol\) 先考虑单栈排序,怎么样的序列可以单栈排序呢?设\(a_i\)表示位置\(i\)是哪个数.\(\exist i<j<k\),都没有\(a_k<a_i<a_j\), ...

  2. IntelliJ IDEA安装教程及使用方法

    IntelliJ IDEA安装教程及使用方法 首先,需要去下载IDEA,直接在百度搜索IntelliJ IDEA,然后找到官网点击进入 进入官网,选择[Download],点击进入,选择自己的操作系统 ...

  3. 小白学 Python 爬虫(37):爬虫框架 Scrapy 入门基础(五) Spider Middleware

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  4. CTPN训练自己的数据集过程大白话记录

    一.算法理解 此处省略1万字.............. 二.训练及源码理解 配置以下3步: 在utils文件夹和utils\bbox文件夹下创建__init__.py文件 在utils\bbox文件 ...

  5. Java 遍历集合时产生的ConcurrentModificationException异常

    前几天做Java实验的时候,打算用foreach遍历一个ArrayList集合,并且当集合中的某个元素符合某个值时删除这个元素.写完运行时抛出了ConcurrentModificationExcept ...

  6. DP- 01背包问题

    这个01背包 , 理解了一天才勉强懂点 , 写个博客  (  推荐   http://blog.csdn.net/insistgogo/article/details/8579597) 题目 : 有N ...

  7. SQLiteOpenHelperde的oncreate方法

    main中

  8. 「2.0」一个人开发一个App,小程序从0到1,文件剖析

    不知你是不是见到“文件剖析”这4个大字,才点进来看一看的?如果真是的话,那我可以坦诚.真心.负责任地告诉你:你上当了,你上了贼船啦,如果你现在想跳的话,还来得及,反正茫茫大海中,鲨鱼正缺搞程序的人.说 ...

  9. unbuntu18.04安装启用splash

    官网:https://splash.readthedocs.io/en/stable/ 1.安装Docker https://www.cnblogs.com/wt7018/p/11880666.htm ...

  10. Flask DBUtils

    作用:创建连接池,解决多线程问题 1.安装模块 pip3 install -i https://pypi.douban.com/simple DBUtils 2.settings.py(配置文件) f ...