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

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

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

authenticate.jsp

或者:

请输入用户姓名:

在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);
%>

在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提交过来的数据。

最新文章

  1. ORACLE实现自定义序列号生成
  2. Python 字符串格式化
  3. win8.1上wamp环境中利用apache自带ab压力测试工具使用超简单讲解
  4. svn设置提交忽略某些文件或文件夹
  5. lib 和 dll 的区别、生成以及使用详解
  6. js验证 button 提交
  7. HW4.10
  8. 开源消息中间件DotNetMQ
  9. 【转】SQL Server 2008 新类型介绍之Date和Time
  10. autoconf添加gcc调试选项
  11. 算法训练 最大的算式 DP
  12. selenium 学习之路开始了,一遍搬一遍理解学习,加油!!!
  13. 企业架构设计之IFW实践回顾
  14. Go 函数方法
  15. string Type
  16. Spring容器技术内幕之内部工作机制
  17. Python基础语法复习
  18. 转移 Visual Studio 2017 的安装临时文件
  19. 同一个线程多次调用start()会出现的问题
  20. JQuery设置和去除disabled属性 与 display显示隐藏

热门文章

  1. 单节点nginx为两台apache服务器提供负载均衡
  2. Android学习笔记(七)
  3. MapReduce排序输出
  4. java连接mysql :No Suitable Driver Found For Jdbc 解决方法
  5. South - 在 Django 中 Migrate Database
  6. nginx+tomcat+memcached-session-manager组成简单的负载均衡和集群
  7. ios系统的中arm指令集
  8. 构造方法Constructor
  9. Windows窗口自动缩放机制
  10. &lt;a&gt;标签的href和onclick属性