Attribute 和 Parameter 的區別


Attribute 和 Parameter 的區別

(1)HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法

(2)當兩個Web組件之間為鏈接關系時,被鏈接的組件通過getParameter()方法來獲得請求參數,

(3)當兩個Web組件之間為轉發關系時,轉發目標組件通過getAttribute()方法來和轉發源組件共享request范圍內的數據。

一般通過表單和鏈接傳遞的參數使用getParameter

通過request.setAttribute("name","jerry")的方式賦值的使用request.getAttribute("name")

這個問題主要是request和session的差別,request范圍較小一些,只是一個請求,簡單說就是你在頁面上的一個操作, request.getParameter()就是從上一個頁面中的url、form中獲取參數,但如果一個request涉及多個類,后面還要取參數,可以用request.setAttribute()和request.getAttribute(),但是當結果輸出之后,request就結束了。

而session可以跨越很多頁面,可以理解是客戶端同一個IE窗口發出的多個請求。這之間都可以傳遞參數,比如很多網站的用戶登錄都用到了。

一般可以用getParameter得到頁面參數。。。字符串。。。

getAttribute()可以得到對象。。。

getParameter可以得到頁面傳來的參數如?id=123之類的。

getAttribute()常用於servlet頁面傳遞參數給jsp

個人見解:

當用戶把值作為鏈接參數傳遞給下個頁面或serve時,用getParameter()獲取.如aa.jsp?id=1;還有表單的提交.

當用戶把值放在了request中的一個屬性時(request.setAttribute("aa","tt"),"aa"這個屬性名可以任意取的),用getAttribute(屬性名)獲取.

request范圍較小一些,只是一個請求,簡單說就是你在頁面上的一個操作, request.getParameter()就是從上一個頁面中的url、form中獲取參數。但如果一個request涉及多個類,后面還要取參數,可以用request.setAttribute()和request.getAttribute(),但是當結果輸出之后,request就結束了。

而session可以跨越很多頁面。范圍大於request。

request.getParameter() 和request.getAttribute()  
(1)request.getParameter()取得是通過容器的實現來取得通過類似post,get等方式傳入的數據,,request.setAttribute()和getAttribute()只是在web容器內部流轉,僅僅是請求處理階段。  
(2)request.getParameter()方法傳遞的數據,會從Web客戶端傳到Web服務器端,代表HTTP請求數據。request.getParameter()方法返回String類型的數據。  
request.setAttribute()和getAttribute()方法傳遞的數據只會存在於Web容器內部  
還有一點就是,HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法。  
拿一個例子來說一下吧,假如兩個WEB頁面間為鏈接關系時,就是說要從1.jsp鏈接到2.jsp時,被鏈接的是2.jsp可以通過getParameter()方法來獲得請求參數.  
假如1.jsp里有  
<form name="form1" method="post" action="2.jsp">  
請輸入用戶姓名:<input type="text" name="username">  
<input type="submit" name="Submit" value="提交">  
</form>的話在2.jsp中通過request.getParameter("username")方法來獲得請求參數username:  
<% String username=request.getParameter("username"); %>  
************************************************************  
但是如果兩個WEB間為轉發關系時,轉發目的WEB可以用getAttribute()方法來和轉發源WEB共享request范圍內的數據,也還是說一個例子吧。  
有1.jsp和2.jsp  
1.jsp希望向2.jsp傳遞當前的用戶名字,如何傳遞這一數據呢?先在1.jsp中調用如下setAttribute()方法:  
<%  
String username=request.getParameter("username");  
request.setAttribute("username",username);  
%>  
<jsp:forward page="2.jsp" />  
在2.jsp中通過getAttribute()方法獲得用戶名字:  
<% String username=(String)request.getAttribute("username"); %> 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM