Request的getParameter和getAttribute方法的区别
下面整理一下getParameter和getAttribute的区别和各自的使用范围。
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
<a href="authenticate.jsp?username=wolf">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提交过来的数据。
Request的getParameter和getAttribute方法的区别的更多相关文章
- Request的getParameter和getAttribute方法的差别
HttpServletRequest.getParameter("modelName");能取到想要的modelObject吗?经过測试之后.发现是不能的. 后来想想.其它道理挺简 ...
- request.getAttribute()和request.getParameter()两个方法的区别
request.getAttribute()获得的数据必须曾经有过setAttibute()过: 而request.getParameter()获得是客户端POST或者GET请求时所携带的参数的值 g ...
- request中getParameter和getAttribute的区别
整理一下getParameter和getAttribute的区别和各自的使用范围. (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方 ...
- getParameter和getAttribute有什么区别
1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...
- 【转】HttpServletRequest.getParameter() &HttpServletRequest.getAttribute() 区别
Ref: HttpServletRequest的getParameter和getAttribute方法有什么区别 具体如下几点: (1)HttpServletRequest类有setAttribute ...
- request:getParameter和getAttribute区别
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...
- request:getParameter getAttribute
转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...
- JSP中getParameter和getAttribute区别
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...
- getParameter和getAttribute区别
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...
随机推荐
- Android手机上监听短信的两种方式
Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...
- 可爱的PYTHON,更新到0.5版本代码
这书看得挺顺的. 按着思路学习软件开发的主流思想.. cdctools.py # _*_ coding: utf-8 _*_ import os,sys def cdWalker(CDROM, cdc ...
- Spring 定时任务 quartz的配置
环境:我用的是spring3.2,其中引入了quartz-1.5.2.jar 先写一个任务类: package com.hlcg.common.task; public class TestJob { ...
- 最简单的CRC32源码---逐BIT法
CRC其实也就那么回事,却在网上被传得神乎其神.单纯从使用角度来说,只需要搞明白模二除法,再理解一些偷懒优化的技巧,就能写出自己的CRC校验程序. 下面的代码完全是模拟手算过程的,效率是最低的,发出来 ...
- linux解压cpio.gz类型文件
1. gunzip XXX.cpio.gz –> 得到 XXX.cpio 文件 2. cpio -idmv <XXX.cpio –> 得到 XXX 文件夹
- lsof 拥有更多的功能
lsof 拥有更多的功能# lsof -i 看系统中有哪些开放的端口,哪些进程.用户在使用它们,比 netstat -lptu 的输出详细. # lsof -i 4 查看IPv4类型的进程COMMA ...
- WC2015 滚粗记
Day 0 和南师附中诸人去杭州,想到这是第三次去杭州有点感动 想到noi还要在杭州,简直…… 火车站接送好评如潮 ym大学军,到学军领资料然后到浙大宿舍安顿,noi的书包还是挺不错的 看起来宿舍还可 ...
- WordPress Quick Paypal Payments插件HTML注入漏洞
漏洞名称: WordPress Quick Paypal Payments插件HTML注入漏洞 CNNVD编号: CNNVD-201310-491 发布时间: 2013-10-22 更新时间: 201 ...
- ruby脚本打印日志到rspec的报告文件中
在通过ruby+webdriver+rspec做自动化测试的时候,为了便于观察用例执行情况,我基本上都会用 rspec XX.rb --format doc -o result.log 如果遇到失败的 ...
- 在CentOS上为DiscuzX3安装ImageMagick支持。
找了几篇外文的,写的很好,按照参考安装成功! http://www.tecmint.com/install-imagemagick-in-linux/ ImageMagick is an free o ...