0%

Web应用加载

Catalina对于Web应用的加载主要由StandardHost、HostConfig、StandardContext、ContextConfig、StandardWrapper这五个类完成。

阅读全文 »

Connector连接器

Coyote是Tomcat连接器框架的名称,是Tomcat服务器提供的供客户端访问的外部接口。客户端通过Coyote与服务器建立连接、发送请求并接收响应。

Connector封装了底层的网络通信(Socket请求和响应),为Catalina容器提供了统一的接口,使Catalina容器与具体的请求协议及I/O方式解耦。Connector将Socket输入转换为Request对象,交由Catalina容器进行处理,处理完成后,Catalina通过Connector提供的Response对象将结果写入到输出流

Connector就是ServerSocket来等待HTTP请求,Processor解析HTTP请求来创建HttpRequest和HttpResponse

阅读全文 »

tomcat之Container容器

连接器负责对外连接,容器负责内部处理。

之前分析tomcat的组件时说过,Container 用于封装Servlet,以及具体处理request请求,Container是一个接口,针对不同级别的容器分为四个子接口:Engine、Host、Context、Wrapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Container是父接口
public interface Container extends Lifecycle {
/**
* Obtain the JMX domain under which this container will be / has been
* registered.
*
* @return The JMX domain name
*/
public String getDomain();


/**
* Return the Pipeline object that manages the Valves associated with
* this Container.
*
* @return The Pipeline
*/
public Pipeline getPipeline();


/**
* Return a name string (suitable for use by humans) that describes this
* Container. Within the set of child containers belonging to a particular
* parent, Container names must be unique.
*
* @return The human readable name of this container.
*/
public String getName();


/**
* Set a name string (suitable for use by humans) that describes this
* Container. Within the set of child containers belonging to a particular
* parent, Container names must be unique.
*
* @param name New name of this container
*
* @exception IllegalStateException if this Container has already been
* added to the children of a parent Container (after which the name
* may not be changed)
*/
public void setName(String name);


/**
* Get the parent container.
*
* @return Return the Container for which this Container is a child, if
* there is one. If there is no defined parent, return
* <code>null</code>.
*/
public Container getParent();


/**
* Set the parent Container to which this Container is being added as a
* child. This Container may refuse to become attached to the specified
* Container by throwing an exception.
*
* @param container Container to which this Container is being added
* as a child
*
* @exception IllegalArgumentException if this Container refuses to become
* attached to the specified Container
*/
public void setParent(Container container);


/**
* Get the parent class loader.
*
* @return the parent class loader for this component. If not set, return
* {@link #getParent()}.{@link #getParentClassLoader()}. If no
* parent has been set, return the system class loader.
*/
public ClassLoader getParentClassLoader();


/**
* Set the parent class loader for this component. For {@link Context}s
* this call is meaningful only <strong>before</strong> a Loader has
* been configured, and the specified value (if non-null) should be
* passed as an argument to the class loader constructor.
*
* @param parent The new parent class loader
*/
public void setParentClassLoader(ClassLoader parent);


/**
* Obtain the Realm with which this Container is associated.
*
* @return The associated Realm; if there is no associated Realm, the
* Realm associated with the parent Container (if any); otherwise
* return <code>null</code>.
*/
public Realm getRealm();


/**
* Set the Realm with which this Container is associated.
*
* @param realm The newly associated Realm
*/
public void setRealm(Realm realm);


/**
* Add a new child Container to those associated with this Container,
* if supported. Prior to adding this Container to the set of children,
* the child's <code>setParent()</code> method must be called, with this
* Container as an argument. This method may thrown an
* <code>IllegalArgumentException</code> if this Container chooses not
* to be attached to the specified Container, in which case it is not added
*
* @param child New child Container to be added
*
* @exception IllegalArgumentException if this exception is thrown by
* the <code>setParent()</code> method of the child Container
* @exception IllegalArgumentException if the new child does not have
* a name unique from that of existing children of this Container
* @exception IllegalStateException if this Container does not support
* child Containers
*/
public void addChild(Container child);

/**
* Obtain a child Container by name.
*
* @param name Name of the child Container to be retrieved
*
* @return The child Container with the given name or <code>null</code> if
* no such child exists.
*/
public Container findChild(String name);


/**
* Obtain the child Containers associated with this Container.
*
* @return An array containing all children of this container. If this
* Container has no children, a zero-length array is returned.
*/
public Container[] findChildren();


/**
* Remove an existing child Container from association with this parent
* Container.
*
* @param child Existing child Container to be removed
*/
public void removeChild(Container child);
}

四个子容器的装配关系如下:

Engine是最顶层的容器,每个Service最多只能有一个Engine,Engine中可以有多个Host,每个Host下可以有多个Context,每个Context下可以有多个Wrapper

容器装配关系

阅读全文 »

Catalina

在Tomcat4.0版本,将Servlet容器命名为Catalina。

Catalina包含Tomcat所有容器组件,通过松耦合的方式集成Coyote,以及完成按照请求协议进行数据读写,同时还包括启动入口和Shell程序。Catalina解析server.xml文件来创建Server,并将其内的各组件创建出来

阅读全文 »

tomcat说明

tomcat是一个servlet容器,有3个基本任务,

  • 当第一次调用某个servlet时,载入该Servlet类,并调用其init方法(该方法只调用一次)
  • 针对每个请求,创建一个ServletRequest对象和一个ServletResponse对象
  • 调用Servlet的service方法

tomcat目录说明

  • bin:存放启动和关闭Tomcat的脚本文件
  • conf:存放Tomcat服务器的各种配置文件
    • conf/Catalina 用于存储针对每个虚拟机的Context配置
    • conf/context.xml 用于定义所有web应用均需要加载的context.xml配置,如果web应用指定了自己的context.xml,那么该文件的配置将被覆盖
    • conf/catalina.properties tomcat环境配置
    • conf/catalina.policy tomcat在安全模式下运行时,此文件为默认的安全策略配置
    • conf/logging.properties 日志配置
    • conf/server.xml 核心配置文件,用于配置链接器、监听端口、处理请求的虚拟主机等
    • conf/tomcat-users.xml 定义tomcat默认用户及角色信息
    • conf/web.xml 所有应用默认的部署描述文件,主要定义了基础的Servlet和MIME映射,如果应用中不包含web.xml,tomcat将使用该文件初始化部署描述,否则会进行合并
  • lib:存放tomcat服务器依赖的jar包
  • logs:存放Tomcat的日志文件
  • temp:存放Tomcat运行时产生的临时文件
  • webapps:web应用部署目录,即供外界访问的web资源的存放目录
  • work:Tomcat的工作目录,web应用jsp代码生成和编译临时目录
阅读全文 »