st.getParameter() 和request.getAttribute() 区别 https://terryjs.iteye.com/blog/1317610
getParameter 是用来接受用post个get方法传递过来的参数的.
getAttribute 必须先setAttribute.
(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() 方法。
拿一个例子来说一下吧,假如两个WEB页面间为链接关系时,就是说要从1.jsp链接到2.jsp时,被链接的是2.jsp可以通过getParameter()方法来获得请求参数.
假如1.jsp里有
- <form name="form1" method="post" action="2.jsp">
- 请输入用户姓名:<input type="text" name="username">
- <input type="submit" name="Submit" value="提交">
- </form>
的话在2.jsp中通过request.getParameter("username")方法来获得请求参数username:
- < % String username=request.getParameter("username"); %>
但是如果两个WEB间为转发关系时,转发目的WEB可以用getAttribute()方法来和转发源WEB共享request范围内的数据,也还是说一个例子吧。
有1.jsp和2.jsp
1.jsp希望向2.jsp传递当前的用户名字,如何传递这一数据呢?先在1.jsp中调用如下setAttribute()方法:
- <%
- String username=request.getParameter("username");
- request.setAttribute("username",username);
- %>
- <jsp:forward page="2.jsp" />
在2.jsp中通过getAttribute()方法获得用户名字:
- <% String username=(String)request.getAttribute("username"); %>
(1)HttpServletRequest 类有setAttribute()方法,而没有setParameter()方法
(2) 当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,
(3) 当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。
一般通过表单和链接传递的参数使用getParameter
通过request.setAttribute("name","jerry")的方式赋值的使用request.getAttribute("name")
这个问题主要是request和session的差别,request范围较小一些,只是一个请求,简单说就是你在页面上的一个操作, request.getParameter()就是从上一个页面中的url、form中获取参数,但如果一个request涉及多个类,后面还要取参数,可以用request.setAttribute()和request.getAttribute(),但是当结果输出之后,request就结束了。
而session可以跨越很多页面,可以理解是客户端同一个IE窗口发出的多个请求。这之间都可以传递参数,比如很多网站的用户登录都用到了。
一般可以用getParameter得到页面参数。。。字符串。。。
getAttribute()可以得到对象。。。
getParameter可以得到页面传来的参数如?id=123之类的。
getAttribute()常用于servlet页面传递参数给jsp
st.getParameter() 和request.getAttribute() 区别 https://terryjs.iteye.com/blog/1317610的更多相关文章
- request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- request.getParameter() 和request.getAttribute() 区别
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...
- request.getparameter和 request.getattribute的差别
request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设 ...
- 〖转〗request.getparameter()和request.getAttribute()的区别
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...
- Servlet & JSP - getParameter 与 request.getAttribute 的区别
getParameter 方法获取的是表单或 URL 的请求参数.参数是从 Web 客户端传递至 Web 服务端.例如有如下的 servlet: @WebServlet(name = "he ...
- request.getParameter() 和request.getAttribute()
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wrs120/article/details/79287607[相同点] 1.都是HttpServle ...
- request.getParameter与request.getAttribute()
这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...
- request.getParameter()和request.getAttribute()的区别
request.getParameter("val_1");这是获取请求的参数,比如你在url上看到的?id=12&name=abc就是参数,如果是post请求,就看不到. ...
- 数据库中无数据时查询数据为空,但出现空指针异常的解决方案(转载https://blueskator.iteye.com/blog/2096026)
异常:java.lang.NullPointerException 解决方案: try{ hotelImageList = ihotelImgManager.query(hiqc); }catch(E ...
随机推荐
- 牛客练习赛14 E - 无向图中的最短距离 (bfs+bitset)
一个链接:https://ac.nowcoder.com/acm/contest/82/E来源:牛客网 无向图中的最短距离 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144 ...
- 两张图理解nodeJS
- Git工程开发实践(四)——Git分支管理策略
A successful Git branching model https://nvie.com/posts/a-successful-git-branching-model/ Git工程开发实践( ...
- 手机能连接上ipsec
手机能连接上,但是FQ不行,路由也正常,iptables也设置,内核转发也弄了,暂时需要看看日志才行了,不知道什么情况,其他类型的没问题
- FCC 成都社区·前端周刊 第 8 期
01. 2018 前端开发者手册 这是一份 2018 前端开发手册,内容包括三个部分:前端工程实践.前端开发学习和前端开发工具. 详情:https://frontendmasters.com/book ...
- django开发环境搭建(参考流程)
django开发环境搭建(参考流程) 2013-08-08 01:09:06 分类: LINUX 原文地址:django开发环境搭建(参考流程) 作者:bailiangcn 对于一个初学者,在实际的开 ...
- SQL查询语句备忘录
有关于SQL查询的相关语句和语法的记录!备忘与复习用 1.SQL多表联合查询 select a.字段1,a.字段2,b,字段2 from 表1 a,表2 b where a.字段1 =b.字段1 2. ...
- python装饰器的简单理解
如果你接触 Python 有一段时间了的话,想必你对 @ 符号一定不陌生了,没错 @ 符号就是装饰器的语法糖. 装饰器的使用方法很固定: 先定义一个装饰函数(帽子)(也可以用类.偏函数实现) 再定义你 ...
- 字符编码、python2和python3编码的区别
目录 字符编码 文本编辑器存储信息的过程 python解释器解释python代码的流程 python解释器与文本编辑器的异同 不同编码格式存入与读取数据的过程 乱码的分析 python2和python ...
- [CF1004E] Sonya and Ice-cream
问题描述 Sonya likes ice cream very much. She eats it even during programming competitions. That is why ...