request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用。
2.request.setAttribute()和request.getAttribute()配对使用,作用域是请求和被请求页面之间。request.setAttribute()是只在此action的下一个forward需要使用的时候使用;request.getAttribute()表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型。其实表单控件中的Object的 name与value是存放在一个哈希表中的,所以在这里给出Object的name会到哈希表中找出对应它的value。setAttribute()的参数是String和Object。
3.request.getParameter()表示接收参数,参数为页面提交的参数。包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter()),而且接收参数返回的不是Object,而是String类型。
举例:
session.setAttribute("kindresult", result); //result 为StringBuffer类型对象
response.sendRedirect("../manage_kind.jsp");
在manage_kind.jsp中:
<%
StringBuffer kindresult=new StringBuffer();
kindresult=(StringBuffer)session.getAttribute("kindresult");
%>
<%=kindresult%>
##################################################################
我在servlet中使用了request.setAttribute()存储信息。
语法如下:request.setAttribute("user","1234");
然后 response.sendRedirect("/hello.jsp");
但是在我的hello.jsp中 request.getAttribute("user");
返回值为null,为什么没有取到String "1234"?
在这里就要注意了,sendRedirect不能传递request对象。使用request.setAttribute时不能使redirect而是forward。即是将请求转发而不是重定向。
你可以使用getServletContext().getRequestDispatcher("/hello.jsp").forward(request,response)转到hello.jsp页面,对客户端而言它意识不到是hello.jsp页面响应它。
request对象和response对象是一样的,当然你的参数就可以传递过去了。
你使用response.sendRedirect("/hello.jsp");转到hello.jsp之后,request对象是新建的,你的属性值自然没有了。但是如果你使用session代替request就还是可以的。
session.setAttribute("user","1234");
session.getAttribute("user");
#####################################################################################
4.request.getParameter() 和request.getAttribute() 区别
(1)request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。
(2)request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。
request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部
还有一点就是,HttpServletRequest类有setAttribute()方法,而没有setParameter()方法。
拿一个例子来说一下吧,假如两个WEB页面间为链接关系时,就是说要从1.jsp链接到2.jsp时,被链接的是2.jsp可以通过getParameter()方法来获得请求参数.
假如1.jsp里有
<form name="form1" method="post" action="2.jsp">
请输入用户姓名:<input type="text" name="username">
<input type="submit" name="Submit" value="提交">
</form>
的话在2.jsp中通过request.getParameter("username")方法来获得请求参数username:
< % String username=request.getParameter("username"); %>
但是如果两个WEB间为转发关系时,转发目的WEB可以用getAttribute()方法来和转发源WEB共享request范围内的数据,也还是说一个例子吧。
有1.jsp和2.jsp
1.jsp希望向2.jsp传递当前的用户名字,如何传递这一数据呢?先在1.jsp中调用如下setAttribute()方法:
<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>
<jsp:forward page="2.jsp" />
在2.jsp中通过getAttribute()方法获得用户名字:
<% String username=(String)request.getAttribute("username"); %>
5.request.getAttribute()与request.setAttribute()
request.getAttribute("nameOfObj")可得到JSP页面一表单中控件的Value。其实表单控件中的Object的 name与value是存放在一个哈希表中的,所以在这里给出Object的name会到哈希表中找出对应它的value。
而不同页面间传值使用request.setAttribute(position, nameOfObj)时,只会从a.jsp到b.jsp一次传递,之后这个request就会失去它的作用范围,再传就要再设一个 request.setAttribute()。而使用session.setAttribute()会在一个过程中始终保有这个值。
P.S:JavaScript与JSP中不能相互传值,因为JavaScript运行在客户端,而JSP运行在服务器端。若想使它们之间可以相互传递参数,可以在JSP中设置一个hidden控件,用它的value结合上面所说的用法来传递所需的数值。
request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系的更多相关文章
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- request.setAttribute()和session.setAttribute()的区别详解
我们在Servlet和页面间传值时,经常会用到request.setAttribute()和session.setAttribute(),下面是两段示例用法 request.setAttribute( ...
- session.setAttribute和session.getAttribute
网上搜了些资料 ----------------------------------------------------------------------------- B/S架构中,客户端与服务器 ...
- request.getParameter()与request.setAttribute()的区别 (转载)
request.getParameter()与request.setAttribute()的区别 request.getParameter(),request.setAttribute()区别如下: ...
- request.getParameter与request.getAttribute()
这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...
- request.getParameter() 和request.getAttribute() 区别
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...
- st.getParameter() 和request.getAttribute() 区别 https://terryjs.iteye.com/blog/1317610
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute. (1)request.getParameter() 取得是通过容 ...
- 〖转〗request.getparameter()和request.getAttribute()的区别
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...
随机推荐
- 创建mysql数据库的时候指定编码
CREATE DATABASE xxx DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- Ubuntu 12.04下安装thrift 0.9
Thrift这里就不介绍了,只说一句--Facebook很牛逼. 我这里安装Thrift主要是为Accumulo数据库作准备,所以java语言为必选项. 具体安装参考官方Apache Thrift R ...
- iOS之UITableView的上拉刷新
#import "ViewController.h" #import "UITableView+PullRefresh.h" @interface ViewCo ...
- iOS10适配——错误:Code=3000
error : Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserIn ...
- jQuery修改css属性
jQuery CSS 操作jQuery 拥有三种用于 CSS 操作的重要函数:$(selector).css(name,value)$(selector).css({properties})$(sel ...
- iOS开发——pch文件创建
新换的公司,接手的项目里面连pch文件都没有,每次需要用到屏幕的宽高时,都是现写.今天既然碰到了,就把PCH这个玩意说一下. 1.Command+N,打开新建文件窗口:iOS->Other-&g ...
- delphi 中COPY()函数的意思
Trim(copy(m,11,5)):copy里面的3个参数(m,11,5)分别是什么意思?COPY还有其他的参数吗? m :就是copy源,就是一个字符串,表示你将要从m里copy一些东西11 : ...
- ucos事件邮箱信号量队列详解
Ucos的事件分为时钟,信号量,互斥性信号量,消息队列,以及消息邮箱 首先说信号量 信号量在ucos中的类型定义为OS_EVENT_TYPE_SEM,在任务控制块ecb中,主要是用到的是信号量计数器O ...
- 2017 ZSTU寒假排位赛 #2
题目链接:https://vjudge.net/contest/147632#overview. A题,状态压缩一下然后暴力即可. B题,水题,略过. C题,有负数,前缀和不是单调的,因此不能用尺取法 ...
- IOS开发-UI学习-NSMutableAttributedString(带属性的字符串)的使用
带属性的字符串: NSString *aa = @"hellochinaIloveYou!"; NSMutableAttributedString *mas = [[NSMutab ...