Servlet乱码问题解决
对于请求参数的编码处理基本上分为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乱码问题解决的更多相关文章
- Servlet中文乱码问题解决办法
首先对于源jsp网站和servlet里面的字符集要一样,一般支持中文的字符集为UTF-8最好采用这个字符集(除此之外还有gb2312); 对于源jsp文件的代码中需要设置 设置你的page里面的字符集 ...
- Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决
servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...
- Servlet中response、request乱码问题解决
Java Web(二) Servlet中response.request乱码问题解决 三月不减肥,五月徒伤悲,这就是我现在的状态,哈哈~ 健身.博客坚持. --WH 一.request请求参数出现 ...
- Java Web之Servlet中response、request乱码问题解决
Java Web之Servlet中response.request乱码问题解决 一.request请求参数出现的乱码问题 get请求: get请求的参数是在url后面提交过来的,也就是在请求行中, ...
- Spring MVC3返回JSON数据中文乱码问题解决(转)
Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用两种很简单的办法解决了中文乱码问题. Spring版本:3.2.2.RELEASE Jack ...
- spring mvc json 返回乱码问题解决(vestion:3.x.x)
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...
- WingIDE中文乱码问题解决方法
WingIDE中文乱码问题解决方法 安装完WingIDE后,首次运行python脚本时,若脚本中含有UTF-8中文,在Debug I/O输出框中,全部变成了乱码. 这时其实我们设置下WingIDE的编 ...
- servlet乱码问题总结
在学习时servlet乱码问题还是挺严重的,总结一下有三种情况 1.新建HTML页面后浏览出现乱码 2.以post形式请求时出现乱码 3.以get形式请求时出现乱码 让我们一个一个来解决吧 1.新建H ...
- ASP 编码转换(乱码问题解决)
ASP 编码转换(乱码问题解决) 输出前先调用Conversion函数进行编码转换,可以解决乱码问题. 注,“&参数&”为ASP的连接符,这里面很多是直接调用的数据库表字段,实际使用请 ...
随机推荐
- 2018铁人三项测评题 IOS99
下面这一部分是我从网上复制过来的, 2.IOS 解题链接:http://ctf4.shiyanbar.com/web/IOS/index.php 这题页面中提示系统升级到了IOS99,我们可以想到修改 ...
- nginx 负载均衡及反向代理
Nginx简介 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师开发,官方测试nginx能够支支撑5万并发链接,并且cpu.内存 ...
- creator 2.0版本对于preloadScene函数获取加载进度
有时候,当我们场景上挂载的资源过多时,我们使用cc.director.loadScene切换场景时会等一段时间才会切换过去,这对游戏的体验是相当不好的.所以我们可以使用cc.director.prel ...
- Spring HTTP invoker简介
Spring HTTP invoker简介 Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制 ...
- APICloud打开三方地图整合
一直想系统的整理打开地图的方法,今天抽时间把了百度,高德,腾讯,苹果自带地图都整理出来了,闲话不多说,直接上干货 ------------------------------------------- ...
- C#中的委托是什么
1.什么是委托?(方法作另一个方法的参数)delegate void MyDel(int value); //声明委托类型和类一样,委托是用户自定义的类型,但是类是数据和方法的集合,而委托是持有 ...
- ELK学习实验018:filebeat收集docker日志
Filebeat收集Docker日志 1 安装docker [root@node4 ~]# yum install -y yum-utils device-mapper-persistent-data ...
- Scrum.站立会议介绍
项目任务分解完毕之后,整个项目要完成的任务也都已经确定,每个人负责的任务也确定.这时候就进入到每天的迭代过程.项目经理的一个职责就是每天负责召开 站立会议. 具体的形式如下: 每天固定时间召开. 项目 ...
- Java电商支付系统实战(一)- 简介
现如今,支付成为热点 对于电商业务,这都是不可或缺的 核心功能剖析 下单->支付 nginx 将用户请求反向代理到我们编写的电商系统 = 下单 之后,点击支付跳转到支付系统,最后对接 通过跳转将 ...
- 第二阶段冲刺个人任务——four
今日任务: 优化统计团队博客结果界面的显示. 昨日成果: 优化统计个人博客结果页面的显示.