Request setAttribute()用法疑点
来源:互联网 发布:数据库与大数据 编辑:程序博客网 时间:2024/05/16 05:05
一下是来自社区问答的一些回答,我整合了一下形成文章
在两个JSP代码片中有这样两端程序:
- String [] test=new String[2];
- test[0]=“1”;
- test[1]=“2”;
- request.setAttribute(”test”,test) ;
- response.sendRedirect(”jsp2.jsp”);
String [] test=new String[2];test[0]="1";test[1]="2";request.setAttribute("test",test) ;response.sendRedirect("jsp2.jsp");
JSP2代码
- </pre><pre name=“code” class=“java”>String test[]=(String[])request.getAttribute(“test”);
- out.print(test);
</pre><pre name="code" class="java">String test[]=(String[])request.getAttribute("test");out.print(test);
但是,问题来了:为什么JSP2 中test获取不到呢?
其实那就要从request的生命周期或者是说作用范围说起了,setAttribute()用来在同一个request周期中保存变量使用。
比如servlet调用后,引出JSP页面,这是一个request周期。如果在Jsp页面需要servlet中的一些 处理结构,就从request.getAttribute中获取。
而sendRedirect()方法是通过浏览器重定向的,所以第二个JSP页面中获得的request并非是前一个页面的request(两次请求生成了前后两个不同的 request对象了)。而此时使用RequestDispatcher接口的forward()方法则能够得到request中的对象了,这是因为后者并不是使用客户端浏览器进行重定向的,从函数的名字就可以看出,RequestDispatcher.forward()就是从服务器端进行任务转发。
sendRedirect()是请求从定向,和超连接是一个意思,比如你在A页面中写一个request.setAtribute,sendRedirect到B页面,就是说服务器从A页面中给你一个response,然后你的浏览器再去request到B页面,由于有两次request和response,是不能在B页面取到request.setAtribute里的值,能从地址栏看到url的改变。
request.getRequestDispatcher().forward(request,response)是请求分发器,比如你在A页面中写一个request.setAtribute,request.getRequestDispatcher().forward(request,response)到B页面,那就是说服务器给你的response是B页面的,并且只有一次request和response,所以是能在B页面取到request.setAtribute里的值,地址栏的url仍然是A页面的。
- List list = new CommonsDao().getAllCommons();
- request.setAttribute(”CommonsList”, list);
- request.getRequestDispatcher(”View.jsp”).forward(request, response);
List list = new CommonsDao().getAllCommons();request.setAttribute("CommonsList", list);request.getRequestDispatcher("View.jsp").forward(request, response);
顺便谈一谈 request.getAttribute()与request.setAttribute()
request.getAttribute(“nameOfObj”),可得到JSP页面一表单中控件的Value。
其实表单控件中的Object的 name与value是存放在一个哈希表中的,所以在这里给出Object的name会到哈希表中找出对应它的value。
而不同页面间传值使用request.setAttribute(position, nameOfObj)时,只会从a.jsp到b.jsp一次传递,之后这个request 就会失去它的作用范围,再传就要再设一个 request.setAttribute()。而使用session.setAttribute()会在一个过程中始终保有这个值。
P.S.:JavaScript与JSP中不能相互传值,因为JavaScript运行在客户端,而JSP运行在服务器端。若想使它们之间可以相互传递参数,可以在JSP中设置一个hidden控件,用它的value结合上面所说的用法来传递所需的数值。
- Request setAttribute()用法疑点
- request.setAttribute的用法
- request.setAttribute()的用法
- request.setAttribute()用法
- request.setAttribute的用法
- request.setAttribute的用法 - [JAVA]
- HTTP协议及其request.setAttribute()用法
- request.setAttribute
- request.setAttribute
- request.setAttribute和session.setAttribute
- request.setAttribute和session.setAttribute
- 关于request.setAttribute和response.getWriter.print()用法环境
- 浅谈Java web 中request的setAttribute()用法
- 浅谈Java web 中request的setAttribute()用法
- 浅谈Java web 中request的setAttribute()用法
- request.getSession().setAttribute()和 request.setAttribute()
- request.setAttribute()和request.getSession().setAttribute()解释
- request.setAttribute()与request.getSession().setAttribute()
- Android中dp和px的转化问题,以及如何获得手机分辨率和像素密度
- AG-Admin 一个spring cloud的好例子
- Android 8.0 行为变更
- prototype原型链
- 判断一个字符串内字符重复的字符个数
- Request setAttribute()用法疑点
- kotlin版的酷欧天气 (郭林大神的第一行代码第二版)
- 如何解决Hadoop Connection Refused和FileNotFoundException问题
- 76. Minimum Window Substring
- effective C++条款四十四解读
- git 常用简单操作
- 页面缩放兼容性处理(zoom,Firefox火狐浏览器)
- 缓存Memcached 与 Redis 相同点差异点分析
- 剑指offer:构建乘积数组