JavaWeb中獲取表單數(shù)據(jù)及亂碼問(wèn)題的解決方法
首先使用一個(gè)用戶提交界面作為舉例(文本框,密碼框,選擇,下拉表單等),效果如下
<!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>用戶注冊(cè)表</title> </head> <body> <!-- 用戶注冊(cè) --> <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>愛(ài)好</td> <td> <input type="checkbox" name="hobby" value="sport" /> 體育 <input type="checkbox" name="hobby" value="music" /> 音樂(lè) <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>個(gè)人簡(jiǎn)介</td> <td> <textarea rows="5" cols="60" name="introduce"></textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" value="注冊(cè)"/></td> </tr> </table> </form> </body> </html>
注:HTML < form> 標(biāo)簽的 action 屬性,其定義和用法是:
<!--必需的 action 屬性規(guī)定當(dāng)提交表單時(shí),向何處發(fā)送表單數(shù)據(jù)。 --> <form action="value">
屬性值為URL,表示向何處發(fā)送表單數(shù)據(jù)。其可能值:
絕對(duì) URL - 指向其他站點(diǎn)(比如 src=”www.example.com/example.htm”)
相對(duì) URL - 指向站點(diǎn)內(nèi)的文件(比如 src=”example.htm”)
例如,下面的表單擁有兩個(gè)輸入字段以及一個(gè)提交按鈕,當(dāng)提交表單時(shí),表單數(shù)據(jù)會(huì)提交到名為 “form_action.asp” 的頁(yè)面:
<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方法中對(duì)信息進(jìn)行獲取
public class RequestServlet5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解決post亂碼 // request.setCharacterEncoding("utf-8"); // 通過(guò) getParameter 獲得指定數(shù)據(jù) String username = request.getParameter("username"); System.out.println(username); // 獲得一個(gè)值 // 解決get亂碼(例如輸入中文) --- 使用手動(dòng)編碼 // 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); // 非空校驗(yàn) if (username != null && username.trim().length() > 0) { System.out.println("username 合法"); } // 使用 getParameter 獲得 checkbox(復(fù)選框) 提交數(shù)據(jù)。默認(rèn)只能獲得第一個(gè)數(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("--------------------------------"); // 打印所有請(qǐng)求提交參數(shù) // 方式一 : 先獲得所有參數(shù) name ,然后通過(guò)name 獲得value Enumeration<String> names = request.getParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement();// 獲得每一個(gè)參數(shù)名稱 System.out.println(name + ":" + Arrays.toString(request.getParameterValues(name))); } System.out.println("----------------------------"); // 方式二 :通過(guò)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); } }
請(qǐng)求參數(shù)亂碼的原因
URL編碼是一種瀏覽器用來(lái)打包表單輸入的格式。瀏覽器從表單中獲取所有的name和其中的值 ,將它們以name/value參數(shù)編碼(移去那些不能傳送的字符,將數(shù)據(jù)排行等等)作為URL的一部分或者分離地發(fā)給服務(wù)器。
不同的請(qǐng)求方式對(duì)應(yīng)不同的解決辦法:
post —- request.setCharacterEncoding(“客戶端編碼集”);
get亂碼手動(dòng)解決
username = URLEncoder.encode(username, “ISO-8859-1”);// 用ISO編碼 username = URLDecoder.decode(username, “utf-8”); // 用utf-8解碼
簡(jiǎn)化上面寫法 : username = new String(username.getBytes(“ISO-8859-1”), “utf-8”);
get亂碼 配置tomcat默認(rèn)解碼字符集
在tomcat/conf/server.xml
Connector中 添加一個(gè)屬性 URIEncoding=”utf-8”
結(jié)論:開發(fā)時(shí),盡量不要修改tomcat默認(rèn)解碼集 ,提交請(qǐng)求請(qǐng)盡量使用post ,如果非要使用get ,手動(dòng)編碼
相關(guān)文章
spring boot使用sharding jdbc的配置方式
這篇文章主要介紹了spring boot使用sharding jdbc的配置方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12SpringBoot 配置文件加載位置與優(yōu)先級(jí)問(wèn)題詳解
這篇文章主要介紹了SpringBoot 配置文件加載位置與優(yōu)先級(jí)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09圖解Java經(jīng)典算法歸并排序的原理與實(shí)現(xiàn)
歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide?and?Conquer)的一個(gè)非常典型的應(yīng)用。本文將通過(guò)動(dòng)圖詳解歸并排序的原理及實(shí)現(xiàn),需要的可以參考一下2022-09-09Java實(shí)現(xiàn)JS中的escape和UNescape代碼分享
在PHP和Python中都有類似JS中的escape和UNescape函數(shù)的功能,那么Java語(yǔ)言中到底有沒(méi)有類似的方法呢?本文就來(lái)介紹一下Java實(shí)現(xiàn)JS中的escape和UNescape轉(zhuǎn)碼方法,需要的朋友可以參考下2017-09-09詳解springcloud之服務(wù)注冊(cè)與發(fā)現(xiàn)
本次分享的是關(guān)于springcloud服務(wù)注冊(cè)與發(fā)現(xiàn)的內(nèi)容,將通過(guò)分別搭建服務(wù)中心,服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn)來(lái)說(shuō)明,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-06-06基于javaMybatis存進(jìn)時(shí)間戳的問(wèn)題
這篇文章主要介紹了javaMybatis存進(jìn)時(shí)間戳的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java框架Struts2實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08