关于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负责前端的页面展 ...
随机推荐
- tensorflow softsign函数应用
1.softsign函数 图像 2.tensorflow softsign应用 import tensorflow as tf input=tf.constant([0,-1,2,-30,30],dt ...
- mk框架,一个基于react、nodejs全栈框架
在这个前端技术爆炸的时代,不自己写套开源框架出门都不好意思跟别人打招呼,作为一个前端领域的小学生,去年年初接触了react,之后一发不可收拾爱上了它,近期重构了自己去年开源的一个项目,废话到此结束句号 ...
- windows本地提权对照表(转载)
2003 systeminfo>C:\Windows\Temp\temp.txt&(for %i in (KB3057191 KB2840221 KB3000061 KB2850 ...
- appach-maven-3.5.0配置与下载
今天需要搭建javaweb开发的环境,需要配置maven.弄了半天终于配置好了.蛋疼的一批.楼主用的是win10的系统.不知道Linux下的也是不是这么坑! 首先,需要把maven的包下载下来.进入h ...
- 使用yeoman构建angular应用
本文将介绍如何亲手来完成一个yeoman的generator,以实现快速构建最适合自己的项目. 本文将实现的generator起名为ngtimo,依照yeoman的命名规矩就叫做generator-n ...
- 设计模式的征途—19.命令(Command)模式
在生活中,我们装修新房的最后几道工序之一是安装插座和开关,通过开关可以控制一些电器的打开和关闭,例如电灯或换气扇.在购买开关时,用户并不知道它将来到底用于控制什么电器,也就是说,开关与电灯.换气扇并无 ...
- Java微信公众平台开发之公众号支付(微信内H5调起支付)
官方文档点击查看准备工作:已通过微信认证的公众号,必须通过ICP备案域名(否则会报支付失败)借鉴了很多大神的文章,在此先谢过了 整个支付流程,看懂就很好写了 一.设置支付目录 在微信公众平台设置您的公 ...
- ORACLE处理用户进程大剖析[阅读]
下面我们要讲ORACLE服务器进程如何处理用户进程的请求,当一个用户进程发出了一条SQL语名: UPDATE TABBLEA SET SALARY=SALARY*2: 首先,服务器进程把这条语 ...
- SQL语句Tips
1. 可以在FROM字句中指定多个表,表与表之间用逗号隔开,如下面所示. SELECT * FROM T1,T123 结果集是这两个表的笛卡尔乘积.
- MySQL数据库— 汇总和分组数据
一 汇总和分组数据 查询语句 ---> 结果集(多条数据) ---> 聚合函数 ----> 单行记录 1.常用的聚合函数: sum() 数字 ...