对于请求参数的编码处理基本上分为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. # Okhttp解析—Interceptor详解

    Okhttp解析-Interceptor详解 Interceptor可以说是okhttp的精髓之一,Okhttp重写请求/响应.重试.缓存响应等操作,基本都是在各个Interceptor中完成的,上篇 ...

  2. DLC双端锁,CAS,ABA问题

    一.什么是DLC双端锁?有什么用处? 为了解决在多线程模式下,高并发的环境中,唯一确保单例模式只能生成一个实例 多线程环境中,单例模式会因为指令重排和线程竞争的原因会出现多个对象 public cla ...

  3. Java扫描指定文件路径下的文件并且递归扫描其子目录下的所有文件

    本文主要实现了扫描指定文件路径下的文件,递归扫描其子目录下的所有文件信息,示例文件为: 要求将后缀为.dat的文件夹信息也写入到数据库中,然后将.chk文件解析,将文件中对应的内容读出来写入到数据库, ...

  4. 【转】Spring面试问题集锦

    Q. 对于依赖倒置原则(Dependency Inversion Principle,DIP),依赖注入(Dependency Injection,DI)和控制反转(Inversion of Cont ...

  5. mongodb的文本搜索

    1.当mongodb进程文本搜索的时候, 一个collection,只有一个文本查询的索引. 2.全文索引的定义,搜索的是有意义的词,不是字母 一开始是简单的用中文姓名的姓,如:张,来查询,但是无效, ...

  6. 唬人的Java泛型并不难

    泛型 public interface Foo<E> {}public interface Bar<T> {}public interface Zar<?> {} ...

  7. 动态内存分配(C++)

    C++中的动态内存分配 C++中通过new关键字进行动态内存分配 C++中的动态内存申请是基于类型进行的 delet关键字用于内存释放 //变量申请 Type*pointer = new Type; ...

  8. 搭建自己的Online Judge

    前言 很多人对于做题有点厌烦,但是,如果让你出题给别人做那么可能会很有意思.可是,出题只能出在一些别人的OJ上,甚至只能在自己的Word文档里出.今天我教大家一个厉害点的,叫做搭建自己的Online ...

  9. 四、JVM之栈与栈帧

    栈: 1.又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把 另一端称为栈底.其特性是先进后出. 2.栈是线程私有的,生命周期跟线程相同,当创 ...

  10. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...