关于RequestDispatcher的原理
RequestDispatcher简介
- public interface RequestDispatcher
- {
- public void forward(ServletRequest request, ServletResponse response)
- throws ServletException, IOException;
- public void include(ServletRequest request, ServletResponse response)
- throws ServletException, IOException;
- }
RequestDispatcher.forward(request, response)
- public class User{
- private String name;
- private int age;
- public String getName(){
- return name ;
- }
- public void setName( String name ){
- this .name = name ;
- }
- public int getAge() {
- return age ;
- }
- public void setAge( int age ){
- this .age = age ;
- }
- }
UsersServlet.java
- public class UsersServlet extends HttpServlet {
- private static final long serialVersionUID = 1L ;
- protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
- /*****************一般实际开发这些用户数据都是从数据库查出来的*********/
- List <User > users = new ArrayList <> ();
- User u1 = new User () ;
- u1 .setAge ( 20) ;
- u1 .setName ( "Bob") ;
- User u2 = new User () ;
- u2 .setAge ( 21) ;
- u2 .setName ( "Tony") ;
- users .add ( u1) ;
- users .add ( u2) ;
- /*********************************************/
- request .setAttribute ( "users", users) ; //对request 进制预处理准备工作
- request .getRequestDispatcher ( "users.jsp").forward( request , response );//转发到users.jsp,让他去具体响应
- }
}
- <%@ page contentType= "text/html; charset=UTF-8" pageEncoding ="UTF-8" trimDirectiveWhitespaces= "true"
- session ="true" %>
- <%@ taglib prefix= "c" uri = "http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html>
- < html>
- <head>
- <meta http-equiv = "Content-Type" content ="text/html; charset=UTF-8">
- <title> 用户列表</title>
- </head>
- <body>
- <p> -----------------转发到的资源users.jsp产生的响应数据------------------ </p>
- < c:forEach var ="user" items= " ${users}" >
- 用户姓名:${user.name} 用户年龄:${user.age} <br />
- </ c:forEach>
- </body>
- </html>
例子2:不使用Attribute,使用Paramter向转发的资源传递参数。
虽然request对象没有setParameter方法来设置参数,但是我们可以在转发的URL后通过QueryString 的方式添加。JSP中的<jsp:foward>标签下的<jsp:param>标签就是使用的这个原理。
AimServlet.java
- public class AimServlet extends HttpServlet {
- private static final long serialVersionUID = 1L ;
- protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException {
- request .getRequestDispatcher ( "foo.jsp?num=1") . forward( request , response );
- }
- }
foo.jsp
- <%@ page contentType= "text/html; charset=UTF-8" pageEncoding ="UTF-8" trimDirectiveWhitespaces= "true"
- session ="true" %>
- <%@ taglib prefix= "c" uri = "http://java.sun.com/jsp/jstl/core" %>
- <! DOCTYPE html>
- <html>
- <head>
- <meta http-equiv = "Content-Type" content ="text/html; charset=UTF-8">
- <title> 标题</title>
- </head>
- <body>
- 通过forward传递过来的参num=${param.num}
- </body>
- </html>
RequestDispatcher.include(request, response)
注意事项:
1、被包含者不能设置ServletResponse的响应状态和响应头(否则并不会产生效果),因为这些都是包含者做的事,被包含者只需要产生响应数据解可以了。
2、不同于 forward中的request的传递特性:在被包含的资源中从request中获取请求路径相关的信息,发现依然是原始请求的路径,也就是浏览器地址栏相关的路径,也就是说被包含的资源获得的request对象的路径属性和原始请求资源的路径一样(见下面的例子1)。其它的API调用也是一样的(Attribute 和Parameter)。
例子1
TargetServlet.java
- public class TargetServlet extends HttpServlet {
- private static final long serialVersionUID = 1L ;
- protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException {
- response .setContentType ( "text/html;charset=utf-8" );
- PrintWriter out = response .getWriter () ;
- out .println ( "----------来自TargetServlet的告白----------------<br />" ) ;
- out .print ( "我偷懒了,下面的响应数据并不是我自己产生的,而是包含的其它资源产生的<br/>" ) ;
- request .getRequestDispatcher ( "test.jsp") . include( request , response );
- out .flush () ;
- out .close () ;
- }
- }
test.jsp
- <%@ page contentType= "text/html; charset=UTF-8" pageEncoding = "UTF-8" trimDirectiveWhitespaces = "true"
- session = "false"
- %>
- <p> ------------------------来自test.jsp的告白-------------------------- </p>
- <p> 我输出的响应数据将被其它的资源包含 </p>
- 请的URL是 <%= request.getRequestURL().toString() %> ,可以看出客户端真正请求的不是我,我只是幕后工作者。
- <p> 但我很开心,因为响应给客户端的数据一部分来自于我 </p>
例子2:通过包含路径后追加QueryString来向被包含资源传递参数,以及通过request.setAttribute传递属性。
同样, JSP中的<jsp:include>标签下的<jsp:param>标签就是通过在含路径后追加QueryString达到的传递参数的效果。
- public class TargetServlet extends HttpServlet {
- private static final long serialVersionUID = 1L ;
- protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException {
- response .setContentType ( "text/html;charset=utf-8" );
- PrintWriter out = response .getWriter () ;
- out .println ( "----------来自TargetServlet的告白----------------<br />" ) ;
- out .print ( "我偷懒了,下面的响应数据并不是我自己产生的,而是包含的其它资源产生的<br/>" ) ;
- request .setAttribute ( "sharedatt", "I`m shared attribute") ;
- request .getRequestDispatcher ( "test.jsp?sharedparam=Im-shared-parameter" ). include (request , response ) ;
- out .flush () ;
- out .close () ;
- }
- }
- <%@ page contentType= "text/html; charset=UTF-8" pageEncoding = "UTF-8" trimDirectiveWhitespaces = "true"
- session = "false"
- %>
- <p> ------------------------来自test.jsp的告白-------------------------- </p>
- <p> 我输出的响应数据将被其它的资源包含 </p>
- <p> 从request中提取共享的属性Attribute : <%= request.getAttribute("s haredatt") %>
- <p> 从request中提取共享的参数Parameter : <%= request.getParameter("sharedparam" ) %>
欢迎转载,请注明出处:www.cnblogs.com/lulipro
为了获得更好的阅读体验,请访问原博客地址。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
代码钢琴家
关于RequestDispatcher的原理的更多相关文章
- JavaWeb——关于RequestDispatcher的原理
RequestDispatcher简介 RequestDispatcher 代表请求的派发者.它有2个动作:forward 和 include .客户端对于任何一个请求,可以根据业务逻辑需要,选择不同 ...
- 【Servlet】关于RequestDispatcher的原理
RequestDispatcher简介 RequestDispatcher 代表请求的派发者.它有2个动作:forward 和 include .客户端对于任何一个请求,可以根据业务逻辑需要,选择不同 ...
- Tomcat 的 ErrorPage 实现原理分析
使用Tomcat,一定见到过404,500的时候,见到过Tomcat提供的错误页面,例如请求的资源找不到的时候,响应状态码为404,这个时候的错误页面是这样的: 这些错误页面是 如何生成及定位展示的 ...
- Spring MVC中DispatcherServlet工作原理探究
转:http://blog.csdn.net/zhouyuqwert/article/details/6853730 下面类图将主要的类及方法抽离出来,以便查看方便,根据类的结构来说明整个请求是如何工 ...
- HttpServletResponse HttpServletRequest RequestDispatcher
HttpServletResponse HttpServletRequest RequestDispatcher 07. 五 / J2EE / 没有评论 一.以字节为单位向客户端发送中文数据1.服 ...
- SpringMVC——使用RequestDispatcher.include()和HttpServletResponseWrapper动态获取jsp输出内容
介绍本篇内容前,先抛出我遇到的问题或者说是需求!(精读阅读本篇可能花费您15分钟,略读需5分钟左右) 一:需求说明 有一个Controller有两个方法 第一个方法通过指定的路径和参数去渲染jsp内容 ...
- Nginx原理和配置总结
一:前言 Nginx是一款优秀的HTTP服务器和反向代理服务器,除却网上说的效率高之类的优点,个人的切身体会是Nginx配置确实简单而且还好理解,和redis差不多,比rabbitmq好理解太多了: ...
- 【spring】- springmvc 工作原理
原理 本质是将DispatcherServlet及关联的Spring上下文环境的初始化工作织入Servlet的生命周期内,将外部WEB请求转换为Spring Bean能处理的形式,然后将处理后的结果借 ...
- Velocity工作原理解析和优化
在MVC开发模式下,View离不开模板引擎,在Java语言中模板引擎使用得最多是JSP.Velocity和FreeMarker,在MVC编程开发模式中,必不可少的一个部分是V的部分.V负责前端的页面展 ...
随机推荐
- 前端框架之bootstrap
一.bootstrap按钮 1.按钮 <button class="btn btn-default">按钮</button><button class ...
- (转)Java多线程之Lock的使用 (待整理)
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- Objective-C 使用核心动画CAAnimation实现动画
先来看看效果吧 整个核心动画就不多做介绍了,随便一搜就能有很多很详细的解释,主要使用以下四种 CABasicAnimation //经典动画 CAKeyframeAnimation //关键帧动画 C ...
- helpers.bulk时 action_request_validation_exception 异常
语言Python 在开发时,批量插入ES,出现了action_request_validation_exception异常.我的代码是这样的 action = { } helpers.bulk(es, ...
- java对Microsoft Document的操作--->对Excel的操作
起初,自己想对网站上爬取一些数据来写到Excel表格中,便在网上找了找java操作Excel接口,了解到Apache的POI接口可以对微软的文档进行操作,WIKI搜索的结果如下, HSSF - 提供读 ...
- Redis-简单实现星形主从配置
高级参考(https://www.zhihu.com/question/21419897) 简单应用场景 现在配置redis 星形 集群, 有三台服务器, 怎样实现? 复制redis.conf两份, ...
- 使用纯css3写出来的表情包 (^v^)
效果如图所示: 不多说,我们直接一个一个来写出,主要列出每个表情的结构,样式我们统一写出,基本全部会用到,颜色以及结构可以根据自己的需求来调整.(里面可是没有一张图片的哦) 页面预览:http://2 ...
- hdu--2084--dp--数塔
#include<iostream> #include<cstring> using namespace std; ; }; void dp(int,int); int n; ...
- HDU 6097---Mindis(二分)
题目链接 Problem Description The center coordinate of the circle C is O, the coordinate of O is (0,0) , ...
- jQuery基础知识总结二
* DOM操作 *1 基本操作 * html() - 等价于innerHTML属性 * text() - 等价于textContent属性 * val() - 等价于value属性 * attr() ...