从form表单提交信息到一个JSP页面或者一个Servlet进行处理的时候,提交的中文信息若不加处理的话就会显示乱码,如一串???。现在通过一个例子来进行总结如下:
写一个用户信息提交页面,通过这个页面向一个JSP页面或者一个servlet提交用户信息,代码如下:
1
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=gbk
"
%>
2
<
html
>
3
<
head
>
4
<
title
>
表单提交
</
title
>
5
</
head
>
6
<
body
>
7
<
form action
=
"
deal.jsp
"
method
=
"
post
"
>
8
用户名:
<
input type
=
"
text
"
name
=
"
username
"
><
br
>
9
密
&
nbsp;
&
nbsp;码:
<
input type
=
"
password
"
name
=
"
password
"
><
br
>
10
爱
&
nbsp;
&
nbsp;好:
<
input type
=
"
radio
"
name
=
"
love
"
value
=
"
运动
"
>
运动
&
nbsp;
11
<
input type
=
"
radio
"
name
=
"
love
"
value
=
"
音乐
"
>
音乐
<
br
>
12
&
nbsp;
&
nbsp;
<
input type
=
"
submit
"
value
=
"
提交
"
>
13
</
form
>
14
</
body
>
15
</
html
>
现在写deal处理页面,代码如下:
1
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=gbk
"
%>
2
<
html
>
3
<
head
>
4
<
title
>
显示用户信息
</
title
>
5
</
head
>
6
<
body
>
7
<%
8
//
request.setCharacterEncoding("gb2312");
9
String username
=
request.getParameter(
"
username
"
);
10
//
String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"gbk");
11
String password
=
request.getParameter(
"
password
"
);
12
//
String love = new String(request.getParameter("love").getBytes("iso-8859-1"),"gbk");
13
String love
=
request.getParameter(
"
love
"
);
14
%>
15
<%=
username
%>
您好,你的密码是:
<%=
password
%>
,您的爱好是:
<%=
love
%>!
16
</
body
>
17
</
html
>
从前面的信息提交页面提交来的信息包含中文,这时就会出现乱码。如:
??????
您好,你的密码是:
1234569
,您的爱好是:
????!
现在,把第8行的注释符号去掉,重新执行页面(请确保web服务器会自动加载更改后的页面,否则请重新启动web服务器),这时可以看到正确的中文信息了,如:
啊拉拉您好,你的密码是:
9856322
,您的爱好是:音乐
!
也可以使用另外一种方法进行处理,把deal.jsp的第8行注释掉,然后把第9行、第13行也注释掉,去掉第10行和第12行的注释符号,保存好重新执行页面(方法同上),同样会显示正常的信息。
下面通过前面的信息提交页面向一个servlet提交信息,然后对其中的中文乱码进行处理。写一个servlet程序(formdeal.java),如下:
1
package
org.wzhongyu;
2

3
import
java.io.IOException;
4
import
java.io.PrintWriter;
5
import
javax.servlet.ServletException;
6
import
javax.servlet.http.HttpServlet;
7
import
javax.servlet.http.HttpServletRequest;
8
import
javax.servlet.http.HttpServletResponse;
9

10

public
class
formdeal
extends
HttpServlet
{
11
public void destroy() {
12
super.destroy(); // Just puts "destroy" string in log
13
// Put your code here
14
}
15
16
public void doGet(HttpServletRequest request, HttpServletResponse response)
17
throws ServletException, IOException {
18
this.doPost(request, response);
19
}
20
21
public void doPost(HttpServletRequest request, HttpServletResponse response)
22
throws ServletException, IOException {
23
//response.setContentType("text/html; charset=gbk");
24
PrintWriter out = response.getWriter();
25
//request.setCharacterEncoding("gbk");
26
String username = request.getParameter("username");
27
String password = request.getParameter("password");
28
String love = request.getParameter("love");
29
out.print("您的用户名:" + username + "<br>"); //
30
out.print("您的密码:" + password + "<br>"); //
31
out.print("您的爱好:" + love); //
32
}
33
public void init() throws ServletException {
34
// Put your code here
35
}
36
}
37
该servlet的部署描述文件(web.xml)如下:
1
<?
xml version="1.0" encoding="UTF-8"
?>
2
<
web-app
version
="2.5"
3
xmlns
="http://java.sun.com/xml/ns/javaee"
4
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
6
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
7
<
servlet
>
8
<
description
>
This is the description of my J2EE component
</
description
>
9
<
display-name
>
This is the display name of my J2EE component
</
display-name
>
10
<
servlet-name
>
formdeal
</
servlet-name
>
11
<
servlet-class
>
org.wzhongyu.formdeal
</
servlet-class
>
12
</
servlet
>
13

14
<
servlet-mapping
>
15
<
servlet-name
>
formdeal
</
servlet-name
>
16
<
url-pattern
>
/servlet/formdeal
</
url-pattern
>
17
</
servlet-mapping
>
18
</
web-app
>
把信息提交页面的第7行改为:
<
form
action
="./servlet/formdeal"
method
="post"
>
重新部署并执行页面,同样看到显示的中文信息是乱码。现在把第23行的注释符去掉,重新执行会看到下面的信息,提交过来的中文信息是乱码:
您的用户名:
??????
您的密码:
123465
您的爱好:
????
把第25行的注释符也去掉,重新执行,可以看到可以显示正常的信息了,如下:
您的用户名:啊拉拉
您的密码:5632215
您的爱好:音乐
如果只去掉第25行的注释,执行程序则会显示下面的信息:
?????????
?????
123456
???????
由此可见,这个两个都不可以忽略掉,也可以从下面的方式验证必须写上两个,把formdeal.java里的第29,30,31行的中文换成英文,同样注释掉第23行,而不要注释掉第25行,执行后显示的信息如下:
username
???
password65462458
love
??
这是由于没有设置servlet响应的页面的字符编码造成的。
在servlet里也可以这样进行处理,把第25行注释掉,而不要注释第23行,把第26行和第28行分别改为如下代码:
String username
=
new
String(request.getParameter(
"
username
"
).getBytes(
"
iso-8859-1
"
),
"
gbk
"
);
String love
=
new
String(request.getParameter(
"
love
"
).getBytes(
"
iso-8859-1
"
),
"
gbk
"
);
这样也可以正常显示中文信息。