小问题:

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. X-Plane飞行模拟资源整理一

    计划开一个博客整理一下飞行仿真软件二次开发的相关内容 预计将陆续介绍X-Plane.Microsoft Flight Simulator.FlightGear三个主流飞行模拟器. 此处为目录(占坑,随 ...

  2. GDB源代码查找路径

    在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,很多时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径.这个时候有两种选择: [1] ...

  3. django基本命令备忘录

    1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...

  4. 两个winform窗体同步

    /// <summary>        /// 初始left距离        /// </summary>        int initx = 0;        /// ...

  5. TreeList

    1.获取当前被选中的节点数据 string tmp = treeL.FocusedNode.GetDisplayText(this.treeListColumnIndex); 2.是否允许编辑 tre ...

  6. 利用青瓷布局自定义加载的场景,而不是自己改写qici-loading

    加载界面如果全部通过自己手动布局不仅不美观,还很难控制.借用原生的场景切换加载效果,来实现我们游戏的加载效果. 没有做加载修改的原来的加载顺序:   黑乎乎界面->(游戏定制的加载)你的第一个场 ...

  7. UEditor配置图片上传

    最近项目中需要用到一个图文编辑器功能,因为之前的kingeditor功能过于简陋,所以决定换成Ueditor,前端已经配置好了,这个是后台配置 1,确定前台已经配置好了 2,将编辑器的插件包下载下来, ...

  8. php 执行linux 命令函数

    php的内置函数exec,system都可以调用系统命令(shell命令),当然还有passthru,escapeshellcmd等函数. 在很多时候利用php的exec,system等函数调用系统命 ...

  9. Start an installation from GRUB

    Start an installation from GRUB Description This tip will show you how to start an installation for ...

  10. http知识累积

    1. http头 Host, Host请求报头域主要用于指定被请求资源的Internet主机和端口号,它通常从HTTP URL中提取出来的. 如果有黑客劫持了用户的请求,篡改了Host的内容,当请求到 ...