getparameter和getattribution的区别的 java详细
2016-03-04 13:53
351 查看
(1)HttpServletRequest 类有 setAttribute()方法,而没有 setParameter()方法
(2)当两个 Web 组件之间为链接关系时,被链接的组件通过 getParameter()方法来 获得请求参数,例如假定 welcome.jsp 和 authenticate.jsp 之间为链接关系,
welcome.jsp 中有以下代码: <a href="authenticate.jsp?username=wolf">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 类型的共享数据。
request.getParameter()取得是通过容器的实现来取得通过类似 post,get 等方式 传入的数据。 request.setAttribute()和 getAttribute()只是在 web 容器内部流转, 仅仅是请求 处理阶段。
getAttribute 是返回对象,getParameter 返回字符串 总的来说:request.getAttribute()方法返回 request 范围内存在的对象,
而 request.getParameter()方法是获取 http 提交过来的数据。 request.getParameter()获取的是 HTTP 提交过来的数据。
request.getAttribute()方法返 回的是 request 范围内存在的对象
转自:http://zhidao.baidu.com/link?url=igALnaT38DnLGR2txH5cDKLOKRdeH-PJNbD7lTuqRWsstf73AKAD51rHC0PpcPJwN45n94BBFL0Dc6i2WwEe4vTbTWY8WDt38fUSWAUTkjG
(2)当两个 Web 组件之间为链接关系时,被链接的组件通过 getParameter()方法来 获得请求参数,例如假定 welcome.jsp 和 authenticate.jsp 之间为链接关系,
welcome.jsp 中有以下代码: <a href="authenticate.jsp?username=wolf">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 类型的共享数据。
request.getParameter()取得是通过容器的实现来取得通过类似 post,get 等方式 传入的数据。 request.setAttribute()和 getAttribute()只是在 web 容器内部流转, 仅仅是请求 处理阶段。
getAttribute 是返回对象,getParameter 返回字符串 总的来说:request.getAttribute()方法返回 request 范围内存在的对象,
而 request.getParameter()方法是获取 http 提交过来的数据。 request.getParameter()获取的是 HTTP 提交过来的数据。
request.getAttribute()方法返 回的是 request 范围内存在的对象
转自:http://zhidao.baidu.com/link?url=igALnaT38DnLGR2txH5cDKLOKRdeH-PJNbD7lTuqRWsstf73AKAD51rHC0PpcPJwN45n94BBFL0Dc6i2WwEe4vTbTWY8WDt38fUSWAUTkjG
相关文章推荐
- 《Java程序设计基础》 第2章手记
- 探究java方法参数传递——引用传递?值传递!
- Java内部类
- 学习Java书籍
- Java自动装箱与拆箱
- Eclipse 快捷键整理
- Java自动装箱与拆箱
- ant、maven解决rt.jar访问限制问题
- JAVA IO流(下)
- 【SpringMVC】SpringMVC系列15之SpringMVC最佳实践
- Java [Leetcode 260]Single Number III
- 【SpringMVC】SpringMVC系列14之SpringMVC国际化
- 【SpringMVC】SpringMVC系列13之关于 mvc:annotation-driven
- java堆栈类解决括号匹配问题
- java mysql 中文查询
- java中==与equals()方法的区别与使用
- 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
- Java 集合类
- 【SpringMVC】SpringMVC系列11之Restful的CRUD
- Java集合