濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解

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

熱門標(biāo)簽:自繪地圖標(biāo)注數(shù)據(jù) 外呼系統(tǒng)使用方法 給地圖標(biāo)注得傭金 海外圖書館地圖標(biāo)注點(diǎn) 潤(rùn)滑油銷售電銷機(jī)器人 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 電銷機(jī)器人免培訓(xùn) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 如何看懂地圖標(biāo)注點(diǎn)

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),僅僅是請(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里有

Html代碼

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:

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()方法來獲得請(qǐng)求參數(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è)請(qǐng)求,簡(jiǎn)單說就是你在頁(yè)面上的一個(gè)操作, request.getParameter()就是從上一個(gè)頁(yè)面中的url、form中獲取參數(shù),但如果一個(gè)request涉及多個(gè)類,后面還要取參數(shù), 可以用request.setAttribute()和request.getAttribute(),但是當(dāng)結(jié)果輸出之后,request就結(jié)束了。

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

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

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

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

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

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

您可能感興趣的文章:
  • java request.getParameter中文亂碼解決方法
  • JSP用過濾器解決request getParameter中文亂碼問題
  • request.getParameter()取值為null的解決方法
  • js獲得參數(shù)的getParameter使用示例
  • 基于request.getAttribute與request.getParameter的區(qū)別詳解
  • Java getParameter方法案例詳解

標(biāo)簽:黃石 銅川 大連 貸款邀約 南京 內(nèi)江 樂山 廣州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解》,本文關(guān)鍵詞  jsp,request.getParameter,和,request.getAttribute,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于jsp request.getParameter() 和request.getAttribute()方法區(qū)別詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    井研县| 滦平县| 天水市| 延安市| 古浪县| 屏山县| 呼和浩特市| 来安县| 朝阳县| 丹寨县| 竹溪县| 东至县| 阿瓦提县| 嘉黎县| 武鸣县| 康保县| 溧水县| 乌审旗| 蒙城县| 马山县| 台山市| 陵川县| 湘阴县| 修文县| 施甸县| 三江| 贡觉县| 洪湖市| 海阳市| 阳泉市| 大名县| 嘉义县| 遵义县| 漳平市| 丹棱县| 铅山县| 蕉岭县| 日土县| 万荣县| 遂昌县| 确山县|