request.setAttribute和request.getAttribute還有session.setAttribute和session.getAttribute還有request.getParameter和request.getAttribute以及request.getAttribute()與request.getSession().getAttribute()的區別和用法


以下內容作為個人學習,如果給原作者造成不便,請私信,會立即刪除。
參考https://www.cnblogs.com/zhangxue521/p/5783043.html以及https://blog.csdn.net/qq_43329749/article/details/105717815

1.session.setAttribute()和session.getAttribute()配對使用,作用域是整個會話期間,在所有的頁面都使用這些數據的時候使用。

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

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

舉例:

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

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


在manage_kind.jsp中:

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

<%=kindresult%>

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

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

在這里就要注意了,sendRedirect不能傳遞request對象。使用request.setAttribute時不能使redirect而是forward。即是將請求轉發而不是重定向。

你可以使用getServletContext().getRequestDispatcher("/hello.jsp").forward(request,response)轉到hello.jsp頁面,對客戶端而言它意識不到是hello.jsp頁面響應它。
 request對象和response對象是一樣的,當然你的參數就可以傳遞過去了。

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

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

4.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"); %>

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

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

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

P.S:JavaScript與JSP中不能相互傳值,因為JavaScript運行在客戶端,而JSP運行在服務器端。若想使它們之間可以相互傳遞參數,可以在JSP中設置一個hidden控件,用它的value結合上面所說的用法來傳遞所需的數值。

6.request.getAttribute()與request.getSession().getAttribute()

request.setAttribute和request.getAttribute以及request.getSession().setAttribute和request.getSession().getAttribute()一般是成對使用

不過它們的作用域不同,request.getAttribute()只能在一個request內有效,如果重定向回客戶端,將取不到值。使用 request.getSession().getAttribute() 就能取得到值。request.getSession()可以幫你得到HttpSession類型的對象,通常稱之為session對象,session對象的作用域為一次會話,通常瀏覽器不關閉,保存的值就還再,當然也會出現session超時。


免責聲明!

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



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