日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttrib

發(fā)布時(shí)間:2023/12/8 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttrib 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
setAttribute這個(gè)方法,在JSP內(nèi)置對(duì)象session和request都有這個(gè)方法,這個(gè)方法作用就是保存數(shù)據(jù),然后還可以用getAttribute方法來取出。
比如現(xiàn)在又個(gè)User對(duì)象,User curruser = new User("zhangsan", 20, "男");
1,request.setAttribute(“curruser”, curruser)這個(gè)方法是將curruser這個(gè)對(duì)象保存在request作用域中,然后在轉(zhuǎn)發(fā)進(jìn)入的頁(yè)面就可以獲取到你的值,如果你會(huì)一些框架的話,那些框架標(biāo)簽也可以獲取到,比如struts標(biāo)簽,還有jstl。如果這你都不會(huì)的話,那么你可以在jsp頁(yè)面編寫java小腳本來獲取:<% User myuser = (User)request.getAttribute("curruser")%>,在jsp頁(yè)面顯示值:<%=myuser.getName()%>。 如果是請(qǐng)求轉(zhuǎn)發(fā)的話可以用request.getRequestpatchcher( ).forword(request,response),是攜帶數(shù)值的,一直請(qǐng)求轉(zhuǎn)發(fā),一直獲得值,但是重定向的話request.setAttribute只針對(duì)一次請(qǐng)求是不攜帶值的,如果想兩次請(qǐng)求或多次請(qǐng)求的話可以request.getsessio.setAttribute( )可以先在session中保存之后再獲得就可以。也可以看地址欄的變化
2,session.setAttribute("curruser", curruser)。這個(gè)方法和上面唯一的區(qū)別就是作用域,就是在你整個(gè)程序啟動(dòng)的時(shí)候,如果在session中保存了數(shù)據(jù),那么在你這個(gè)無論你在哪個(gè)頁(yè)面,在什么時(shí)候都可以獲取到這個(gè)值,全局的,只要你的這個(gè)程序是啟動(dòng)的。session默認(rèn)的過期時(shí)間是30分鐘,過期無效,可以去修改這個(gè)值。 ************************************************************************************************************************************** getAttribute表示從request范圍取得設(shè)置的屬性,必須要先setAttribute設(shè)置屬性,才能通過getAttribute來取得,設(shè)置與取得的為Object對(duì)象類型?
getParameter表示接收參數(shù),參數(shù)為頁(yè)面提交的參數(shù),包括:表單提交的參數(shù)、URL重寫(就是xxx?id=1中的id)傳的參數(shù)等,因此這個(gè)并沒有設(shè)置參數(shù)的方法(沒有setParameter),而且接收參數(shù)返回的不是Object,而是String類型

HttpServletRequest類既有g(shù)etAttribute()方法,也由getParameter()方法,這兩個(gè)方法有以下區(qū)別:

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

(2)當(dāng)兩個(gè)Web組件之間為鏈接關(guān)系時(shí),被鏈接的組件通過getParameter()方法來獲得請(qǐng)求參數(shù),例如假定welcome.jsp和authenticate.jsp之間為鏈接關(guān)系,welcome.jsp中有以下代碼:

<a? href="authenticate.jsp?username=weiqin">authenticate.jsp? </a>

或者:

<form? name="form1"? method="post"? action="authenticate.jsp">
?? 請(qǐng)輸入用戶姓名:<input? type="text"? name="username">
?? <input? type="submit"? name="Submit"? value="提交">
</form>

在authenticate.jsp中通過request.getParameter("username")方法來獲得請(qǐng)求參數(shù)username:

<%? String? username=request.getParameter("username");? %>

(3)當(dāng)兩個(gè)Web組件之間為轉(zhuǎn)發(fā)關(guān)系時(shí),轉(zhuǎn)發(fā)目標(biāo)組件通過getAttribute()方法來和轉(zhuǎn)發(fā)源組件共享request范圍內(nèi)的數(shù)據(jù)。假定? authenticate.jsp和hello.jsp之間為轉(zhuǎn)發(fā)關(guān)系。authenticate.jsp希望向hello.jsp傳遞當(dāng)前的用戶名字,? 如何傳遞這一數(shù)據(jù)呢?先在authenticate.jsp中調(diào)用setAttribute()方法:

<%
String? username=request.getParameter("username");
request.setAttribute("username",username);
%>
<jsp:forward? page="hello.jsp"? />

在hello.jsp中通過getAttribute()方法獲得用戶名字:

<%? String? username=(String)request.getAttribute("username");? %>
Hello:? <%=username? %>

從更深的層次考慮,request.getParameter()方法傳遞的數(shù)據(jù),會(huì)從Web客戶端傳到Web服務(wù)器端,代表HTTP請(qǐng)求數(shù)據(jù)。request.getParameter()方法返回String類型的數(shù)據(jù)。

request.setAttribute()和getAttribute()方法傳遞的數(shù)據(jù)只會(huì)存在于Web容器內(nèi)部,在具有轉(zhuǎn)發(fā)關(guān)系的Web組件之間共享。這兩個(gè)方法能夠設(shè)置Object類型的共享數(shù)據(jù)。

request.getParameter()取得是通過容器的實(shí)現(xiàn)來取得通過類似post,get等方式傳入的數(shù)據(jù),,? request.setAttribute()和getAttribute()只是在web容器內(nèi)部流轉(zhuǎn),僅僅是請(qǐng)求處理階段,這個(gè)的確是正解.

getAttribute是返回對(duì)象,getParameter返回字符串

request.getAttribute()方法返回request范圍內(nèi)存在的對(duì)象,而request.getParameter()方法是獲取http提交過來的數(shù)據(jù)。 --------------------------------------------------------------------------------------------------------------------------------

1.session.setAttribute()和session.getAttribute()配對(duì)使用,作用域是整個(gè)會(huì)話期間,在所有的頁(yè)面都使用這些數(shù)據(jù)的時(shí)候使用。

2.request.setAttribute()和request.getAttribute()配對(duì)使用,作用域是請(qǐng)求和被請(qǐng)求頁(yè)面之間。request.setAttribute()是只在此action的下一個(gè)forward需要使用的時(shí)候使用;request.getAttribute()表示從request范圍取得設(shè)置的屬性,必須要先setAttribute設(shè)置屬性,才能通過getAttribute來取得,設(shè)置與取得的為Object對(duì)象類型。其實(shí)表單控件中的Object的 name與value是存放在一個(gè)哈希表中的,所以在這里給出Object的name會(huì)到哈希表中找出對(duì)應(yīng)它的value。setAttribute()的參數(shù)是String和Object。

3.request.getParameter()表示接收參數(shù),參數(shù)為頁(yè)面提交的參數(shù)。包括:表單提交的參數(shù)、URL重寫(就是xxx?id=1中的id)傳的參數(shù)等,因此這個(gè)并沒有設(shè)置參數(shù)的方法(沒有setParameter()),而且接收參數(shù)返回的不是Object,而是String類型。

舉例:

session.setAttribute("kindresult", result);??? //result 為StringBuffer類型對(duì)象

response.sendRedirect("../manage_kind.jsp");

在manage_kind.jsp中:

<%???StringBuffer kindresult=new StringBuffer();kindresult=(StringBuffer)session.getAttribute("kindresult");%>

<%=kindresult%>

##################################################################

我在servlet中使用了request.setAttribute()存儲(chǔ)信息。語(yǔ)法如下:request.setAttribute("user","1234");然后 response.sendRedirect("/hello.jsp");但是在我的hello.jsp中 request.getAttribute("user");返回值為null,為什么沒有取到String "1234"?

在這里就要注意了,sendRedirect不能傳遞request對(duì)象。使用request.setAttribute時(shí)不能使redirect而是forward。即是將請(qǐng)求轉(zhuǎn)發(fā)而不是重定向。

你可以使用getServletContext().getRequestDispatcher("/hello.jsp").forward(request,response)轉(zhuǎn)到hello.jsp頁(yè)面,對(duì)客戶端而言它意識(shí)不到是hello.jsp頁(yè)面響應(yīng)它。request對(duì)象和response對(duì)象是一樣的,當(dāng)然你的參數(shù)就可以傳遞過去了。

你使用response.sendRedirect("/hello.jsp");轉(zhuǎn)到hello.jsp之后,request對(duì)象是新建的,你的屬性值自然沒有了。但是如果你使用session代替request就還是可以的。session.setAttribute("user","1234");session.getAttribute("user");

#####################################################################################

4.request.getParameter() 和request.getAttribute() 區(qū)別

(1)request.getParameter()取得是通過容器的實(shí)現(xiàn)來取得通過類似post,get等方式傳入的數(shù)據(jù),request.setAttribute()和getAttribute()只是在web容器內(nèi)部流轉(zhuǎn),僅僅是請(qǐng)求處理階段。

(2)request.getParameter()方法傳遞的數(shù)據(jù),會(huì)從Web客戶端傳到Web服務(wù)器端,代表HTTP請(qǐng)求數(shù)據(jù)。request.getParameter()方法返回String類型的數(shù)據(jù)。

request.setAttribute()和getAttribute()方法傳遞的數(shù)據(jù)只會(huì)存在于Web容器內(nèi)部

還有一點(diǎn)就是,HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法。

拿一個(gè)例子來說一下吧,假如兩個(gè)WEB頁(yè)面間為鏈接關(guān)系時(shí),就是說要從1.jsp鏈接到2.jsp時(shí),被鏈接的是2.jsp可以通過getParameter()方法來獲得請(qǐng)求參數(shù).

假如1.jsp里有

<form name="form1" method="post" action="2.jsp">請(qǐng)輸入用戶姓名:<input type="text" name="username"><input type="submit" name="Submit" value="提交"></form>

的話在2.jsp中通過request.getParameter("username")方法來獲得請(qǐng)求參數(shù)username:

< % String username=request.getParameter("username"); %>

但是如果兩個(gè)WEB間為轉(zhuǎn)發(fā)關(guān)系時(shí),轉(zhuǎn)發(fā)目的WEB可以用getAttribute()方法來和轉(zhuǎn)發(fā)源WEB共享request范圍內(nèi)的數(shù)據(jù),也還是說一個(gè)例子吧。

有1.jsp和2.jsp

1.jsp希望向2.jsp傳遞當(dāng)前的用戶名字,如何傳遞這一數(shù)據(jù)呢?先在1.jsp中調(diào)用如下setAttribute()方法:

<%String username=request.getParameter("username");request.setAttribute("username",username);%>

<jsp:forward page="2.jsp" />

在2.jsp中通過getAttribute()方法獲得用戶名字:<% String username=(String)request.getAttribute("username"); %>

5.request.getAttribute()與request.setAttribute()

request.getAttribute("nameOfObj")可得到JSP頁(yè)面一表單中控件的Value。其實(shí)表單控件中的Object的 name與value是存放在一個(gè)哈希表中的,所以在這里給出Object的name會(huì)到哈希表中找出對(duì)應(yīng)它的value。

而不同頁(yè)面間傳值使用request.setAttribute(position, nameOfObj)時(shí),只會(huì)從a.jsp到b.jsp一次傳遞,之后這個(gè)request就會(huì)失去它的作用范圍,再傳就要再設(shè)一個(gè) request.setAttribute()。而使用session.setAttribute()會(huì)在一個(gè)過程中始終保有這個(gè)值。

P.S:JavaScript與JSP中不能相互傳值,因?yàn)镴avaScript運(yùn)行在客戶端,而JSP運(yùn)行在服務(wù)器端。若想使它們之間可以相互傳遞參數(shù),可以在JSP中設(shè)置一個(gè)hidden控件,用它的value結(jié)合上面所說的用法來傳遞所需的數(shù)值。

總結(jié)

以上是生活随笔為你收集整理的request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttrib的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。