JavaWeb——关于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" ) %>


JavaWeb——关于RequestDispatcher的原理的更多相关文章
- 关于RequestDispatcher的原理
RequestDispatcher简介 RequestDispatcher 代表请求的派发者.它有2个动作:forward 和 include .客户端对于任何一个请求,可以根据业务逻辑需要,选择不同 ...
- 【Servlet】关于RequestDispatcher的原理
RequestDispatcher简介 RequestDispatcher 代表请求的派发者.它有2个动作:forward 和 include .客户端对于任何一个请求,可以根据业务逻辑需要,选择不同 ...
- javaweb监听器实现与原理
参考:https://www.cnblogs.com/lxp503238/p/6678688.html https://blog.csdn.net/CPOHUI/article/details/888 ...
- JavaWeb总结--Servlet 工作原理解析
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...
- javaweb(十四)——JSP原理
一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...
- javaweb基础(10)_HttpServletRequest原理介绍
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- javaweb基础(7)_HttpServletResponse原理详解
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- javaweb基础(5)_servlet原理
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- 资料汇总--java开发程序员必备技能
1. 熟练使用Java语言进行面向对象程序设计(面向对象:继承.多态.抽象): 有良好的编程习惯(阿里开发手册 链接:http://pan.baidu.com/s/1dFEA6cT 密码:kqj4 ...
随机推荐
- Ubuntu 16.04 环境下配置apache2.4 + php5.6
相信用惯了Windows的朋友一开始接触Linux是很崩溃的,因为很多东西都是通过命令行来完成的,包括安装绝大多数的开发工具以及环境,那么在Ubuntu下其实可以直接通过apt-get指令来安装apa ...
- docker-compose部署mongodb+redis遇到的问题
Demo环境下需要用到Redis+mongodb两种DB配合使用,所以暂时直接使用docker的redids和mongodb镜像,用docker-compose进行联合部署 使用的版本如下: dock ...
- linux文本编辑器vim大全
linux基础之vim编辑器 1.vim编辑器 基本介绍 vim编辑器的前身叫做vi.vi的英文名:Visual Interface.中文解释文本编辑器,你不应该用他去打开二进制可执行文件 文本编辑 ...
- 百度编辑器UEditor 点击上传图片选择框会延迟几秒才会显示 反应很慢(转)
转自:http://www.blogxuan.com/php/show/323.html UEditor 编辑器点击上传文件选择框会延迟几秒才会显示,反应很慢,上传图片选择框显示很慢. 1.uedit ...
- 【转】Cookie/Session机制详解
Cookie/Session机制详解 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息 ...
- vue2上传图片到OSS
第一步:安装阿里云OSS <!-- 引入在线资源 --> <script src="http://gosspublic.alicdn.com/aliyun-oss-sdk- ...
- 服务器A制定计划任务,BAT脚本自动备份oracle数据文件,拷贝至服务器B的共享目录。
运行环境:windows server 2008 R2 目的:在数据库服务器A进行数据库自动备份,并且保留5天. 为了安全,需要在web应用服务器B进行数据库的冗余备份,建立双保险.(保留15天) A ...
- java_集合类_简
Collection 来源于Java.util包,实用常用的数据结构,字面意思就是容器 主要方法 boolean add(Object o)添加对象到集合 boolean remove(Object ...
- 2019年4月zstu月赛A: 我不会做
问题 A: 我不会做 时间限制: 1 Sec 内存限制: 128 MB 题目描述 众所周知,duxing201606就是plw. 然而已经9102年了,plw仍旧没有npy.plw非常难过,于是他打 ...
- 公司内网接口ip城市查询分析
require 'rubygems' require 'json' print ARGV print "fist is :",ARGV[0] logfile="#{ARG ...