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()的联系与区别(记录)的更多相关文章

  1. request.setAttribute()和session.setAttribute()的区别详解

    我们在Servlet和页面间传值时,经常会用到request.setAttribute()和session.setAttribute(),下面是两段示例用法 request.setAttribute( ...

  2. 分享知识-快乐自己:Struts2中 获取 Request和Session

    目录结构: POM: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnco ...

  3. struts2在action中获取request、session、application,并传递数据

    假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...

  4. request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  5. request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  6. 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别

    protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...

  7. request.getSession().setAttribute(&quot;&quot;,..)和request.setAttribute(&quot;&quot;,...)的差别

    request.getSession.setAttribute()是获得当前会话的session,然后再setAttribute到session里面去,有效范围是session而不是request. ...

  8. Request与session与application的区别

    (1)request的setAttribute与getAttribute方法一般都是成对出现的,首先通过setAttribute方法设置属性与属性值,然后通过getAttribute方法根据属性获取到 ...

  9. web初学之request,session与application

    request (1)request的setAttribute()与getAttribute()方法一般都是成对出现,首先通过setAttribute()方法设置属性与属性值,然后通过getAttri ...

随机推荐

  1. redis从入门到放弃 -> 部署方案

    单点部署方案 环境准备: [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@ ...

  2. docker stack 部署 filebeat

    =============================================== 2018/7/21_第3次修改                       ccb_warlock 更新 ...

  3. Python匿名函数详解

    python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的. 在Python中,lambda ...

  4. python类中的私有方法

    假设有如下一个python类: class Foo(object): def __a(self): print "Bet you can't see me..." def bar( ...

  5. php内存管理机制与垃圾回收机制

    PHP内存管理机制 1 var_dump(memory_get_usage()); //获取内存 2 $a = "laruence"; //定义一个变量 3 var_dump(me ...

  6. Java容器---迭代器

    任何容器类,都必须有某种方式可以插入元素并将它们再次取回.毕竟,持有事物是容器最基本的工作. 对于List, add0是插入元素的方法之一,而get()是取出元素的方法之一. 如果从更高层的角度思考, ...

  7. hdu 5078(2014鞍山现场赛 I题)

    数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 ...

  8. centos7 配置ftp访问

    vsftp安装 创建用户 并限定用户在自己的目录 1.查看是否已经安装了vsftpd vsftpd -version 2.安装vsftpd(CentOS7) yum install -y vsftpd ...

  9. Java学习(正则表达式、Date类、DateFormat类、Calendar类)

    一.正则表达式 1.概念:英语:Regular Expression,在代码中常简写为regex.正则表达式,是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串. ...

  10. Ionic Js十七:侧栏菜单

    一个容器元素包含侧边菜单和主要内容.通过把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换. 效果图如下所示:   用法 要使用侧栏菜单,添加一个父元素,一个中间内容 ,和一个或更 ...