request屬性
主要作用是接收客戶端發送過來的請求信息,如參數等。
Request是javax.servlet.http.HttpServletRequest接口的實例化對象。常用方法有:
No | 方法 | 類型 | 描述 |
1 | Public String getParameter(String name) | 普通 | 取得客戶端發來的請求參數內容 |
2 | Public String[] getParameter(String name) | 普通 | 取得客戶端發來的一組請求參數內容 |
3 | Public Enumeraton getParameterName() | 普通 | 取得全部請求參數的名稱 |
4 | Pulbic String getRemoteAddr() | 普通 | 得到客戶端IP地址 |
5 | Void setCharacterEncoding(String env) | 普通 | 設置統一的請求編碼 |
6 | Public ovid isUserInRole(Sting role) | 普通 | 進行用戶身份的驗證 |
7 | 等等 | 普通 |
1.亂碼解決
request.setCharacterEncoding("GBK"); //設置統一編碼
例
<!doctype html> <html> <head> <meta charset="GBK"> <title>request</title> </head> <body> <form action="request_demo01.jsp" method="post"> 請輸入信息:<input type="text" name="info" > <input type="submit" value="提交" > </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head> <title>request</title> </head> <body> <% //設置統一編碼 request.setCharacterEncoding("GBK"); //接收表單提交的參數 String content = request.getParameter("info"); %> <h2><%=content%></h2> </body> </html>
注意:
如果是編輯器不支持中文則運行時也是不會顯示中文。如直接安裝的sublime、DW都不是直接支持中文的。EditPlus編輯肯定支持中文,但寫代碼速度慢,比較適合初學者。
2.接收請求參數
2.1單一參數可使用getParameter()方法接收;而一組參數要使用getParameterValues()方法接收。若一組參數使用getParameter()接收只是接收和一個選中參數。
例
<!doctype html> <html> <head> <meta charset="GBK"> <title>udbful</title> </head> <body> <form action="request_demo02.jsp" method="post"> 姓名:<input type="text" name="username"><br> 興趣:<input type="checkbox" name="inst" value="唱歌">唱歌 <input type="checkbox" name="inst" value="跳舞">跳舞 <input type="checkbox" name="inst" value="游泳">游泳 <input type="checkbox" name="inst" value="看書">看書 <input type="checkbox" name="inst" value="上網">上網 <input type="hidden" name="id" value="3"> <!--定義隱藏域--> <br><input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head> <title>include</title> </head> <body> <% request.setCharacterEncoding("GBK"); String id = request.getParameter("id"); String name = request.getParameter("username"); String inst[] = request.getParameterValues("inst"); %> <h2>編號:<%=id%></h2> <h2>姓名:<%=name%></h2> <% if(null != inst){ %> <h3>興趣: <% for(int x=0; x<inst.length; x++){ %> <%=inst[x]%>、 <% } %> </h3> <% } %> </body> </html>
2.2URL地址重寫(通過地址欄傳遞參數)
動態頁面地址?參數名稱1=參數內容1&參數名稱2=參數內容2&……
例
<%@page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>include</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String param1 = request.getParameter("name");
String param2 =request.getParameter("password");
%>
<h2>用戶名:<%=param1%></h2>
<h2>密碼:<%=param2%></h2>
</body>
</html>
在瀏覽器中輸出下面地址
http://localhost/newudbful/Chapter6/02request/request_demo03.jsp?name=zzg&password=123
2.3關於表單提交方式post與get
兩者有個明顯的區別在於,post提交表單,提交的內容不會顯示在地址欄上;而get提交時,提交的內容會以地址重寫方式顯示在地址欄上。
一般情況表單提交方式用post比較多。
一般而言當提交表單數據較大時則是用post方式提交。
2.4使用getParameterNames()方法傳遞參數,此方式一般用於表單動態變化的情況,如購物車網站程序開發中。
例
<!doctype html> <html> <head> <meta charset="GBK"> <title>udbful</title> </head> <body> <form action="request_demo04.jsp" method="post"> 姓名:<input type="text" name="username"><br> 性別:<input type="radio" name="sex" value="男" CHECKED >男 <input type="radio" name="sex" value="女" >女<br> 城市:<select name="city"> <option value="北京">北京</option> <option value="上海">上海</option> <option value="南寧">南寧</option> <option value="鷹潭">鷹潭</option> <option value="上饒">上饒</option> </select><br> 興趣:<input type="checkbox" name="**inst" value="唱歌">唱歌 <input type="checkbox" name="**inst" value="跳舞">跳舞 <input type="checkbox" name="**inst" value="游泳">游泳 <input type="checkbox" name="**inst" value="看書">看書 <input type="checkbox" name="**inst" value="上網">上網<br> 自我介紹:<textarea cols="30" rows="3" name="note"></textarea><br> <input type="hidden" name="id" value="1"><br> <!--定義隱藏域--> <input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*"%> <html> <head> <title>include</title> </head> <body> <% request.setCharacterEncoding("GBK"); %> <center> <table border="1"> <tr> <td>參數名稱</td> <td>參數內容</td> </tr> <% //接收全部請求參數據的名稱 Enumeration enu = request.getParameterNames(); //依次取出每一個參數名稱 while(enu.hasMoreElements()){ String paramName = (String) enu.nextElement(); %> <tr> <td><%=paramName%></td> <td> <% if(paramName.startsWith("**")){ String paramValue[] = request.getParameterValues(paramName); for(int x=0; x<paramValue.length; x++){ %> <%=paramValue[x]%>、 <% } }else{ String paramValue = request.getParameter(paramName); %> <%=paramValue%> <% } %> </td> </tr> <% } %> </table> </center> </body> </html>
3.顯示全部的頭信息(略)
使用getHeaderNames()/getHeader()方法
4.角色驗證(略)
使用isUserInRole()方法及配置文件
5.IP地址、路徑信息、提交方式取得
例1
<%@page contentType="text/html" pageEncoding="GBK"%> <%@page import="java.util.*"%> <html> <head> <title>include</title> </head> <body> <% //取得提交方式 String method = request.getMethod(); //取得客戶端IP地址 String ip = request.getRemoteAddr(); //取得訪問路徑 String path = request.getServletPath(); //取得上下文資源名稱 String contextPath =request.getContextPath(); %> <h2>請求方式:<%=method%></h2> <h2>IP地址:<%=ip%></h2> <h2>訪問路徑:<%=path%></h2> <h2>上下文名稱:<%=contextPath%></h2> </body> </html>
例2
<%@page contentType="text/html" pageEncoding="GBK"%> <%@page import="java.util.*"%> <html> <head> <title>include</title> </head> <body> <imgsrc="../images/udbful.jpg"> <img src="<%=request.getContextPath()%>/images/udbful.jpg"; </body> </html>
<imgsrc="<%=request.getContextPath()%>/images/udbful.jpg";使用更加安全而不是用../
以上內容參考JAVAWEB開發實戰經典(名師講壇)