首页 技术 正文
技术 2022年11月14日
0 收藏 527 点赞 3,821 浏览 1147 个字

request.getParameter()与request.setAttribute()的区别 (转载) request.getParameter()与request.setAttribute()的区别

request.getParameter(),request.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”); %>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298