兩個頁面a.jsp,b.jsp。一個發,一個收。
a.jsp
1
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
6
<title>A</title>
7
</head>
8
9
<body>
10
<%
11
String str_test = "中文";
12
%>
13
<form method=post action="b.jsp?test=<%=java.net.URLEncoder.encode(str_test) %>">
14
<input type="submit" value="按鈕1">
15
</form>
16
17
<form method=post action="b.jsp?test=<%=str_test %>">
18
<input type="submit" value="按鈕2">
19
</form>
20
21
</body>
22
</html>
23

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

b.jsp
1
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head><title>B</title></head>
5
6
<body>
7
8
<%
9
String str = new String(request.getParameter("test").getBytes("ISO8859_1"));
10
%>
11
<BR>
12
<%=str %>
13
<BR>
14
<%
15
String str1 = new String(request.getParameter("test").getBytes("ISO8859_1"),"gb2312");
16
%>
17
<BR>
18
<%=str1 %>
19
20
</body>
21
</html>
22

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

運行a.jsp,點擊按鈕1.
提交到b.jsp。
如果直接傳中文呢?點擊按鈕2.
結果:
關鍵的問題在于每個頁面中的 charset=gb2312;這個是頁面顯示中文的關鍵