Chapter 5: Container
Chapter 5: Container
A container is a module that processes the requests for a servlet and populates the response objects for web clients. A container is represented by the org.apache.catalina.Container interface and there are four types of containers: Engine, Host, Context, Wrapper. This chapter covers Context and Wrapper and leaves Engine and Host to Chapter 13. This chapter starts with the discussion of the Container interface, followed by the piplining mechanism in a container. It then looks at the Wrapper and Context interfaces by presenting 2 applications.
The Container Interface
A container must implements org.apache.catalina.Container interface. Recall the folling the code from the Bootstrap class in the application in Chapter 4.
HttpConnector connector = new HttpConnector();
SimpleContainter container = new SimpleContainer();
connector.setContainer(container);
The first thing to note about containers in Catalina is that there are four types of containers at different conceptual levels:
- Engine. Represents the entire Catalina servlet engine.
- Host. Represents a virtual host with a number of contexts
- Context. Represents a web application. A context contains one or more wrappers.
- Wrapper. Represents an individual servlet.
Figure 5.1 shows the class diagram of the Container interface and its sub-interfaces and implementations. Note that all interfaces are part of the org.apache.catalina package and all classes are part of the org.apache.catalina.core package.

A functional Catalina deployment does not need all the 4 types of containers. For example, the container module in this chapter's first application consists of only a wrapper.
A container can have zero or more child containers of the lower level. For instance, a context normally has one or more wrappers and a host can have zero or more context. However, a wrapper, being the lowest in the hierachy, cannot contain a child container. There are serveral methods in the Container interface.
public void addChild(Container child);
public void removeChild(Conainter child);
public Container findChind(String name);
public Container[] findChildren();
A container can also contain a number of support components such as Loader, Logger, Manager, Realm, and Resources. We will discuss thest components in later chapter.
More interestingly, the Container interface has been designed in such a way that at the time of deployment, a Tomcat administrator can determine what a container performs by editing the configuration file(server.xml). This is achieved by introducing a pipline and a set of valves in a container, which we'll disucss in the next section, "Piplining Tasks".
Note: The container interface in Tomcat 4 is slightly different from that in Tomcat 5. For example, in Tomcat 4 this interface has a map method, which no longer exists in the Container interface in Tomcat 5.
Piplining Tasks
This section explain what happens when a container's invoke method is called by the connector.
A pipeline contains tasks that the container will invoke. A valve represents a specific task. There is one basic valve in a container's pipeline, but you can add as many valves as you can. The number of valves is defined to be the number of additional valves, i.e. not including the basic valve. Vavles can be added dynamically by editing Tomcat's configuration file(server.xml).
If you understand servlet filters, it is no hard to imagine how a pipeline and its valves work. A pipeline is like a fiter chain and each valve is filter. Like a filter, a valve can manipulate the request and response objects passed to it. After a valve finishes processing, it calls the next valve in the pipeline. The basic valve is always called the last.
A container can have one pipeline.
When a container's invoke method is called, the container passes processing to its pipeline and the pipeline invokes the first valve in it, which will then invoke the next valve, and so on, until there is no more valve in the pipeline. You might imagine that you have the following pseudo code inside the pipeline's invoke method:
// invoke each valve added to the pipeline
for(i=0; i<valves.length; i++) {
valves[i].invoke(...);
}
// then, invoke the basic valve
basicValve.invoke(....);
Howere, the tomcat designer chose a different approach by introducing the org.apache.cataline.ValveContext interface. Here is how it works.
A container does not hard code what it is supposed to do when its invoke method is called by the connector. Instead, the container calls its pipeline's invoke method. The Pipeline interface's invoke method has the following signature, which is exactly the same as the invoke method of the Container interface.
public void invoke(Request request, Response response) throws IOException, ServletException;
Here is the implementation of the Container interface's invoke method in the org.apache.catalina.core.ContainerBase class.
public void invoke(Request request, Response response) throws IOException, ServletException {
pipeline.invoke(request, response);
}
where pipeline is an instance of the Pipeline interface inside the container.
Now, the pipeline has to make sure that all the valves added to it as well as its basic valve must be invoked once. The pipeline does this by creating an instance of the ValveContext interface. The ValveContext is implemented as an inner class of the pipeline so that the ValveContext has access to all members of the pipeline. The most important method of the ValveContext interface is:
public void invokeNext(Request request, Response response) throws IOException, ServletException;
The Wrapper Interface
The Context Interface
The Wrapper Application
The Context Application
Summary
Chapter 5: Container的更多相关文章
- qt 总结
Qt中的每个类,都有一个对应的同名头文件,其中包含其类定义.例如要使用QApplication类,则需要在程序中添加" #include <QApplication>" ...
- Unity文档阅读 第三章 依赖注入与Unity
Introduction 简介In previous chapters, you saw some of the reasons to use dependency injection and lea ...
- Chapter 2: A Simple Servlet Container
一.这一章从头构建一个简单的Servlet容器,可以处理Servlet和静态资源(如html文件/图片等). 要处理Servlet,必须遵循javax.servlet.Servlet规范,而处理静态资 ...
- Chapter 4: Tomcat Default Connector
一.概述 第三章介绍的connector是一个很好的学习工具,但是我们还可以做的更多.这一章介绍的是Tomcat4默认的connector. 一个Tomcat的connector是一个独立的模块,能够 ...
- Chapter 3: Connector(连接器)
一.概述 Tomcat或者称之为Catalina(开发名称),可以简化为两个主要的模块,如下图: 多个Connector关联一个Container.之所以需要多个Connector,是为了处理多种协议 ...
- Spring Batch Concepts Chapter
Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...
- JavaScript- The Good Parts Chapter 5 Inheritance
Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...
- JavaScript- The Good Parts Chapter 3 Objects
Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
随机推荐
- Java多线程基础:进程和线程之由来
转载: Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程.当然,Java并发编程涉及到很多方面的内容,不是一朝一夕就能够 ...
- Android 子activity关闭 向父activity传值
使用startActivity方式启动的Activity和它的父Activity无关,当它关闭时也不会提供任何反馈. 可变通的,你可以启动一个Activity作为子Activity,它与父Activi ...
- [Hadoop 周边] Hadoop和大数据:60款顶级大数据开源工具(2015-10-27)【转】
说到处理大数据的工具,普通的开源解决方案(尤其是Apache Hadoop)堪称中流砥柱.弗雷斯特调研公司的分析师Mike Gualtieri最近预测,在接下来几年,“100%的大公司”会采用Hado ...
- OC 继承子类对象调用方法机制 子类对象访问父类中的实例变量
在继承中,子类对象如何调用到正确方法的机制 每一个Objective - C对象都有一个隐藏的指针指向类的代码,当向一个对象发送消息的时候,当前的对象会首先在当前类里去查找相应的方法,如果找到的话,直 ...
- eclipse中SVN分支合并到主干
在项目开发中,需要添加一些新的功能,但是又不想影响到其他开发人员的项目进度,所以决定使用SVN分支进行开发,分支开发完毕后再合并到主干.本文介绍如何在eclipse中合并分支到主干. 1. 要想将分支 ...
- 在express3.0上使用模板
express3.0取消了layout设置,为了能使用模版,经过百度后发现有个express-partials模块可以使用 1:安装 npm install express-partials 模块安装 ...
- 并发容器之CopyOnWriteArrayList
原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...
- checkbox改成radio效果,单选,取消
$(function () { var allBox = $(":checkbox"); allBox.click(function ( ...
- [转载]linux下svn常用指令
一下内容转载于:http://blog.chinaunix.net/space.php?uid=22976768&do=blog&id=1640924.这个总结的很好~ windows ...
- [Js]Ajax
一.什么是Ajax 不刷新的情况下读取数据或提交数据 (最早出现ajax:谷歌地图,拖动一下出现一片新的视野) 应用:用户注册.在线聊天.微博 特性:只能从服务器上去读取数据(所以我们需要配置自己的服 ...