上面写了console的乱码问题,接下来写的是web中servlet中的问题,大楷我比较关心一点,因为遇到这个的情况多一些吧。直接开始吧。
2. jsp和servlet中的乱码问题
分析:
a. 其实在java文件的编译的情况和(一)中的情况是一样的,不过这里是由WEB容器去调用JVM而已,那么我们得知道一些默认的东西
比如特别重要的:(摘要)
如果Servlet 在运行的过程中,需要接受从客户端传来的字符如:表单输入的值和URL中传入的值,此时如果程序中没有设定接受参数时采用的编码格式,则WEB 容器会默认采用ISO-8859-1 编码格式来接受传入的值并在JVM 中转化为UNICODE 格式的保存在WEB 容器的内存中。Servlet 运行后生成输出,输出的字符串是UNICODE 格式的,紧接着,容器将Servlet 运行产生的UNICODE 格式的串(如html语法,用户输出的串等)直接发送到客户端浏览器上并输出给用户,如果此时指定了发送时输出的编码格式,则按指定的编码格式输出到浏览器上,如果没有指定,则默认按ISO-8859-1 编码发送到客户的浏览器上。
注意是ISO-8859-1就行了,tomcat 5.0之前采用是由用户设置的编码方式解析,tomcat 5.0过后有个参数(useBodyEncodingForURI)被默认了false,就使用了ISO-8859-1解析了,这儿是配置中的关键。
测试代码如下:
Servlet类(注释说明)
01
package com.test.one;
02
03
import java.io.IOException;
04
import java.io.PrintWriter;
05
06
import javax.servlet.ServletException;
07
import javax.servlet.http.HttpServlet;
08
import javax.servlet.http.HttpServletRequest;
09
import javax.servlet.http.HttpServletResponse;
10
11
public class Helloextends HttpServlet {
12
13
private static final long serialVersionUID = 4878915372815719687L;
14
15
public Hello() {
16
super();
17
}
18
19
public void destroy() {
20
super.destroy();// Just puts "destroy" string in log
21
}
22
23
public void doGet(HttpServletRequest request, HttpServletResponse response)
24
throws ServletException, IOException {
25
//输入:设置请求编码格式
26
request.setCharacterEncoding("GBK");
27
//输出:设置响应编码格式
28
response.setContentType("text/html; charset=GBK");
29
30
PrintWriter out = response.getWriter();
31
out.write("
");
32
out.write("Hello, 中文!");
33
out.write("
");
34
}
35
36
public void doPost(HttpServletRequest request, HttpServletResponse response)
37
throws ServletException, IOException {
38
//输入:设置请求编码格式
39
request.setCharacterEncoding("GBK");
40
//输出:设置响应编码格式
41
response.setContentType("text/html; charset=GBK");
42
43
//从请求中接收参数
44
String input_str = request.getParameter("input_str");
45
String url_arg = request.getParameter("url_arg");
46
47
//出错处理
48
input_str = (null == input_str) ?"" : input_str;
49
url_arg = (null == url_arg) ?"" : url_arg;
50
51
PrintWriter out = response.getWriter();
52
out.write("
");
53
out.println("您输入的字符串是:" + input_str);
54
out.write("
");
55
// out.println("您的表单传递的URL参数是:" + new String(url_arg.getBytes("ISO-8859-1"), "GBK"));
56
out.println("您的表单传递的URL参数是:" + url_arg);
57
out.write("
");
58
}
59
60
public void init()throws ServletException {
61
// Put your code here
62
}
63
64
}
index.jsp代码:
01
02
03
04
String path = request.getContextPath();
05
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
06
%>
07
08
09
10
11
12
编码测试13
14
15
function clickFun() {
16
var base = document.base;
17
base.action = "./Hello?url_arg=中文";
18
base.submit();
19
}
20
21
22
23
24
25
26
27
28
点击发送
29
30
31
当然要运行servlet,必须配置web.xml
01
02
03
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
05
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
06
07
08
index.jsp
09
10
11
Hello
12
com.test.one.Hello
13
14
15
Hello
16
/Hello
17
18
41
接下来改一下tomcat中的server.xml文件中的配置(至于为什么可以google一下之前的属性,有大神比我讲解好)
1
2
connectionTimeout="20000"
3
redirectPort="8443" useBodyEncodingForURI="true" />