1. 程式人生 > >JAVA-JSP內置對象之request獲得封裝所有參數值的Map

JAVA-JSP內置對象之request獲得封裝所有參數值的Map

method set 所有 eight req spa pos bmi ges

技術分享

相關資料:
《21天學通Java Web開發》

獲得封裝所有參數值的Map
1.通過request對象的getParameterMap()方法來獲得封裝所有的參數值的Map對象。
2.通過該Map對象可以獲得指定參數的參數值。

RequestForm5.jsp

技術分享
 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>表單</title>
 5 </head>
6 <body> 7 <form action="RequestDemo5.jsp" method="post"> 8 用戶名:<input type= "text" name="username"/><br> 9 用戶密碼:<input type= "password" name="userpassword"/><br> 10 喜歡的運動: 11 <input type = "checkbox" name="sport" value="pingpang">乒乓球
12 <input type = "checkbox" name="sport" value="badketball">籃球 13 <input type = "checkbox" name="sport" value="football">足球<br> 14 <input type="submit" value="提交"> 15 </form> 16 </body> 17 </html>
View Code

RequestDemo5.jsp

技術分享
 1 <%
@ page language="java" contentType="text/html;charset=gb2312" import="java.util.*" %> 2 <html> 3 <head> 4 <title>使用request對象獲得封裝所有參數值的Map</title> 5 </head> 6 <body> 7 <%-- 通過request對象的getParameterMap封裝所有的參數值的Map --%> 8 <% 9 Map<String, String[]> mapParamter = request.getParameterMap();//獲得封裝備的有參數值的Map 10 String[] strUsername = (String[])mapParamter.get("username");//獲得其中的username參數值 11 out.println("用戶名:"+strUsername[0]+"<br>");//打印輸出username參數值 12 String[] strPassword = (String[])mapParamter.get("userpassword");//獲得suerpassword參數值 13 out.println("用戶密碼:"+strPassword[0]+"<br>");//打印輸出userpassword參數值 14 String[] strSport = (String[])mapParamter.get("sport");//獲得其中的sport參數值 15 out.println("喜歡的運動:"); 16 for(String sport:strSport){ 17 out.println(sport);//遍歷打印輸出sport參數值 18 } 19 %> 20 </body> 21 </html>
View Code

JAVA-JSP內置對象之request獲得封裝所有參數值的Map