jsp request,正确使用. 为什么不能使用 getAttribute 得到保存的数据
2008-03-21 11:22
459 查看
转;
和同事碰到一个问题,发现挺经典的. 后来搜到这篇文章,才恍然大悟.
想想自己真够郁闷的. 笨蛋一个!
------------------------------------------------------------------
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
<a href="authenticate.jsp?username=weiqin">authenticate.jsp </a>
或者:
<form name="form1" method="post" action="authenticate.jsp">
请输入用户姓名:<input type="text" name="username">
<input type="submit" name="Submit" value="提交">
</form>
在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:
<% String username=request.getParameter("username"); %>
(3) 当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字, 如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:
<%
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类型的共享数据。
在网上找到的!
和同事碰到一个问题,发现挺经典的. 后来搜到这篇文章,才恍然大悟.
想想自己真够郁闷的. 笨蛋一个!
------------------------------------------------------------------
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
<a href="authenticate.jsp?username=weiqin">authenticate.jsp </a>
或者:
<form name="form1" method="post" action="authenticate.jsp">
请输入用户姓名:<input type="text" name="username">
<input type="submit" name="Submit" value="提交">
</form>
在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:
<% String username=request.getParameter("username"); %>
(3) 当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字, 如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:
<%
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类型的共享数据。
在网上找到的!
相关文章推荐
- Android app系统设置界面 数据的保存与读取 SharedPreferences 的正确使用
- JSP自定义标签中不能使用<%=request.getSession.getAttribute("attributeName")%>
- 为什么不能在VMware上使用VTune采集数据
- hadoop(为什么不能使用数据库来对大量磁盘上的大规模数据进行分析呢,为什么要用MapReduce)
- 使用IShellFolder::EnumObjects()不能正确枚举虚目录“我的电脑”下的对象,为什么?
- 在JSP中使用struts2的标签把request中的对象数据展示在JSP上
- 使用Entity Framwork 保存数据时,提示不能在对象中插入重复键,违反了PRIMARY_KEY约束
- 在使用了UpdatePanel后,在服务器端为什么无法得到DropDownList的SelectedIndex的正确值
- [置顶] Matlab中save实现保存数据到mat文件的正确使用
- jsp页面使用request.getAttribute来获取值
- 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?
- 数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……
- 使用js声明数组,对象在jsp页面中(获得ajax得到json数据)
- Android使用SharedPreferences保存HashSet<String>数据不能更新成功
- jsp使用request.getParamter提交数据 并显示
- jsp 使用request为页面添加静态数据的实例
- register_globals使用详解 为什么我的表单无法传递数据?为什么我的程序无法得到传递过来的变量?
- request和response对象(疑问:为什么servlet和jsp中可以直接使用req和res对象?如何理解内置的含义?)
- 有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。
- 两个JSP页面之间如何传递一个对象?比如一个LIST或者HASHMAP?以及为什么有时候REQUEST.GETATTRIBUTE("NAME")取不到值?