随笔-59  评论-31  文章-0  trackbacks-0

request.getParameter
是用来接受来自get方法或post方法的参数
<form method=post>
<form method=get>
<a href="1.jsp?id=1">ok</a>
只能接受java.lang.String
也就是说String hotel_id = request.getParameter("hotel_id");
request.getAttribute
是用来接受来自servlet的变量或Action(其实Action就是特殊的Servlet)
在Action中,request.setAttribute("ret",ret);
只能接受java.lang.Object
也就是说List ret = (List)request.getAttribute("ret");

一个是变量类型,一个对象类型

http协议中

get形式的请求的参数是在
url的 ? 后面
如 http://xxx.xxx.xxx/aaa.jsp?a=1

post形式的请求的参数是在请求的体里,没有在http的头部

-------------------------------------------
以下是get形式的请求 http协议部分

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形式的请求 http协议部分
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

-------------------------------------
看到区别的吧
也有共同点,这些参数的值都是保存在http请求部分
以上这种参数都需要通过request.getParameter()方法取得参数的值

=======================================================


request.getAttribute()方法所取得的值是保存在服务器端的
服务器端的4个内置对象
page
request
session
application
中保存的数据都需要通过 getAttribute()方法取得

引自:http://www.javathinker.org/bbs/topic.jsp?db=2&topic=76

HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:

(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

<a href="authenticate.jsp?username=weiqin">authenticate.jsp </a>

或者:

<form name="form1" method="post" action="authenticate.jsp">
   请输入用户姓名:<input type="text" name="username">
   <input type="submit" name="Submit" value="提交">
</form>

在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:

<% String username=request.getParameter("username"); %>

(3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>

<jsp:forward page="hello.jsp" />

在hello.jsp中通过getAttribute()方法获得用户名字:

<% String username=(String)request.getAttribute("username"); %>
Hello: <%=username %>

从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wirror800/archive/2009/04/04/4048764.aspx

posted on 2009-06-25 22:01 RoyPayne 阅读(1150) 评论(0)  编辑  收藏 所属分类: jsp相关

只有注册用户登录后才能发表评论。


网站导航: