Servlet HttpRequest 中【getAttribute】和【getParameter】的区别
1.获取的值不同
- getAttribute表示从request范围取得设置的属性,必须要通过setAttribute设置属性,才能通过getAttribute取得。设置和取得的值都是Object类型。
- getParameter表示接收页面提交的 参数,主要有表单提交的参数、URL重写传递的参数(http://item.jsp?id=1中的id值)等,所以不需要通过setParameter来设置参数,而且参数的返回类型是String类型。
2.数据传递路径不同
- request.getParameter()方法传递的数据是从web客户端到web服务端,代表http的请求数据,用于表单或URL重定向时使用。
举例:
比如两个web页面之间的转换,准确的说是两个web页面时链接关系时,从login.jsp链接到menu.jsp时,menu.jsp就可以通过getParameter()方法来获得请求参数。
login.jsp:
<%
//获取参数
String username=request.getParameter("username");
//设置属性
request.setAttribute("username",username);
%> <!-- 页面跳转 --!>
<jsp:forward page="menu.jsp" />
1 <form name="form1" method="post" action="menu.jsp">
2 请输入用户姓名:<input type="text" name="username">
3 <input type="submit" name="Submit" value="提交">
4 </form>
menu.jsp中则可通过request.getParameter("username")方法来获得请求的参数username:
<% String username=request.getParameter("username"); %>
- request.getAttribute()传递的数据只存在于web容器内部,在request范围内也就是转发时web组件之间的共享。
举例:
login.jsp想要向menu.jsp传递当前的用户名,首先要在login.jsp中调用setAttribute()方法:
1 <%
2 //获取参数
3 String username=request.getParameter("username");
4 //设置属性
5 request.setAttribute("username",username);
6 %>
7
8 <!-- 页面跳转 --!>
9 <jsp:forward page="menu.jsp" />
在menu.jsp中通过getAttribute()方法获得用户名:
<% String username=(String)request.getAttribute("username"); %>
Servlet HttpRequest 中【getAttribute】和【getParameter】的区别的更多相关文章
- java中getAttribute和getParameter的区别
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...
- 2017.10.16 java中getAttribute和getParameter的区别
(1)getAttribute:表示得到 域中的对象 返回的是OBJ类型; getParameter:表示 得到 传递的参数 返回的是String类型; 也就是getAttribute获得的值需要进 ...
- SERVLET API中转发与重定向的区别?
SERVLET API中转发与重定向的区别? 1.转发(forward方法) 转发仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址. 转发是服务器请求资源,服务器直接访问目标地址的 ...
- getAttribute()与getParameter的区别
当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getParamet ...
- java中getAttribute与getParameter方法的区别
知识点1:getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为object对象类型 例: r ...
- getAttribute与getParameter的区别
1.getParameter得到的是字符串,其取值源于jsp页面,从jsp页面中接受一个存在的参数,多用于servlet中,用于判断业务的类型和跳转页面.如: request.getParameter ...
- getattribute()与getparameter()的区别
1.它们取到的值不同.getAttribute取到的是对象(object),而getParameter取到的是String. 2.数据传递路劲不同.request.getParameter方法传递的数 ...
- getAttribute和getParameter的区别
2016年1月19日JSP中getParameter与getAttribute有何区别? ——getParameter得到的都是String类型的.或者是http://a.jsp?id=123中的12 ...
- SERVLET API 中 forward() 与 redirect()的区别?
答:前者仅是容器中控制权的转向, 在客户端浏览器地址栏中不会显示出转向后的地址: 后者则是完全的跳转, 浏览器将会得到跳转的地址, 并重新发送请求链接. 这样, 从浏览器的地址栏中可以看到跳转后的链接 ...
随机推荐
- angular2-组件样式
组件样式: :host 选择器 使用:host伪类选择器,用来选择组件宿主元素中的元素(相对于组件模板内部的元素) 这是我们能以宿主元素为目标的唯一方式.除此之外,我们将没办法指定它, 因为宿主不是组 ...
- Swift中as as! as?的区别
as :类型一致或者子类 仅当一个值的类型在运行时(runtime)和as模式右边的指定类型一致 - 或者是该类型的子类 - 的情况下,才会匹配这个值.如果匹配成功,被匹配的值的类型被转换成as模 ...
- AndroidStudio多AppId多渠道快速打包
一直感觉AndroidStudio没有eclipse快,但是最近由于遇到一个问题不得不将工程迁移到AndroidStudio上,迁移后之前在eclipse上所做的所有批量打包又得重新在AndroidS ...
- dojo事件绑定
Dojo如何动态绑定事件的小demo <html> <title>事件绑定测试test</title> <head></head> < ...
- C语言问题集
征服C指针:P70#include "stdio.h" char *int_to_str(int int_value){ static char buf[20]; sprintf( ...
- linux系统unzip文件报错的解决方案
data.zip文件有4G多,解压的时候出问题了. Archive: data.zip End-of-central-directorysignature not found. Either th ...
- Moodle-3.1.2 (Ubuntu 16.04 )
平台: Ubuntu 类型: 虚拟机镜像 软件包: moodle-3.1.2 commercial education moodle open-source 服务优惠价: 按服务商许可协议 云服务器费 ...
- 微信小程序之怎样识别一个小程序用户
本节主要是说下怎样识别一个小程序的用户,需要用什么数据来做标识呢: 我们应该都知道判断是不是一个用户大部分都是通过userid来判断,如果这个用户访问的应用发送了一个请求,把userid之类的数据发给 ...
- 2017.10.28 QB模拟赛 —— 上午
题目链接 T1 1e18 内的立方数有 1e6个 直接枚举可过 二分最优 考场用set 死慢.. #include <cstdio> int t; long long p; int ma ...
- CRM, C4C和Hybris的工作流简介
CRM的例子 Step by Step to debug IC inbox workflow WS14000164 C4C Custom recipient determination in work ...