一、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()方法。

拿一个例子来说一下吧,假如

j1.jsp里有

    1. <!--  j1与j2为链接关系-->
    2. <form  method="post" action="j3.jsp">
    3. 用户名:<input type="text" name="username">
    4. <input type="submit" name="submit" value="提交">
    5. </form>

的话在j2.jsp中通过request.getParameter("username")方法来获得请求参数username:

  1. <%
  2. String s = request.getParameter("username");
  3. %>
  4. 名字是:<%=s %>

或者j1.jsp中有

  1. <a href="2.jsp?username=accp">2.jsp</a>

在j2.jsp中通过request.getParameter("username")来获得请求参数username。

但是如果范围内的数据,也还是说一个例子吧。

有j1.jsp j2.jsp和j3.jsp,

j1.jsp月j3.jsp为链接关系,j3.jsp与j2.jsp为转发关系

从j1.jsp链接到j3.jsp时,被链接的是j3.jsp可以通过getParameter()方法来获得请求参数.

j3.jsp希望向j2.jsp传递从j1.jsp获取的用户名字,如何传递这一数据呢?先在j3.jsp中调用如下setAttribute()方法:

j1.jsp

  1. <form  method="post" action="j3.jsp">
  2. 用户名:<input type="text" name="username">
  3. <input type="submit" name="submit" value="提交">
  4. lt;/form>

j3.jsp

  1. <%
  2. String username=request.getParameter("username");
  3. request.setAttribute("username",username);
  4. %>
  5. <jsp:forward page="j2.jsp" />
  6. <!-- j1到j3,j3到j2是一个request对象, request.setAttribute(),给request对象设置一个属性,然后在另一个页面中获取 -->


j2.jsp

  1. <% String username=(String)request.getAttribute("username"); %>
  2. j2页面,用户名:<%=username %>


二、从更深的层次考虑

request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端。request.getParameter()方法返回String类型的数据。


request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。
getParameter得到的都是String类型的。或者是http://a.jspid=123中的123,或者是某个表单提交过去的数据。

getAttribute则可以是对象。
getParameter()是获取POST/GET传递的参数值;
getAttribute()是获取对象容器中的数据值;
getParameter:用于客户端重定向时,即点击了链接或提交按扭时传值用,即用于在用表单或url重定向传值时接收数据用。

getAttribute:用于服务器端重定向时,即在sevlet中使用了forward函数,或struts中使用了mapping.findForward。getAttribute只能收到程序用setAttribute传过来的值。

setAttribute是应用服务器把这个对象放在该页面所对应的一块内存中去,当你的页面服务器重定向到另一个页面时,应用服务器会把这块内存拷贝另
一个页面所对应的内存中。这样getAttribute就能取得你所设下的值,当然这种方法可以传对象。session也一样,只是对象在内存中的生命周
期不一样而已。

getParameter只是应用服务器在分析你送上来的request页面的文本时,取得你设在表单或url重定向时的值。

另外,JavaScript与JSP中不能相互传值,因为JavaScript运行在客户端,而JSP运行在服务器端,若想使他们之间可以相互传递参数,可以在JSP中设置一个hidden控件,用它的value结合上面的用法来传递所需要的参数。

request属性 request.getAttribute()的更多相关文章

  1. request:getParameter getAttribute

    转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...

  2. JSP中request getParameter和getAttribute不同(转载)

    (1)request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,request.setAttribute()和getAttribute()只是在 ...

  3. struts2视频学习笔记 15-17 (访问或添加request属性,文件上传)

    课时15 访问或添加request/session/application属性 1.简单说 page指当前页面.在一个jsp页面里有效 2.request 指从http请求到服务器处理结束,返回响应的 ...

  4. struts2中访问和添加Application、session以及request属性

    一.访问或添加Application.session.request属性 <一>方式一 HelloWorldAction类中添加如下代码 //此方法适用于仅对Application.ses ...

  5. 【Flask】关于Flask的request属性

    前言 在进行Flask开发中,前端需要发送不同的请求及各种带参数的方式,比如GET方法在URL后面带参数和POST在BODY带参数,有时候又是POST的表单提交方式,这个时候就需要从request提取 ...

  6. request:getParameter和getAttribute区别

    getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...

  7. java web中的session属性范围和request属性范围

    首先必需要了解client跳转和server端跳转的差别: client跳转: response.sendRedict(String path).地址栏发生改变. 不能传递request属性. ser ...

  8. Flask request 属性详解

    Flask request 属性详解 一.关于request在Flask的官方文档中是这样介绍request的:对于 Web 应用,与客户端发送给服务器的数据交互至关重要.在 Flask 中由全局的 ...

  9. Flask的响应及request属性整理

    类比django框架,Response三贱客: return  HttpResponse:  return ‘xxxxxxxxxx’                 # 返回字符串 return  r ...

随机推荐

  1. Devexpress WPF Theme Editor 01

    在Devexpress中,已经有内置了很多主题样式.一般我们开发就已经够用了.但是随着客户的需求提高..我们要自己手动写一些样式这些的, 那么Devexpress 已经提供一个专门这样的工具. 下载地 ...

  2. ADO.net数据绑定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  3. nodejs 遍历数组的两种方法

    var array = [1,2,3]; array.forEach(function(v,i,a){ console.log(v); console.log(i); console.log(a); ...

  4. SpringMVC Mybatis Shiro RestTemplate的实现客户端无状态验证及访问控制【转】

    A.首先需要搭建SpringMVC+Shiro环境 a1.pom.xml配置 spring: <dependency> <groupId>org.springframework ...

  5. 【转载】实用的Javascript获取网页屏幕可见区域高度

    本文转载原地址:这里 document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 docu ...

  6. sharepoint2013用场管理员进行文档库的爬网提示"没有权限,拒绝"的解决方法

    爬网提示被拒绝,场管理员明明可以打开那个站点的,我初步怀疑是:环回请求(LoopbackRequest)导致的 解决方法就是修改环回问题.修改注册表 具体操作方法: http://www.c-shar ...

  7. web开发技术-过滤器

    纪录自己的学习过程,帮助记忆 1.简介 过滤器是服务器端的一个组件,可以接收用户端的请求和响应信息,并且对这些信息进行过滤 过滤器不处理结果,只做一些辅助性操作 2.过滤器的工作原理 3.过滤器的生命 ...

  8. CSS3-05 样式 4

    前言 关于 CSS 的介绍,基本上告一段落了.在该博客中将介绍如何通过 CSS 去设置一个 HTML 元素,显示在 Web 页面的位置. 定位 概述 定义元素位置的基准,即:该元素与 HTML 文档流 ...

  9. js 调试

    $(":select[name='start_Month'").each(function(item,i){ console.log(item.name + "" ...

  10. out

    //练习1 class Program { static void Main(string[] args) { //写一个方法 求一个数组中的最大值.最小值.总和.平均值 int[] numbers ...