面向对象设计方法的最大好处是可重用性。特别是,J2EE 系统将它们借用到模块化风格的开发中,其中组件可以在其他应用程序中重新安排、重新打包和重新使用。即使您对设计可重用的 Web 模块不感兴趣,也很可能会发现您的 J2EE 应用程序由几个部分组成。任何时候使用多个 servlet 或者 JSP 页面(也就是组件)完成一个请求的时候,都需要使用某种类型的流控制技术。Servlet 架构提供两种这样的技术:forward(转发) 和 include(包括)。 在 J2EE Web 开发中,forward 会把处理用户请求的控制权转交给到其他 Web 组件。forward 在有些时候会比较有用,比如说需要用一个组件设置一些 JavaBean、打开或关闭资源、认证用户,或者在将控制权传递给下一个组件之前需要执行一些准备工作。在转发之前可以执行很多类型的任务,但是要转发的组件不能设置响应头部信息,也不能有内容发送到输出缓冲区。所有与向客户发送内容直接相关的任务必须由被转发的组件完成。 J2EE 中第二种流控制技术是include。在使用 forward 时,要传递控制权。与此不同的是,执行 include 的组件维持对请求的控制权,而只是简单地请求将另一个组件的输出包括在该页面的某个特定的地方。对于常见的设计元素,例如页首、页脚和导航栏等,这是一个非常好的方法。 forward 和 include 都是通过一个专门的对象 java.servlet.RequestDispatcher 来完成的。简单地调用一个 ServletContext 对象的 getRequestDispatcher()方法就可以获得一个RequestDispatcher 对象。得到对 ServletContext 对象的引用有几种方法,我们可以:使用隐式声明的 application 变量,因为它的类型本身已经是 ServletContext。
调用方法 getServletContext(),该方法返回一个对隐式声明的 application 变量的引用。
调用隐式声明的 config 变量的 getServletContext()方法。
调用隐式声明的 pageContext 变量的 getServletContext()方法。
调用隐式声明的 request 变量的 getServletContext()方法。
调用隐式声明的 session 变量的 getServletContext()方法。
清单1给出了使用隐式变量 application 的 forward 流控制机制的代码示例。清单1. forward 流控制示例javax.servlet.RequestDispatcher rd;/* Obtain a reference to a RequestDispatcher object via the implicit
application variable*/
rd = application.getRequestDispatcher( "/NextPage.jsp" );/* Perform the forward specified by the RequestDispatcher
and pass along a reference to the current request and
response objects */
rd.forward( request, response ); 清单2给出了同样使用变量 application 的 include 流控制的代码示例。清单2. include 流控制示例javax.servlet.RequestDispatcher rd;/* Obtain a reference to a RequestDispatcher object via the implicit
application variable*/
rd = application.getRequestDispatcher( "/Header.jsp" );/* Perform the include specified by the RequestDispatcher
and pass along a reference to the current request and
response objects */
rd.include( request, response ); forward 和 include 是添加到 J2EE Web 开发工具包中的两个非常棒的技术。还有其他一些方法可以在 JSP 页面中完成 include,而且还有很多解决 J2EE 设计模式方面的文献中讲到了如何结合使用这两种技术
调用方法 getServletContext(),该方法返回一个对隐式声明的 application 变量的引用。
调用隐式声明的 config 变量的 getServletContext()方法。
调用隐式声明的 pageContext 变量的 getServletContext()方法。
调用隐式声明的 request 变量的 getServletContext()方法。
调用隐式声明的 session 变量的 getServletContext()方法。
清单1给出了使用隐式变量 application 的 forward 流控制机制的代码示例。清单1. forward 流控制示例javax.servlet.RequestDispatcher rd;/* Obtain a reference to a RequestDispatcher object via the implicit
application variable*/
rd = application.getRequestDispatcher( "/NextPage.jsp" );/* Perform the forward specified by the RequestDispatcher
and pass along a reference to the current request and
response objects */
rd.forward( request, response ); 清单2给出了同样使用变量 application 的 include 流控制的代码示例。清单2. include 流控制示例javax.servlet.RequestDispatcher rd;/* Obtain a reference to a RequestDispatcher object via the implicit
application variable*/
rd = application.getRequestDispatcher( "/Header.jsp" );/* Perform the include specified by the RequestDispatcher
and pass along a reference to the current request and
response objects */
rd.include( request, response ); forward 和 include 是添加到 J2EE Web 开发工具包中的两个非常棒的技术。还有其他一些方法可以在 JSP 页面中完成 include,而且还有很多解决 J2EE 设计模式方面的文献中讲到了如何结合使用这两种技术
得到 servlet 上下文RequestDispatcher rd = sc.getRequestDispatcher(url);
得到 url 的请求调度权rd.forward(request, response);
通过 response 回应 request大致如此
就是说以前的操作权限和数据仍然有效。
getServletContext().getRequestDispatcher(url).forward(request, response);