JavaWeb中獲取表單數(shù)據(jù)及亂碼問題的解決方法
首先使用一個用戶提交界面作為舉例(文本框,密碼框,選擇,下拉表單等),效果如下

<!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> <!-- 復(fù)選框 --> <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> 標(biāo)簽的 action 屬性,其定義和用法是:
<!--必需的 action 屬性規(guī)定當(dāng)提交表單時,向何處發(fā)送表單數(shù)據(jù)。 --> <form action="value">
屬性值為URL,表示向何處發(fā)送表單數(shù)據(jù)。其可能值:
絕對 URL - 指向其他站點(比如 src=”www.example.com/example.htm”)
相對 URL - 指向站點內(nèi)的文件(比如 src=”example.htm”)
例如,下面的表單擁有兩個輸入字段以及一個提交按鈕,當(dāng)提交表單時,表單數(shù)據(jù)會提交到名為 “form_action.asp” 的頁面:
<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方法中對信息進(jìn)行獲取
public class RequestServlet5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 解決post亂碼
// request.setCharacterEncoding("utf-8");
// 通過 getParameter 獲得指定數(shù)據(jù)
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(復(fù)選框) 提交數(shù)據(jù)。默認(rèn)只能獲得第一個數(shù)據(jù)
String hobby = request.getParameter("hobby"); // 多選框
System.out.println(hobby);
// 獲得checkbox所有提交數(shù)據(jù)--- getParameterValues
String[] hobbies = request.getParameterValues("hobby");
System.out.println(Arrays.toString(hobbies));
System.out.println("--------------------------------");
// 打印所有請求提交參數(shù)
// 方式一 : 先獲得所有參數(shù) name ,然后通過name 獲得value
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();// 獲得每一個參數(shù)名稱
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是參數(shù) name
System.out.println(key + ":"
+ Arrays.toString(parameterMap.get(key)));
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
請求參數(shù)亂碼的原因
URL編碼是一種瀏覽器用來打包表單輸入的格式。瀏覽器從表單中獲取所有的name和其中的值 ,將它們以name/value參數(shù)編碼(移去那些不能傳送的字符,將數(shù)據(jù)排行等等)作為URL的一部分或者分離地發(fā)給服務(wù)器。

不同的請求方式對應(yīng)不同的解決辦法:
post —- request.setCharacterEncoding(“客戶端編碼集”);
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”);
get亂碼 配置tomcat默認(rèn)解碼字符集
在tomcat/conf/server.xml
Connector中 添加一個屬性 URIEncoding=”utf-8”
結(jié)論:開發(fā)時,盡量不要修改tomcat默認(rèn)解碼集 ,提交請求請盡量使用post ,如果非要使用get ,手動編碼
相關(guān)文章
Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決
這篇文章主要介紹了Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring?@DateTimeFormat日期格式化時注解場景分析
這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時注解場景分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
Java數(shù)據(jù)結(jié)構(gòu)之雙端鏈表原理與實現(xiàn)方法
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之雙端鏈表原理與實現(xiàn)方法,簡單描述了雙端鏈表的概念、原理并結(jié)合實例形式分析了java實現(xiàn)雙端鏈表的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10

