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.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。 
一般是string型用request.getParameter 
对象的引用用request.getAttribute 
当然啦,String也是对象 。

request.getAttribute() 和 request.getParameter() 有何区别?的更多相关文章

  1. request.getAttribute()与request.setAttribute()

    request.getAttribute()与request.setAttribute() request.getAttribute("nameOfObj")可得到JSP页面一表单 ...

  2. 基于request.getAttribute与request.getParameter的区别详解

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

  3. request.getAttribute()和request.getParameter()两个方法的区别

    request.getAttribute()获得的数据必须曾经有过setAttibute()过: 而request.getParameter()获得是客户端POST或者GET请求时所携带的参数的值 g ...

  4. request.getAttribute()和request.getParameter()

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

  5. request.getparameter和 request.getattribute的差别

    request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设 ...

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

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

  7. request.getParameter与request.getAttribute()

    这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...

  8. Servlet & JSP - getParameter 与 request.getAttribute 的区别

    getParameter 方法获取的是表单或 URL 的请求参数.参数是从 Web 客户端传递至 Web 服务端.例如有如下的 servlet: @WebServlet(name = "he ...

  9. 〖转〗request.getparameter()和request.getAttribute()的区别

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

随机推荐

  1. 数据结构Java实现04----循环链表、仿真链表

    单向循环链表 双向循环链表 仿真链表 一.单向循环链表: 1.概念: 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形 ...

  2. java 16-1 ArrayList的练习1

    需求: ArrayList去除集合中字符串的重复值(去掉相同的字符串) 分析: 第一种做法:创建一个新的空集合: A:创建1个具有相同字符串的集合 B:创建1个空的集合 C:遍历第一个集合里面的元素 ...

  3. python 维吉尼亚

    加密key='COMPUTER' plaintext='BLOCKCIPHERDESIGNPRINCIPLE' ascii='abcdefghijklmnopqrstuvwxyz'.upper() k ...

  4. 10SpringMvc_springmvc快速入门小案例(注解版本)

    第一步:新建案例工程:

  5. Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  6. CAS 单点登录流程

    经验:在网上学东西不要指望一篇文章就能让你明白——我在网上学CAS流程,看了五六篇博文,其中三篇是觉得作者表达能力不行,或者作者自己就没明白怎么回事就出来写东西,看到一半就跳过了,剩下两篇每篇看了两遍 ...

  7. normal.1

    11 # coding:utf-8 def maxnum(ipstr): ipstr = ipstr.split(' ')[1] return ipstr def minnum(ipstr): ips ...

  8. JNI 程序开发

    参考资料: http://blog.csdn.net/wwj_748/article/details/28136061 JNI_最简单的Java调用C/C++代码 http://blog.csdn.n ...

  9. C++ 栈的基本操作

    // zhan.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using namesp ...

  10. [tomcat7源码学习]初始化之catalina.home和catalina.base(转)

    我们在代码中为了获取某个配置文件路径下的文件经常会这么写 String tomcatPath = System.getProperty("catalina.home") + &qu ...