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请求头中的全部信息都封装在这个对象中 ...
随机推荐
- iOS 权限判断 跳转对应设置界面
相机权限 1.1 使用说明 在合适的地方导入#import <AVFoundation/AVFoundation.h> 使用AVAuthorizationStatus类获取当前权限状态 在 ...
- hdoj--1276--士兵队列训练问题(数组模拟)
士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- nginx的安装步骤
nginx学习资料;https://zhuanlan.zhihu.com/p/34943332 1.下载nginx的安装包:https://nginx.org/en/download.html 2. ...
- oracle 索引优化之distinct
11G R2环境: --DISTINCT测试前的准备drop table t purge;create table t as select * from dba_objects;update t se ...
- CentOS7开启网络配置
虚拟机在安装时可以开启网络 如果没有开启的话 可以通过以下操作 ip addr 查看是否开启网络 没有开启的话 cd /etc/sysconfig/network-scripts/ 然后 执行 ls ...
- POJ 1201 差分约束+SPFA
思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...
- kettle的下载、安装和初步使用(Ubuntu 16.04平台下)(图文详解)
不多说,直接上干货! 能够看我这篇博客的博友们,想必是已经具备一定基础了. 扩展博客 kettle的下载.安装和初步使用(windows平台下)(图文详解) kettle的下载 Kettle可以在h ...
- ubuntu16.04 安装 go
1,下载go安装包wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz 2,解压 sudo tar -C /usr ...
- SpringBoot(六) SpirngBoot与Mysql关系型数据库
pom.xml文件的配置 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- Java NIO(七)管道
Java NIO 管道是两个线程之间的单向数据连接.Pipe有一个source通道和sink通道(内部类).数据会被写到sink通道,从source通道读取. 给一张Pipe通道的原理图: 创建管道: ...