星空网 > 软件开发 > Java

getAttribute()与getParameter的区别

       当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getParametergetParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

authenticate.jsp

或者:


请输入用户姓名:

在authenticate.jsp中通过request.getParameter("username"); %>

(3)getAttribute()方法来和转发源组件共享request,session范围内的数据。

假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字, 如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

<%
String username=request.%>

在hello.jsp中通过getAttribute("username"); %>
Hello: <%=username %>

从更深的层次考虑,request.getParameter()方法返回String类型的数据。

request.setAttribute()和getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。

request.setAttribute()和getAttribute是返回对象,getAttribute()方法返回reques,sessiont范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

 




原标题:getAttribute()与getParameter的区别

关键词:get

get
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流