servlet 域 getAttribute 和getParameter 笔记

来源:互联网 发布:思科网络防火墙5506 编辑:程序博客网 时间:2024/06/08 07:21

<%
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类型的共享数据。


getAttribute是返回对象,getParameter返回字符串


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

0 0
原创粉丝点击