getAttribute getParameter 區別

來源:互聯網
上載者:User

網上說的不少,但都比較亂,紀錄點作為參考。

getAttribute 和 getParameter 的區別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.getAttribute():是request時設定的變數的值,用request.setAttribute("name","您自己的值");來設定值,
request.getParameter():提取發送過來的參數如:本網頁
http://community.csdn.net/Expert/topic/4633/4633804.xml?temp=.3488123
request.getParameter("temp")==".3488123"

request.getParameter
是用來接受來自get方法或post方法的參數
<form method=post>
<form method=get>
<a href="1.jsp?id=1">ok</a>
只能接受java.lang.String
也就是說String hotel_id = request.getParameter("hotel_id");
request.getAttribute
是用來接受來自servlet的變數或Action(其實Action就是特殊的Servlet)
在Action中,request.setAttribute("ret",ret);
只能接受java.lang.Object
也就是說List ret = (List)request.getAttribute("ret");
如果你只用JSP,根本用不到request.getAttribute()

request.getAttribute()和request.getParameter()的區別是request.getAttribute()獲得的是物件類型,而request.getParameter()獲得的是字串類型

 

一般的網頁應用,如同 chenshaizi(陳紹彬) 所說,基本上是基於Post方式的傳遞,用getParameter取值。對於自己控制的,可以通過request.setAttribute和getAttribute 實現值得傳遞。
對於應用Structs架構的,getAttribute用的多一點,其他的基本上用getParameter

我的理解:
session.getAttribute();獲得session
request.getParameter();獲得parameter

1.getParameter可以獲得用戶端傳送給伺服器端的參數值。    
getAttribute可以得到由setAttribute設定的參數值,就相當於是使用getAttribute得到一個自己定義的參數,而不是從用戶端得到的參數。
2.getParameter只能傳遞string類型的變數,getAttribute能傳遞vector。

 

getParameter(),擷取表單的值 getAttribute()獲得session的值
getParameterNames() 獲得表單或則url中的參數的數組
getattributeNames():返回request對象所有屬性的名字,結果集是一個Enumeration(枚舉)類的執行個體

根據樓上,是不是 getParameter()得到的值如果下次不提交或儲存起來的話,下次重新導向後就沒啦?
:理解對了
getAttribute()所要得到的屬性值因為存在session中,所以重新導向後仍可以取出?
getAttribute()在request和session中都有,只是範圍不同,在取之前肯定是要在某個地方存一下,這種東東可以存取對象

呵呵

http://community.csdn.net/Expert/topic/4763/4763471.xml?temp=.1793177

看見後面的?temp=.1793177沒有,?號後面的這個東西用request.getAttribute("temp")就能得到字串".1793177",
而getAttribute()之前,你必須在頁面或者邏輯中用serAttribute()設定了才能用,已經很清楚了,我不再說了哈

 

Parameter是html裡傳來的 像 checkbox textfield password radio ...的value
getAttribute是取得jsp中用setAttribute設定的attribute還有....
parameter得到的是string
attribute得到的是object

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.