request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
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()、session.setAttribute()和request.getParameter()的联系与区别的更多相关文章
- request.setAttribute()和session.setAttribute()的区别详解
我们在Servlet和页面间传值时,经常会用到request.setAttribute()和session.setAttribute(),下面是两段示例用法 request.setAttribute( ...
- 分享知识-快乐自己:Struts2中 获取 Request和Session
目录结构: POM: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnco ...
- struts2在action中获取request、session、application,并传递数据
假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...
- request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别
protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...
- request.getSession().setAttribute("",..)和request.setAttribute("",...)的差别
request.getSession.setAttribute()是获得当前会话的session,然后再setAttribute到session里面去,有效范围是session而不是request. ...
- Request与session与application的区别
(1)request的setAttribute与getAttribute方法一般都是成对出现的,首先通过setAttribute方法设置属性与属性值,然后通过getAttribute方法根据属性获取到 ...
- web初学之request,session与application
request (1)request的setAttribute()与getAttribute()方法一般都是成对出现,首先通过setAttribute()方法设置属性与属性值,然后通过getAttri ...
随机推荐
- HTTP协议原理
HTTP是一个客户端终端(用户)和服务器端(网站)请求和应答的标准(TCP).通过使用网页浏览器.网络爬虫或者其它的工具,客户端发起一个HTTP请求到服务器上指定端口(默认端口为80).我们称这个客户 ...
- ATM-core-src
from interface import bank, shopping, userfrom lib import common user_data = { 'name': None} def log ...
- 【CSS】CSS 的优先级总结
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- php扩展开发-快速上手
系统环境CentOS release 6.5 (Final) PHP版本php-5.6.27 扩展开发需要有php环境及php的源代码,我的PHP安装目录/home/zhangxiaomin/stud ...
- python中生成器对象和return 还有循环的区别
python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...
- python编写定时执行脚本
前几天在抓博客园文章,打算每天抓10条最新的,所以就在脚本中加了定时让它在每天凌晨四点中时执行,但是昨天发现,报错了: 显示是远程主机强制关闭了一个链接, 原因是:mysql数据库默认当连续8小时不对 ...
- 处理IE6下PNG图片透明背景问题
由于历史原因,IE较早的版本不支持PNG透明 可以支持GIF等的透明 由于png图片相对较小,所以很多网站还是青睐于PNG图片 最近就遇到这种情况,使用js和css滤镜来实现的与大家分享一下下: 首先 ...
- MySQL安装教程&Navicat安装
一.下载MySQL http://jingyan.baidu.com/article/e3c78d64412ae83c4c85f5fd.html 首先打开MySQL官网,找到Downloads标签,点 ...
- Intellij IDEA快捷键大全
Intellij IDEA快捷键大全 Intellij IDEA这个工具有些方面确实比较优秀,使用了一段时间的IntelliJ IDEA,感觉这个JAVA IDE非常好用!比如javascript自动 ...
- Java之OutOfMemoryError简单分析
Java之OutOfMemoryError简单分析 最近编码遇到了Java内存溢出的问题,所以就想顺便总结一下几种导致Java内存溢出的栗子,以及碰到Java内存溢出要如何去解决. Java堆溢出 J ...