getParameter getAttribute
URL:http://localhost:8888/Test/index.jsp?test=123
<body>
${test}
${requestScope.test}
<%request.getAttribute("test"); %>
</body>
以上代码均不能取出值
仅当 使用
<%
request.setAttribute("test", "123");
%>
赋值时<body/>内可以正常取出值
那么如何取出URL 中的test 的值呢?如下
<body>
${param.test}
<%=request.getParameter("test") %>
</body>
均可取出URL中的test的值。。
结论:
${param.name} 等价于 request.getParamter("name"),这两种方法一般用于服务器从页面或者客户端获取的内容。
${requestScope.name} 等价于 request.getAttribute("name"),一般是从服务器传递结果到页面,在页面中取出服务器保存的值。
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类型的共享数据
简单来讲request.getParamenter()方法使用的是HTTP协议来传递数据,只能传递String类型的信息,而request.setAttribtute()方法传递数据是在WEB容器中传递,可以转发任意类型的对象信息,比如一个listAction的servlet想传给list.jsp一个LIST集合,则必须使用setAttribute方法。
getParameter getAttribute的更多相关文章
- request:getParameter getAttribute
转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...
- request getParameter getAttribute
在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:8888/Test/index.jsp?test=123 在index.jsp中尝试使用EL表达式取出,代码如 ...
- request中getParameter和getAttribute的区别
整理一下getParameter和getAttribute的区别和各自的使用范围. (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方 ...
- getattribute()与getparameter()的区别
1.它们取到的值不同.getAttribute取到的是对象(object),而getParameter取到的是String. 2.数据传递路劲不同.request.getParameter方法传递的数 ...
- JSP中request getParameter和getAttribute不同(转载)
(1)request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,request.setAttribute()和getAttribute()只是在 ...
- getAttribute和getParameter的区别
2016年1月19日JSP中getParameter与getAttribute有何区别? ——getParameter得到的都是String类型的.或者是http://a.jsp?id=123中的12 ...
- J2EE中getParameter与getAttribute以及EL表达式${requestScope}和${param[]}
getParameter ① 得到的都是String类型的.如http://name.jsp?name=xy中的xy ② 获取POST/GET传递的参数值 ③ 用于客户端重定向,如点击链接或提交按扭时 ...
- request.getAttribute() 和 request.getParameter() 有何区别?
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...
- request.getParameter与request.getAttribute()
这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...
随机推荐
- ORA-00972_标识符过长
执行SQL查询报:"ORA-00972:标识符过长"错误. 执行SQL: SELECT T.F_FTBS, T.F_TZMC "X组/XXXX/XXXX名称", ...
- PHP 字符 正则表达式 +,*,?
1.“/”是定界符,“/”定界符之间的部分就是将要在目标对象中进行匹配的模式.同时为了正则更加灵活,引入了元字符,即“+”, “*”,以及 “?”. (1)“+”元字符规定其前导字符必须在目标对象中连 ...
- 在WIN7、WIN8中,将快捷方式锁定到任务栏,C#
其实很简单,使用 API 函数 ShellExecute,就可以解决这个问题. 首先添加引用 using System.Runtime.InteropServices; 代码如下: using Sys ...
- Java编程基础-异常
一.异常 1.什么是异常 在java中,程序在运行时出现的不正常情况称为异常,以异常类的形式对这些非正常情况进行封装,通过异常处理机制对程序运行时发生的各种问题进行处理.其实就是java对不正常情况进 ...
- CF1059B Forgery
思路: 若某个位置是‘.’,说明不能在周围的8个位置下笔.在所有可以下笔的位置填充一次,看能否“包含”需要的图案即可. 实现: #include <iostream> using name ...
- Ubuntu16.04 + cuda8.0 + GTX1080安装教程
1. 安装Ubuntu16.04 不考虑双系统,直接安装 Ubuntu16.04,从 ubuntu官方 下载64位版本: ubuntu-16.04-desktop-amd64.iso . 在MAC下制 ...
- Android 实现九宫格、点击图片放大全屏浏览等
项目GitHub地址https://github.com/tikeyc/TNinePlaceGridView_Android https://github.com/tikeyc/TikeycAndro ...
- [windows]解决Win7访问Windows 2003、XP共享慢的问题
解决方法: 1. 修改网卡配置打开本地连接属性,点击"配置"在"高级"选项卡中,将"大型发送分载(IPv4)"的值设置成"禁用&q ...
- (三)mybatis之对Hibernate初了解
前言:为什么会写Hibernate呢?因为HIbernate跟Mybatis一样,是以ORM模型为核心思想的,但是这两者有相似的地方也有差异的地方.通过这两种框架的比对,可以对mybatis有着更深的 ...
- 用NSCoding协议完成“编码/解码”操作-Object-C
Archiving Objective-C Objects with NSCoding For the seasoned Cocoa developer, this is a piece of cak ...