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. base64转图片上传

    不成功,但是有一定的借鉴性 /** * @param base64Codes * 图片的base64编码 */ function sumitImageFile(base64Codes){ consol ...

  2. h5中的video与audio

    ·首先带大家熟悉一下video标签的属性方法,根据属性方法做一个小demo, HTML5支持的视频格式: Ogg 带有Theora视频编码+Ogg文件 支持的浏览器:F.O MEPG4  带有H.26 ...

  3. JZOJ 5347. 遥远的金字塔

    Description Input Output Sample Input 5 3 1 6 1 5 3 5 4 4 4 4 Sample Output 15 Data Constraint 做法: 其 ...

  4. linux 的安装

    3linux 软件安装 3.1 vm ware 软件安装 双击VMware-workstation-full-10.0.2-1744117.1398244508.exe 单击下一步 单击下一步 选择典 ...

  5. 【STM32】IIC的基本原理(实例:普通IO口模拟IIC时序读取24C02)(转载)

     版权声明:本文为博主原创文章,允许转载,但希望标注转载来源. https://blog.csdn.net/qq_38410730/article/details/80312357 IIC的基本介绍 ...

  6. [GDOI2016][树链剖分+主席树]疯狂动物城

    题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...

  7. Detecting iOS

    Detecting iOS I am not a fan of User Agent sniffing, but here is how you would do it: var iOS = /iPa ...

  8. loj2063 「HAOI2016」字符合并

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  9. StartWith 测试

    var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...

  10. nsfwjs鉴黄识别最小化案例

    3个月前,也就是2月份左右吧,Github上出现一个开源项目: Infinite Red, Inc.工作室宣布开源旗下基于tensorflow的tfjs的鉴黄小工具 据说是从15000张图片中 进行机 ...