小问题:

JSP1代码

String [] test=new String[2];

test[0]="1";

test[1]="2";

request.setAttribute("test",test) ;

response.sendRedirect("jsp2.jsp");
JSP2代码

String test[]=(String[])request.getAttribute("test"); out.print(test);
为什么JS P2中会找不到test?

原因:用来在同一个request周期中保存变量使用。比如servlet调用后,推出JSP页面,这是一个request周期,如果在Jsp页面需要servlet中的一些 处理结构,就从request.getAttribute中获取。

sendRedirect()方法是通过浏览器重定向的,所以第二个JSP页面中获得的request并非是前一个页面的request(两次请求生成了前后两个不同的 request对象了)。

下面用RequestDispatcher接口的forward()试试。

List list = new CommonsDao().getAllCommons();

request.setAttribute("CommonsList", list);

request.getRequestDispatcher("View.jsp").forward(request, response);

View.jsp中就能找到CommonsList了

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()会在一个过程中始终保有这个值。

request.getRequestDispatcher()是请求转发,前后页面共享一个request 
response.sendRedirect()是重新定向 前后页面不是一个request

sendRedirect是通知IE再次提交一个http请求,  dispatcher是直接转到目的地址,最直接得表现  应该是dispatcher后的IE地址栏地址是不变的
request.getRequestDispatcher()是服务器端跳转  response.sendRedirect()是客户端跳转
 

今天在调试一个Servlet程序的时候,报了以下错误! Java.lang.IllegalStateException: Cannot forward after response has been committed 根据字面理解的话,意识是在response已经提交后程序不能再一次的跳转!
研究代码以后发现,是因为前面已经执行过一次request.request.getRequestDispatcher().forward() 但是后面的request.request.getRequestDispatcher().forward()依然被执行到了!
那么很有理由相信request.request.getRequestDispatcher().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页面的

response.sendRedirect 与 request.getRequestDispatcher.forward 的区别
1、forward重定向是在容器内部实现的同一个Web应用程序的重定向,所以forward方法只能重定向到同一个Web应用程序中的一个资源,重定向后浏览器地址栏URL不变,而sendRedirect方法可以重定向到任何URL, 因为这种方法是修改http头来实现的,URL没什么限制,重定向后浏览器地址栏URL改变。
2、forward重定向将原始的HTTP请求对象(request)从一个servlet实例传递到另一个实例,而采用sendRedirect方式两者不是同一个application。即参数的传递方式不一样:forward的form参数跟着传递,所以在第二个实例中可以取得HTTP请求的参数。sendRedirect只能通过链接传递参数,response.sendRedirect(“login.jsp?param1=a”)。
3、sendRedirect能够处理相对URL,自动把它们转换成绝对URL,如果地址是相对的,没有一个‘/’,那么Web container就认为它是相对于当前的请求URI的。比如,如果为response.sendRedirect("login.jsp"),则会从当前servlet 的URL路径下找login.jsp: http://10.1.18.8:8081/dms/servlet/Servlet 重定向的URL: http://10.1.18.8:8081/dms/servlet/login.jsp,如果为response.sendRedirect("/login.jsp")则会从当前应用径下查找url:http://10.1.18.8:8081/login.jsp。而forward不能这样处理相对路径。

request.setAttribute()用法的更多相关文章

  1. request.setAttribute()的用法

    分类专栏: Java   request在当次的请求的url之间有效一次传参数,速度快,缺点是参数只能取一次 forward是转向,将现在的请求转交,他只能在同一个容器里使用同时保存客户的请求状态. ...

  2. request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  3. request.setAttribute()怎么用的?

    request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]=&quo ...

  4. request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  5. request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  6. request.setAttribute()和session.setAttribute()的区别详解

    我们在Servlet和页面间传值时,经常会用到request.setAttribute()和session.setAttribute(),下面是两段示例用法 request.setAttribute( ...

  7. 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别

    protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...

  8. Request.url用法

    原文:Request.url用法 我們在開發網頁應用程式,時常需要去解析網址(Request.Url)的每個片段,進行一些判斷.例如說 "http://localhost:1897/News ...

  9. request.getAttribute( "result");和request.setAttribute("result",username);

    request.setAttribute("result",username);在request对象中加入名为result的属性并附值为username,因为request对象是可 ...

随机推荐

  1. jquery自适应布局

    代码整理 - uix.layout.js /** * Grace [jQuery.js] * * UIX页面布局 * 290353142@qq.com * exp: * $.uix.layout(); ...

  2. Windows phone 之 UserControl的应用

    一.新建一个UserControl.xaml页面  MyUserControl.xaml 添加代码如下: <UserControl x:Class="bodypicture.MyUse ...

  3. python模块学习 logging

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...

  4. OSI 7层模型

    协议:电脑与电脑通信之间的一种“约定”ois模式1,物理层2. 数据链路层3. 网络层4. 传输层5. 会话层6. 表示层7. 应用层发email为例 应用层:从用户输入完成所要发送的内容并点击“发送 ...

  5. frameset标签代码实现网站跳转

    js代码1: document.writeln("<frameset rows=\"0, *\">"); document.writeln(&quo ...

  6. phpmyadmin数据导入最大限制的解决方法

    mysql导入文件最大限制更改解决方法:phpmyadmin库导入出错:You probably tried to upload too large file. Please refer to doc ...

  7. quick-x 触摸事件的新方法

    --[[ local function onTouch(event, x, y) print(event, x, y) if event == "began" then retur ...

  8. sublime text下载和汉化

    好处就不说了,能认识到这款编辑器,基本上对它有一定的了解了. Sublime Text2是一款开源的软件,不需要注册即可使用(虽然没有注册会有弹窗,但是基本不影响使用). 官方网站:http://ww ...

  9. MAC 使用Jetbrains's产品

    Jetbrains's MAC 使用 ./gradle fatjar 或者 ./gradlew.sh fatjar java -jar build/lib/xx.jar 链接: http://pan. ...

  10. 机器视觉工具箱-Machine Vision Toolbox for Matlab

    发现了一个机器视觉的Matlab工具箱,分享一下. 机器视觉工具箱(MVT的)规定,在机器视觉和基于视觉的控制有益的多种功能.这是一个有点折衷收集反映作者在光度学,摄影测量,色度学 方面的个人利益.它 ...