首先使用一個用戶提交界面作為舉例(文本框,密碼框,選擇,下拉表單等),效果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>用戶注冊表</title> </head> <body> <!-- 用戶注冊 --> <form action= "/requesttest/request5" method= "get" > <table> <!-- 文本輸入框 --> <tr> <td>用戶名</td> <td><input type= "text" name= "username" /></td> </tr> <!-- 密碼框 --> <tr> <td>密碼</td> <td><input type= "password" name= "password" /></td> </tr> <!-- 單選按鈕 radio--> <tr> <td>性別</td> <td> <input type= "radio" name= "gender" value= "male" /> 男 <input type= "radio" name= "gender" value= "female" />女 </td> </tr> <!-- 復選框 --> <tr> <td>愛好</td> <td> <input type= "checkbox" name= "hobby" value= "sport" /> 體育 <input type= "checkbox" name= "hobby" value= "music" /> 音樂 <input type= "checkbox" name= "hobby" value= "game" /> 游戲 </td> </tr> <!-- 下拉框 --> <tr> <td>城市</td> <td> <select name= "city" > <option value= "beijing" >北京</option> <option value= "shanghai" >上海</option> <option value= "shenzhen" >深圳</option> </select> </td> </tr> <!-- 多行文本框 --> <tr> <td>個人簡介</td> <td> <textarea rows= "5" cols= "60" name= "introduce" ></textarea> </td> </tr> <tr> <td colspan= "2" ><input type= "submit" value= "注冊" /></td> </tr> </table> </form> </body> </html> |
注:HTML < form> 標簽的 action 屬性,其定義和用法是:
1
2
|
<!--必需的 action 屬性規定當提交表單時,向何處發送表單數據。 --> <form action= "value" > |
屬性值為URL,表示向何處發送表單數據。其可能值:
絕對 URL - 指向其他站點(比如 src=”www.example.com/example.htm”)
相對 URL - 指向站點內的文件(比如 src=”example.htm”)
例如,下面的表單擁有兩個輸入字段以及一個提交按鈕,當提交表單時,表單數據會提交到名為 “form_action.asp” 的頁面:
1
2
3
4
5
|
<form action= "form_action.asp" method= "get" > <p>First name: <input type= "text" name= "fname" /></p> <p>Last name: <input type= "text" name= "lname" /></p> <input type= "submit" value= "Submit" /> </form> |
method為get,因此在servlet的doGet方法中對信息進行獲取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
public class RequestServlet5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解決post亂碼 // request.setCharacterEncoding("utf-8"); // 通過 getParameter 獲得指定數據 String username = request.getParameter( "username" ); System.out.println(username); // 獲得一個值 // 解決get亂碼(例如輸入中文) --- 使用手動編碼 // username = URLEncoder.encode(username, "ISO-8859-1");// 用ISO編碼 // username = URLDecoder.decode(username, "utf-8"); // 用utf-8解碼 username = new String(username.getBytes( "ISO-8859-1" ), "utf-8" ); System.out.println(username); // 非空校驗 if (username != null && username.trim().length() > 0 ) { System.out.println( "username 合法" ); } // 使用 getParameter 獲得 checkbox(復選框) 提交數據。默認只能獲得第一個數據 String hobby = request.getParameter( "hobby" ); // 多選框 System.out.println(hobby); // 獲得checkbox所有提交數據--- getParameterValues String[] hobbies = request.getParameterValues( "hobby" ); System.out.println(Arrays.toString(hobbies)); System.out.println( "--------------------------------" ); // 打印所有請求提交參數 // 方式一 : 先獲得所有參數 name ,然后通過name 獲得value Enumeration<String> names = request.getParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); // 獲得每一個參數名稱 System.out.println(name + ":" + Arrays.toString(request.getParameterValues(name))); } System.out.println( "----------------------------" ); // 方式二 :通過request.getParameterMap Map<String, String[]> parameterMap = request.getParameterMap(); Set<String> keys = parameterMap.keySet(); for (String key : keys) { // key是參數 name System.out.println(key + ":" + Arrays.toString(parameterMap.get(key))); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
請求參數亂碼的原因
URL編碼是一種瀏覽器用來打包表單輸入的格式。瀏覽器從表單中獲取所有的name和其中的值 ,將它們以name/value參數編碼(移去那些不能傳送的字符,將數據排行等等)作為URL的一部分或者分離地發給服務器。
不同的請求方式對應不同的解決辦法:
post —- request.setCharacterEncoding(“客戶端編碼集”);
get亂碼手動解決
1
2
|
username = URLEncoder.encode(username, “ISO- 8859 - 1 ”); // 用ISO編碼 username = URLDecoder.decode(username, “utf- 8 ”); // 用utf-8解碼 |
簡化上面寫法 : username = new String(username.getBytes(“ISO-8859-1”), “utf-8”);
get亂碼 配置tomcat默認解碼字符集
在tomcat/conf/server.xml
Connector中 添加一個屬性 URIEncoding=”utf-8”
結論:開發時,盡量不要修改tomcat默認解碼集 ,提交請求請盡量使用post ,如果非要使用get ,手動編碼
原文鏈接:http://blog.csdn.net/megustas_jjc/article/details/53335529