Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
表單代碼
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action="getParamter" method="get"> <input type="text" name="username" placeholder="請輸入用戶名"></br> <input type="password" name="password" placeholder="請輸入密碼"></br> <input type="checkbox" name="hobby" value="study">學習 <input type="checkbox" name="hobby" value="basktball">打籃球 <input type="checkbox" name="hobby" value="sleep">睡覺</br> <input type="submit" value="提交"> </form> </body> </html>
request.getParamter(String name);通過name獲取值
代碼片段
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通過name屬性獲取值 String name = request.getParameter("username"); System.out.println("獲取到的姓名是:"+name); String pwd = request.getParameter("password"); System.out.println("獲取到的密碼是: "+pwd); }
運行結果
request.getParamterValues(String name);通過name獲取value值(一般用于復選框獲取值) 代碼片段
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通過name屬性獲取value值 String[] names = request.getParameterValues("username"); for (String name : names) { System.out.println("獲取得到的姓名是 " + name); } String[] hobbys = request.getParameterValues("hobby"); for (String hobby : hobbys) { System.out.println("復選框的值是: " + hobby); } }
運行結果
總結:此方法雖然是通過name獲取value值,但是對于普通輸入框是直接獲取的是它們的輸入值,類似第一種方法,普通輸入框獲取值可以使用但是不建議使用,一般用來獲取復選框的值
request.getParameterNames();直接獲取表單所有對象的name值,返回值是枚舉集合
代碼片段
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通過name屬性獲取value值 Enumeration<String> names2 = request.getParameterNames(); while (names2.hasMoreElements()){ String name =names2.nextElement(); System.out.println(name); } }
運行結果
總結:獲取到name值以后采用第一種方法獲取真實的值
request.getParameterMap();直接獲取表單所有對象的name值以及數(shù)據(jù)
該方法的返回值是map集合,集合key是String類型,value是Sting類型的數(shù)組
代碼片段
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通過map代表所有的值 ,集合中的key代表表單name值 value代表表單數(shù)據(jù) Map<String, String[]> map = request.getParameterMap(); //通過keySet遍歷集合 Set<String> keySet = map.keySet(); for(String key : keySet){ System.out.println("表單的name值: "+key); //通過key值獲取所有value值 String[] values = map.get(key); for(String value : values ){ System.out.println("表單中的數(shù)據(jù): "+value); } } }
運行結果
總結:該方法獲取所有值,所以有復選框的時候可以選擇這種方法
到此這篇關于Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解的文章就介紹到這了,更多相關Javaweb Request獲取表單數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringCloud通過Feign傳遞List類型參數(shù)方式
這篇文章主要介紹了SpringCloud通過Feign傳遞List類型參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java 將PPT幻燈片轉為HTML文件的實現(xiàn)思路
本文以Java程序代碼為例展示如何通過格式轉換的方式將PPT幻燈片文檔轉為HTML文件,本文通過實例代碼圖文相結合給大家分享實現(xiàn)思路,需要的朋友參考下吧2021-06-06Spring事務執(zhí)行流程及如何創(chuàng)建事務
這篇文章主要介紹了Spring事務執(zhí)行流程及如何創(chuàng)建事務,幫助大家更好的理解和學習使用spring框架,感興趣的朋友可以了解下2021-03-03SpringMVC互聯(lián)網(wǎng)軟件架構REST使用詳解
這篇文章主要為大家詳細介紹了SpringMVC互聯(lián)網(wǎng)軟件架構REST的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03