欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解

 更新時(shí)間:2021年08月16日 15:06:48   作者:不過是技術(shù)  
這篇文章主要介紹了jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

getParameter 是用來接受用post個(gè)get方法傳遞過來的參數(shù)的.
getAttribute 必須先setAttribute.

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

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

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

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

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

假如1.jsp里有

Html代碼

<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")方法來獲得請求參數(shù)username:

Html代碼

< % 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()方法:

Html代碼

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

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

Html代碼

<% String username=(String)request.getAttribute("username"); %>  
  1. HttpServletRequest 類有setAttribute()方法,而沒有setParameter()方法
  2. 當(dāng)兩個(gè)Web組件之間為鏈接關(guān)系時(shí),被鏈接的組件通過getParameter()方法來獲得請求參數(shù),
  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ù)。

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

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

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

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

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

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

getParameter可以得到頁面?zhèn)鱽淼膮?shù)如?id=123之類的。

getAttribute()常用于servlet頁面?zhèn)鬟f參數(shù)給jsp

到此這篇關(guān)于jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解的文章就介紹到這了,更多相關(guān)jsp request.getParameter() 和request.getAttribute()方法區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論