1.getAttribute是取得jsp中 用setAttribute設定的attribute
2.parameter得到的是string;attribute得到的是object
3.request.getParameter()方法傳遞的資料,會從Web用戶端傳到Web伺服器端,代表HTTP請求資料;request.setAttribute()和getAttribute()方法傳遞的資料只會存在於Web容器內部,在具有轉寄關係的Web組件之間共用。即request.getAttribute()方法返回request範圍記憶體在的對象,而request.getParameter()方法是擷取http提交過來的資料。
JSP中getParameter與getAttribute有何區別?
——說實話,這個問題當初我也困惑很久,我也知道怎麼用,可是到底有什麼區別,我也不是很清楚,後來找了很多資料才明白。昨天又有一位朋友問我這個問題,想我當初同樣也困惑過,於是我就把這個問題貼出來,讓同樣困惑的朋友解惑。
——getParameter得到的都是String類型的。或者是http://a.jsp?id=123中的123,或者是某個表單提交過去的資料。
——getAttribute則可以是對象。
——getParameter()是擷取POST/GET傳遞的參數值;
——getAttribute()是擷取對象容器中的資料值;
——getParameter:用於用戶端重新導向時,即點擊了連結或提交按扭時傳值用,即用於在用表單或url重新導向傳值時接收資料用。
——getAttribute:用於伺服器端重新導向時,即在sevlet中使用了forward函數,或struts中使用了mapping.findForward。getAttribute只能收到程式用setAttribute傳過來的值。
——getParameter()是擷取POST/GET傳遞的參數值;
——getAttribute()是擷取SESSION的值;
另外,可以用setAttribute,getAttribute發送接收對象.而getParameter顯然只能傳字串。
setAttribute 是應用伺服器把這個對象放在該頁面所對應的一塊記憶體中去,當你的頁面伺服器重新導向到另一個頁面時,應用伺服器會把這塊記憶體拷貝另一個頁面所對應的記憶體中。這樣getAttribute就能取得你所設下的值,當然這種方法可以傳對象。session也一樣,只是對象在記憶體中的生命週期不一樣而已。
getParameter只是應用伺服器在分析你送上來的request頁面的文本時,取得你設在表單或url重新導向時的值。
getParameter 返回的是String, 用於讀取提交的表單中的值;
getAttribute 返回的是Object,需進行轉換,可用setAttribute設定成任意對象,使用很靈活,可隨時用;
多情況下,在請求轉寄時,把一些資料帶到轉寄後的頁面處理。這時,就可以使用request對象的setAttribute()方法將資料設定在request範圍記憶體取。轉寄資料的方法使用如下:
-
在範圍中管理屬性
很多情況下,在請求轉寄時,把一些資料帶到轉寄後的頁面處理。這時,就可以使用request對象的setAttribute()方法將資料設定在request範圍記憶體取。
轉寄資料的方法使用如下:
request.setAttribute("key", Object);
方法說明如下。
參數key是鍵,為String類型。在轉寄後的頁面取資料時,就通過這個鍵來擷取資料。參數object是索引值,為Object類型,代表需要儲存在request範圍內的資料。
擷取轉寄資料的方法使用如下:
request.getAttribute(String name);
方法說明如下。
參數name表示鍵名。
在頁面使用request對象的setAttribute("name",obj)方法,可以把資料obj設定在request範圍內。請求轉寄後的頁面使用getAttribute("name")方法;就可以取得資料obj。
本執行個體使用setAttribute()方法設定資料,然後在請求轉寄後取得設定的資料。程式開發步驟如下。
(1)建立名稱為index.jsp的頁面,該頁面通過request對象調用setAttribute()方法儲存資料,程式碼如下:
index.jsp關鍵代碼:
<%
String uName = "tom";
String uSex = "man";
String uAge = "26";
String uCity = "長春";
String uTel = "13999999";
request.setAttribute("name", uNname); //將uNname值存放在request對象中
request.setAttribute("sex", uSex); //將uSex值存放在request對象中
request.setAttribute("age", uAge); //將uAge值存放在request對象中
request.setAttribute("city", uCity); //將"city值存放在request對象中
request.setAttribute("tel", uTel); //將uTel值存放在request對象中
%>
<jsp:forward page="getAttribute.jsp" />
代碼說明如下。
程式的末尾必須使用JSP動作元素<jsp:forward page=" getAttribute.jsp" />將請求轉寄到getAttribute.jsp頁面。因此,在getAttribute.jsp頁面才能取得請求轉寄頁面的設定資料。
(2)建立名稱為getAttribute.jsp的頁面,該頁面的主要功能是通過request對象調用getAttribute()方法顯示資料,程式碼如下:
getAttribute.jsp關鍵代碼:
<table border="1">
<tr>
<td>姓名:</td>
<td><%=request.getAttribute("name")%></td> <!--通過getAttribute()方法擷取name對象的值-->
</tr>
<tr>
<td>性別:</td>
<td><%=request.getAttribute("sex")%></td> <!--通過getAttribute()方法擷取sex對象的值-->
</tr>
<tr>
<td>年齡:</td>
<td><%=request.getAttribute("age")%></td> <!--通過getAttribute()方法擷取age對象的值-->
</tr>
<tr>
<td>所在城市:</td>
<td><%=request.getAttribute("city")%></td> <!--通過getAttribute()方法擷取city對象的值-->
</tr>
<tr>
<td>電話:</td>
<td><%=request.getAttribute("tel")%></td> <!--通過getAttribute()方法擷取tel對象的值-->
</tr>
</table>
注意:
設定為request範圍記憶體取的資料,只能在請求轉寄後的頁面對其進行取值,其他頁面不能對其取值。
(3)程式發布後,啟動Tomcat伺服器,在瀏覽器地址欄中輸入如下地址:http://127. 0.0.1:8080/02,運行結果1所示。
圖1 getAttribute()方法取得資料
完整代碼:
index.jsp完整代碼:
<%@ page contentType="text/html;charset=gb2312"%>
<%
String uName = "城中狼";
String uSex = "男";
String uAge = "26歲";
String uCity = "長春";
String uTel = "12345678987";
request.setAttribute("name", uName);
request.setAttribute("sex", uSex);
request.setAttribute("age", uAge);
request.setAttribute("city", uCity);
request.setAttribute("tel", uTel);
%>
<jsp:forward page="getAttribute.jsp" />
getAttribute.jsp完整代碼:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</head>
<body>
<table width="222" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#000000">
<tr bgcolor="#FFFFFF">
<td width="101">姓名:</td>
<td width="108"><%=request.getAttribute("name")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>性別:</td>
<td><%=request.getAttribute("sex")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>年齡:</td>
<td><%=request.getAttribute("age")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>城市:</td>
<td><%=request.getAttribute("city")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>電話:</td>
<td><%=request.getAttribute("tel")%></td>
</tr>
</table>
</body>
</html>
WEB-INF/ web.xml完整代碼:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
當你在Servlet與JSP頁面跳轉時,經常會用到這兩個方法。但是區別是什麼呢。response.sendRedirect是重新將請求發給了用戶端,讓用戶端再重新組織request,然後訪問重定位的網址,因此,你在之前組裝的request裡的Attribute統統都沒了。但是如果你用的是 Session,Session裡的值還是有的。
而 request.getRequestDispatcher("servlet/PlatformList.jsp").forward(request,response); 是在伺服器內部進行跳轉,是帶著request和response的。之前設定的request中的attribute當然也在。所以可以用 request的attribute或者session來傳值,都可以。
然而,我在具體實驗時,卻遇到了 request.getRequestDispatcher("servlet/PlatformList.jsp").forward(request,response); 可以跳轉過去,但是卻獲得不了request裡的Attribute。而且自從用了 getServletContext().getRequestDispatcher進行跳轉後,在JSP頁面跳回Servlet的時候,地址也不用帶上 servlet了,直接用XXXservlet就可以了。我很奇怪,目前也沒有時間進行實驗探討了。因此先記錄下來。反正目前功能實現了就好。