自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

了解JSP中request屬性的用法

開發(fā) 后端
本文介紹JSP中request屬性的用法,以及request.getParameter() 和request.getAttribute() 區(qū)別。

一、request.getParameter() 和request.getAttribute() 區(qū)別

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里有

  1. <form name="form1" method="post" action="2.jsp"> 
  2. 請輸入用戶姓名:<input type="text" name="username"> 
  3. <input type="submit" name="Submit" value="提交"> 
  4. form> 

的話在2.JSP中通過request.getParameter("username")方法來獲得請求參數(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()方法:

  1. <%  
  2. String username=request.getParameter("username");  
  3. request.setAttribute("username",username);  
  4. %> 
  5. <jsp:forward page="2.jsp" /> 
  6. 在2.jsp中通過getAttribute()方法獲得用戶名字:  
  7. <% String username=(String)request.getAttribute("username"); %> 

二、request.getAttribute()與request.setAttribute()

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

而不同頁面間傳值使用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ù)值。

【編輯推薦】

  1. 簡單介紹JSP數(shù)據(jù)庫高級(jí)操作
  2. 實(shí)現(xiàn)JSP數(shù)據(jù)和JavaScript數(shù)據(jù)交互使用
  3. JSP和Servlet中的幾個(gè)編碼的作用及原理
  4. 如何解決JSP頁面顯示亂碼問題
  5. 在JSP中用JDBC連接各種數(shù)據(jù)庫
責(zé)任編輯:佚名 來源: builder
相關(guān)推薦

2009-03-17 16:18:51

JspActionStruts

2009-07-02 17:06:36

JSP中的PageEn

2010-09-07 14:40:10

title屬性Alt屬性CSS

2009-07-02 13:51:05

對象和范圍屬性

2010-08-25 08:57:33

marginpadding

2010-08-24 15:11:24

PositionCSS

2010-09-07 16:46:59

CSSexpression

2010-08-24 13:14:36

CSSmargin

2010-09-13 17:36:02

overflowCSS

2011-06-07 11:21:04

JSP隱含對象

2010-08-23 13:40:46

CSSpadding-bot

2010-08-27 11:10:30

CSSmargin

2010-09-01 11:21:18

CSSpositionfloat

2010-08-23 09:42:50

CSSPosition

2010-09-09 15:08:40

CSSfloatclear

2010-09-03 10:43:05

CSSmargin

2010-09-16 10:10:50

CSSdisplay

2009-07-02 14:27:53

JSP內(nèi)置對象

2010-08-17 10:31:10

DIV布局屬性

2011-06-22 14:14:27

pageEncodincontentType
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)