国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

JavaWeb中獲取表單數據及亂碼問題的解決方法

2019-11-26 13:31:17
字體:
來源:轉載
供稿:網友

首先使用一個用戶提交界面作為舉例(文本框,密碼框,選擇,下拉表單等),效果如下

這里寫圖片描述

<!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 屬性,其定義和用法是:

<!--必需的 action 屬性規定當提交表單時,向何處發送表單數據。 --><form action="value">

屬性值為URL,表示向何處發送表單數據。其可能值:

絕對 URL - 指向其他站點(比如 src=”www.example.com/example.htm”)

相對 URL - 指向站點內的文件(比如 src=”example.htm”)

例如,下面的表單擁有兩個輸入字段以及一個提交按鈕,當提交表單時,表單數據會提交到名為 “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方法中對信息進行獲取

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所有提交數據--- getParameterValuesString[] hobbies = request.getParameterValues("hobby");System.out.println(Arrays.toString(hobbies));System.out.println("--------------------------------");// 打印所有請求提交參數// 方式一 : 先獲得所有參數 name ,然后通過name 獲得valueEnumeration<String> names = request.getParameterNames();while (names.hasMoreElements()) {String name = names.nextElement();// 獲得每一個參數名稱System.out.println(name + ":"+ Arrays.toString(request.getParameterValues(name)));}System.out.println("----------------------------");// 方式二 :通過request.getParameterMapMap<String, String[]> parameterMap = request.getParameterMap();Set<String> keys = parameterMap.keySet();for (String key : keys) { // key是參數 nameSystem.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亂碼手動解決

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 ,手動編碼

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 厦门市| 镇沅| 聂拉木县| 宜兴市| 姚安县| 融水| 台北市| 乐都县| 商丘市| 乾安县| 乌鲁木齐县| 铜川市| 准格尔旗| 仁化县| 武宁县| 泗水县| 新兴县| 龙井市| 嘉兴市| 旺苍县| 中山市| 武城县| 青浦区| 门源| 赫章县| 武城县| 新源县| 封丘县| 韶关市| 辰溪县| 玛纳斯县| 辉县市| 惠安县| 云浮市| 吉安市| 淮滨县| 定西市| 邵阳市| 台北县| 哈尔滨市| 巴里|