Servlet---RequestDispatcher.include方法
为了实现代码重用,需要将某些代码和数据放在一个或多个Servlet中,以供其他Servlet使用,提供了RequestDispatcher.include方法,首先通过getServletContext方法获取ServletContext对象,然后通过ServletContext.getRequestDispatcher方法获取RequestDispatcher对象。
以下代码中,IncludingServlet类演示了如何使用Include方法包含web资源。
//包含了一个Servlet:IncludedServlet,和一个HTML页面
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置Context-type字段值,类型为text/html,编码格式为utf-8
// response.setHeader("Content-type", "text/html;charset:utf-8");
response.setContentType("text/html;charset=utf-8");
// 获取PrintWriter对象
PrintWriter out = response.getWriter();
// response.setCharacterEncoding("UTF-8");
// 向客户端输出信息
out.println("hello world<br/>");
// 向客户端输出请求URI
out.println("Including URI:" + request.getRequestURI() + "<p/>");
// 封装名为IncludedServlet的Servlet
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/IncludedServlet");
rd.include(request, response);
// 封装名为IncludedHTML.html页面
rd = getServletContext().getRequestDispatcher("/IncludedHTML.html");
rd.include(request, response);
}
1.里面包含两个web资源,映射路径为“/IncludedServlet”的Servlet类和一个名为IncludedHTML.html的静态页面
2.getRequestDispatcher方法的参数值必须以“/”开头。
IncludedServlet类的代码
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置Context-type字段值,类型为text/html,编码格式为UTF-8
response.setContentType("text/html;charset=UTF-8");
//获取PrintWriter对象
PrintWriter out=response.getWriter();
//向客户端输出带HTML代码的信息
out.println("<b>crazy</b><br/>");
//向客户端输出请求URI
out.println("IncludedServlet URI:"+request.getRequestURI()+"<p/>");
}
IncludedHTML.html的代码
<!-- 利用表格显示相关信息 -->
<table border="1">
<tr>
<td>name</td>
<td>sex</td>
</tr>
<tr>
<td>zhang</td>
<td>male</td>
</tr>
<tr>
<td>zheng</td>
<td>female</td>
</tr>
</table>
运行结果如图

Servlet---RequestDispatcher.include方法的更多相关文章
- 关于“javax.servlet.include.request_uri”属性值 include 请求 RequestDispatcher.include
在springMVC的DispatcherServlet类的doService方法中有如下代码: 1 2 3 4 5 6 7 8 9 10 if (WebUtils.isIncludeRequest( ...
- SpringMVC——使用RequestDispatcher.include()和HttpServletResponseWrapper动态获取jsp输出内容
介绍本篇内容前,先抛出我遇到的问题或者说是需求!(精读阅读本篇可能花费您15分钟,略读需5分钟左右) 一:需求说明 有一个Controller有两个方法 第一个方法通过指定的路径和参数去渲染jsp内容 ...
- JAVA记录-Servlet RequestDispatcher请求转发
RequestDispatcher接口提供将请求转发送到另一个资源的功能,它可能是html,servlet或jsp等. 此接口也可用于包括另一资源的内容.它是servlet协作的一种方式. 在Requ ...
- Servlet RequestDispatcher需要注意的情况
Servlet RequestDispatcher需要注意的情况: 如果使用getServletContext().getRequestDispatcher("/a.do")得到R ...
- Servlet生命周期方法,request.getRequestDispatcher
1,request.getRequestDispatcher 方法全称javax.servlet.ServletRequest.getRequestDispatcher(String) 2,在web. ...
- Servlet体系及方法
时间:2016-11-11 15:07 --Servlet体系Servlet(interface): 实现类:GenericServlet.HttpServletServletConfig(in ...
- 《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-7 在别的LINQ查询操作中使用Include()方法 问题 你有一个LINQ ...
- WEB启动时就加载servlet的dopost方法
web启动的时候可以加载servlet的init方法,无法加载dopost方法,如果你需要什么内容在启动的时候执行,可以将内容放到init方法里面,dopost方法,是在客户端使用post请求的时候才 ...
- Servlet的init()方法如何才会在服务器启动时执行
如果要想让 servlet 的 init () 方法在服务器启动 时就被执行,则需要在 web.xml 中相应的 servlet 下配置 <servlet > <servlet -n ...
随机推荐
- The trip(Uva 11100)
题目大意: 给出n个数,要求将其分成最少的递增序列,保证序列最少的同时要使得序列长度的最大值最小. n<=10000 题解: 1.我是直接看着<训练指南>里的中文题面的,lrj似乎 ...
- 读取.properties配置文件
方法1 public class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...
- [SHTSC 2014] 信号增幅仪
最小覆盖圆算法.看着题解半蒙半抄的搞过去了… 主要参考以下http://blog.csdn.net/acdreamers/article/details/9406735http://blog.csdn ...
- java开发连接Oracle 12c采用PDB遇到问题记录
今天初次使用java连接Oracle 12c,遇到各种问题,为方便后续查询,在汇总了问题记录及解决方案如下. ORA-28040: No matching authentication protoco ...
- 【转】Memcached安装
解析:Memcached是什么? Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. 一.软件版本 ...
- 借用Google API在线生成网站二维码地址方法
二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...
- JAVA编程讲座-吴老
JAVA系列公开课第4讲:多态系列课程:从JAVA编程零基础讲起,同时结合工作中遇到的具体实例,语言清晰易懂,连续10周+深入讲解,打下编程基础,让我们一起打来自动化测试的大门时间:4月25日(周一) ...
- 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》
Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...
- swift 闭包循环引用
当使用闭包时,类本身持有self,然后又在闭包中访问了self或者self的属性,就会导致恶心额循环引用.swift提供的解决方法是在闭包中定义捕获列表,捕获列表是闭包想怎么引用捕获来的变量.例如下面 ...
- sql语句的匹配
like 的通配符有两种 %(百分号):代表零个.一个或者多个字符. _(下划线):代表一个数字或者字符. 1. name以"李"开头 where name like '李%' 2 ...