转载:setAttribute和setParameter方法的区别

getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型  getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型

HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:

(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

<a  href="authenticate.jsp?username=weiqin">authenticate.jsp  </a>

或者:

<form  name="form1"  method="post"  action="authenticate.jsp">
   请输入用户姓名:<input  type="text"  name="username">
   <input  type="submit"  name="Submit"  value="提交">
</form>

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

<%  String  username=request.getParameter("username");  %>

(3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定  authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,  如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

<%
String  username=request.getParameter("username");
request.setAttribute("username",username);
%>

<jsp:forward  page="hello.jsp"  />

在hello.jsp中通过getAttribute()方法获得用户名字:

<%  String  username=(String)request.getAttribute("username");  %>
Hello:  <%=username  %>

从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。

request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,  request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段,这个的确是正解.

getAttribute是返回对象,getParameter返回字符串

request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

时间: 2024-10-13 23:16:06

转载:setAttribute和setParameter方法的区别的相关文章

setAttribute和setParameter方法的区别

getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数.URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型 HttpServletRequest类既有getAttribu

定义c/c++全局变量/常量几种方法的区别(转载)

出自:http://www.cnblogs.com/yaozhongxiao/archive/2010/08/08/1795338.html 在讨论全局变量之前我们先要明白几个基本的概念:  1. 编译单元(模块): 在ide开发工具大行其道的今天,对于编译的一些概念很多人已经不再清楚了,很多程序员最怕的就是处理连接错误 (link error)  因为它不像编译错误那样可以给出你程序错误的具体位置,你常常对这种错误感到懊恼,但是如果你经常使用 gcc,makefile等工具在linux或者嵌入

Request的getParameter和getAttribute方法的区别

下面整理一下getParameter和getAttribute的区别和各自的使用范围.      (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法      (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:      <a  href="authent

Java中创建对象的5种方式 &amp;&amp;new关键字和newInstance()方法的区别

转载:http://www.kuqin.com/shuoit/20160719/352659.html 用最简单的描述来区分new关键字和newInstance()方法的区别:newInstance: 弱类型.低效率.只能调用无参构造.new: 强类型.相对高效.能调用任何public构造. newInstance( )是一个方法,而new是一个关键字,其次,Class下的newInstance()的使用有局限,因为它生成对象只能调用无参的构造函数,而使用new关键字生成对象没有这个限制.Cla

jQuery中attr和prop方法的区别

相比attr,prop是1.6.1才新出来的,两者从中文意思理解,都是获取/设置属性的方法(attributes和properties).只是,window或document中使用.attr()方法在jQuery1.6之前不能正常运行,因为window和document中不能有attributes.prop应运而生了. 之前看网上对比两者的文章,更是列出一个表来区分什么标签下使用prop,什么标签下使用attr,原谅我是懒惰的人,最害怕要背的东西,所以只有自己想想办法了. 既然我们想知道他们两的

[JS]jQuery中attr和prop方法的区别

相比attr,prop是1.6.1才新出来的,两者从中文意思理解,都是获取/设置属性的方法(attributes和properties).只是,window或document中使用.attr()方法在jQuery1.6之前不能正常运行,因为window和document中不能有attributes.prop应运而生了. 之前看网上对比两者的文章,更是列出一个表来区分什么标签下使用prop,什么标签下使用attr,原谅我是懒惰的人,最害怕要背的东西,所以只有自己想想办法了. 既然我们想知道他们两的

请问一下这三种存储方法的区别?原理是什么样子的?哪一种比较好,能不能提供一些意见。谢谢

public String nickName; public Integer userId; public String login(){ //登录之后 方法1. userId = user.getId();nickName = user.getNickName(); 方法2. this.getResRequest().setAttribute("userId", userId); this.getResRequest().setAttribute("nickName&quo

jQuery 中 attr() 和 prop() 方法的区别&lt;转&gt;

前几天,有人给 Multiple Select 插件 提了问题: setSelects doesn't work in Firefox when using jquery 1.9.0 一直都在用 jQuery 1.8.3 的版本,没有尝试过 jQuery 1.9.0 的版本. 于是,开始调试代码,在 1.9.0 的版本中: <input type="checkbox" /> <script> $(function() { $('input').click(fun

Jquery empty() remove() detach() 方法的区别

方法简介: empty() This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child no