request.getParameter
是用來(lái)接受來(lái)自get方法或post方法的參數(shù)
<form method=post>
<form method=get>
<a href="1.jsp?id=1">ok</a>
只能接受java.lang.String
也就是說(shuō)String hotel_id = request.getParameter("hotel_id");
request.getAttribute
是用來(lái)接受來(lái)自servlet的變量或Action(其實(shí)Action就是特殊的Servlet)
在Action中,request.setAttribute("ret",ret);
只能接受java.lang.Object
也就是說(shuō)List ret = (List)request.getAttribute("ret");
一個(gè)是變量類(lèi)型,一個(gè)對(duì)象類(lèi)型
http協(xié)議中
get形式的請(qǐng)求的參數(shù)是在
url的 ? 后面
如 http://xxx.xxx.xxx/aaa.jsp?a=1
post形式的請(qǐng)求的參數(shù)是在請(qǐng)求的體里,沒(méi)有在http的頭部
-------------------------------------------
以下是get形式的請(qǐng)求 http協(xié)議部分
GET /aaa.jsp?a=1 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 1.1.4322)
Host: 127.0.0.1:8080
Connection: Keep-Alive
以下是post形式的請(qǐng)求 http協(xié)議部分
POST /aaa.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
Referer: http://127.0.0.1:8080/b.html
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 1.1.4322)
Host: 127.0.0.1:8080
Content-Length: 48
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=111
a=1
-------------------------------------
看到區(qū)別的吧
也有共同點(diǎn),這些參數(shù)的值都是保存在http請(qǐng)求部分
以上這種參數(shù)都需要通過(guò)request.getParameter()方法取得參數(shù)的值
=======================================================
而
request.getAttribute()方法所取得的值是保存在服務(wù)器端的
服務(wù)器端的4個(gè)內(nèi)置對(duì)象
page
request
session
application
中保存的數(shù)據(jù)都需要通過(guò) getAttribute()方法取得
引自:http://www.javathinker.org/bbs/topic.jsp?db=2&topic=76
HttpServletRequest類(lèi)既有g(shù)etAttribute()方法,也由getParameter()方法,這兩個(gè)方法有以下區(qū)別:
(1)HttpServletRequest類(lèi)有setAttribute()方法,而沒(méi)有setParameter()方法
(2)當(dāng)兩個(gè)Web組件之間為鏈接關(guān)系時(shí),被鏈接的組件通過(guò)getParameter()方法來(lái)獲得請(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)輸入用戶(hù)姓名:<input type="text" name="username">
<input type="submit" name="Submit" value="提交">
</form>
在authenticate.jsp中通過(guò)request.getParameter("username")方法來(lái)獲得請(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)組件通過(guò)getAttribute()方法來(lái)和轉(zhuǎn)發(fā)源組件共享request范圍內(nèi)的數(shù)據(jù)。假定authenticate.jsp和hello.jsp之間為轉(zhuǎn)發(fā)關(guān)系。authenticate.jsp希望向hello.jsp傳遞當(dāng)前的用戶(hù)名字,如何傳遞這一數(shù)據(jù)呢?先在authenticate.jsp中調(diào)用setAttribute()方法:
<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>
<jsp:forward page="hello.jsp" />
在hello.jsp中通過(guò)getAttribute()方法獲得用戶(hù)名字:
<% String username=(String)request.getAttribute("username"); %>
Hello: <%=username %>
從更深的層次考慮,request.getParameter()方法傳遞的數(shù)據(jù),會(huì)從Web客戶(hù)端傳到Web服務(wù)器端,代表HTTP請(qǐng)求數(shù)據(jù)。request.getParameter()方法返回String類(lèi)型的數(shù)據(jù)。
request.setAttribute()和getAttribute()方法傳遞的數(shù)據(jù)只會(huì)存在于Web容器內(nèi)部,在具有轉(zhuǎn)發(fā)關(guān)系的Web組件之間共享。這兩個(gè)方法能夠設(shè)置Object類(lèi)型的共享數(shù)據(jù)。
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/wirror800/archive/2009/04/04/4048764.aspx
posted on 2009-06-25 22:01
RoyPayne 閱讀(1165)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
jsp相關(guān)