Request的getParameter和getAttribute方法的差别
HttpServletRequest.getParameter("modelName");能取到想要的modelObject吗?经过測试之后。发现是不能的。
后来想想。其它道理挺简单的,当两个Web组件之间为转发关系时,转发源会将要共享request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过getAttribute方法来取得要共享的数据。而MVC中用的就是Web组件之间的转发啊!真是笨。怎么当时没有想到呢?
以下整理一下getParameter和getAttribute的差别和各自的使用范围。
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求參数,比如假定welcome.jsp和authenticate.jsp之间为链接关系。welcome.jsp中有下面代码:
<a href="authenticate.jsp?username=wolf">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传递当前的username字, 怎样传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:
<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>
<jsp:forward page="hello.jsp" />
在hello.jsp中通过getAttribute()方法获得username字:
<% String username=(String)request.getAttribute("username"); %>
Hello: <%=username %>
从更深的层次考虑,request.getParameter()方法传递的数据。会从Webclient传到Webserver端。代表HTTP请求数据。request.getParameter()方法返回String类型的数据。
request.setAttribute()和getAttribute()方法传递的数据仅仅会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法可以设置Object类型的共享数据。
request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。
request.setAttribute()和getAttribute()不过在web容器内部流转,不过请求处理阶段。
getAttribute是返回对象,getParameter返回字符串
总的来说:request.getAttribute()方法返回request范围内存在的对象。而request.getParameter()方法是获取http提交过来的数据。
Request的getParameter和getAttribute方法的差别的更多相关文章
- Request的getParameter和getAttribute方法的区别
下面整理一下getParameter和getAttribute的区别和各自的使用范围. (1)HttpServletRequest类有setAttribute()方法,而没有setParam ...
- request中getParameter和getAttribute的区别
整理一下getParameter和getAttribute的区别和各自的使用范围. (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方 ...
- 【转】HttpServletRequest.getParameter() &HttpServletRequest.getAttribute() 区别
Ref: HttpServletRequest的getParameter和getAttribute方法有什么区别 具体如下几点: (1)HttpServletRequest类有setAttribute ...
- request.getAttribute()和request.getParameter()两个方法的区别
request.getAttribute()获得的数据必须曾经有过setAttibute()过: 而request.getParameter()获得是客户端POST或者GET请求时所携带的参数的值 g ...
- request:getParameter和getAttribute区别
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...
- request:getParameter getAttribute
转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...
- JSP中request getParameter和getAttribute不同(转载)
(1)request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,request.setAttribute()和getAttribute()只是在 ...
- JSP中getParameter和getAttribute区别
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...
- Servlet中的request对象、重定向和转发的差别(6)
1.这里所说的request对象指的是HttpServletRequest对象,它代表client的请求,当client通过HTTP协议訪问server时.HTTP请求头中的全部信息都封装在这个对象中 ...
随机推荐
- 移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇
假设你的目标測试app有非常多imageview组成的话,这个时候monkeyrunner的截图比較功能就体现出来了. 而其它几个流行的框架如Robotium,UIAutomator以及Appium都 ...
- Android框架简要介绍
1. Android架构直观图 下图展示了Android系统的主要组成部分: 总体上而言,Android系统结构由5个部分组成.从上到下,别人是Applications (Android应用 ...
- 2014年湖北省TI杯大学生电子设计竞赛论文格式
2014年湖北省TI杯大学生电子设计竞赛 B题:金属物体探測定位器(本科) 2014年8月15日 文件夹 1 系统方案 1.1 XXX的论证与选择........................... ...
- 2016.03.02,英语,《Vocabulary Builder》Unit 03
ambi/amphi: 指on both sides或者around的意思,ambi-来自拉丁语,amphi-来自希腊语.ambidextrous:[ˌæmbi'dekstrəs] adj. 两手俱利 ...
- python网络编程三次握手和四次挥手
TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法可以防止产生错误的 ...
- [转]C# 位域[flags]
.NET中的枚举我们一般有两种用法,一是表示唯一的元素序列,例如一周里的各天:还有就是用来表示多种复合的状态.这个时候一般需要为枚举加上[Flags]特性标记为位域,例如: [Flags] enu ...
- input上传文件检测文件大小
前几天在做 input[type='file'] 上传图片时,需要检测上传文件的内存大小,写了一个小demo,在此做一总结: <!DOCTYPE html> <html lang=& ...
- javascript动画函数封装(升级版)
//把 任意对象 的 任意数值属性 改变为 任意的目标值 function animate(obj, json, fn) { clearInterval(obj.timer); obj.timer = ...
- const使用总结
1.常变量: const 类型说明符 变量名 const int a; 常引用: const 类型说明符 &引用名 const int &a; 常对象: 类名 cons ...
- 七牛上图片总是net::ERR_NAME_NOT_RESOLVED
七牛上图片总是net::ERR_NAME_NOT_RESOLVED >> php这个答案描述的挺清楚的:http://www.goodpm.net/postreply/php/101000 ...