1.1 - Server
A Server element represents the entire Catalina servlet container. (Singleton)
1.2 - Service
A Service element represents the combination of one or more Connector components that share a single Engine
Service是这样一个集合:(x)它由一个或者多个Connectorl成Q以?qing)一个EngineQ负责处理所有Connector所获得的客戯?br>
1.3 - Connector
一个Connector在某个指定端口上侦听客戯求,q将获得的请求交lEngine来处理,从Engine处获得回应ƈq回客户
TOMCAT有两个典型的ConnectorQ一个直接侦听来自browser的httphQ一个侦听来自其它WebServer的请?br>
Coyote Http/1.1 Connector 在端?080处侦听来自客户browser的httph
Coyote JK2 Connector 在端?009处侦听来自其它WebServer(Apache)的servlet/jsp代理h
1.4 - Engine
The Engine element represents the entire request processing machinery associated with a particular Service
It receives and processes all requests from one or more Connectors
and returns the completed response to the Connector for ultimate transmission back to the client
Engine下可以配|多个虚拟主机Virtual HostQ每个虚拟主机都有一个域?br>
当Engine获得一个请求时Q它把该h匚w到某个Host上,然后把该h交给该Host来处?br>
Engine有一个默认虚拟主机,当请求无法匹配到M一个Host上的时候,交l该默认Host来处?br>
1.5 - Host
代表一个Virtual HostQ虚拟主机,每个虚拟L和某个网l域名Domain Name相匹?br>
每个虚拟L下都可以部v(deploy)一个或者多个Web AppQ每个Web App对应于一个ContextQ有一个Context path
当Host获得一个请求时Q将把该h匚w到某个Context上,然后把该h交给该Context来处?br>
匚w的方法是“最长匹?#8221;Q所以一个path==""的Context成Host的默认Context
所有无法和其它Context的\径名匚w的请求都最l和该默认Context匚w
1.6 - Context
一个Context对应于一个Web ApplicationQ一个Web Application׃个或者多个Servletl成
Context在创建的时候将Ҏ(gu)配置文g$CATALINA_HOME/conf/web.xml?WEBAPP_HOME/WEB-INF/web.xml载入Servletc?br>
当Context获得hӞ在自己的映表(mapping table)中寻扄匚w的Servletc?br>
如果扑ֈQ则执行该类Q获得请求的回应Qƈq回
该文件描qC如何启动Tomcat Server
<!----------------------------------------------------------------------------------------------->
<!-- 启动Server
在端?005处等待关闭命?br> 如果接受?SHUTDOWN"字符串则关闭服务?br> -->
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<!-- Listener ???
目前没有看到q里
-->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
<!-- Global JNDI resources ???
目前没有看到q里Q先略去
-->
<GlobalNamingResources>
... ... ... ...
</GlobalNamingResources>
<!-- Tomcat的Standalone Service
Service是一lConnector的集?br> 它们q一个Engine来处理所有Connector收到的请?br> -->
<Service name="Tomcat-Standalone">
<!-- Coyote HTTP/1.1 Connector
className : 该Connector的实现类是org.apache.coyote.tomcat4.CoyoteConnector
port : 在端口号8080处侦听来自客户browser的HTTP1.1h
minProcessors : 该Connector先创?个线E等待客戯求,每个h׃个线E负?br> maxProcessors : 当现有的U程不够服务客户hӞ若线EL不75个,则创建新U程来处理请?br> acceptCount : 当现有线E已l达到最大数75Ӟ为客戯求排?br> 当队列中h数超q?00Ӟ后来的请求返回Connection refused错误
redirectport : 当客戯求是httpsӞ把该h转发到端?443?br> 其它属性略
-->
<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
port="8080"
minProcessors="5" maxProcessors="75" acceptCount="100"
enableLookups="true"
redirectPort="8443"
debug="0"
connectionTimeout="20000"
useURIValidationHack="false"
disableUploadTimeout="true" />
<!-- Engine用来处理Connector收到的Httph
它将匚wh和自q虚拟LQƈ把请求{交给对应的Host来处?br> 默认虚拟L是localhost
-->
<Engine name="Standalone" defaultHost="localhost" debug="0">
<!-- 日志c,目前没有看到Q略d -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- RealmQ目前没有看刎ͼ略去?-->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" .../>
<!-- 虚拟Llocalhost
appBase : 该虚拟主机的根目录是webapps/
它将匚wh和自qContext的\径,q把h转交l对应的Context来处?br> -->
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- 日志c,目前没有看到Q略d -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- ContextQ对应于一个Web App
path : 该Context的\径名?"Q故该Context是该Host的默认Context
docBase : 该Context的根目录是webapps/mycontext/
-->
<Context path="" docBase="mycontext" debug="0"/>
<!-- 另外一个ContextQ\径名?wsota -->
<Context path="/wsota" docBase="wsotaProject" debug="0"/>
</Host>
</Engine>
</Service>
</Server>
<!----------------------------------------------------------------------------------------------->
一个Context对应于一个Web AppQ每个Web App是由一个或者多个servletl成?br>
当一个Web App被初始化的时候,它将用自qClassLoader对象载入“部v配置文gweb.xml”中定义的每个servletc?br>
它首先蝲入在$CATALINA_HOME/conf/web.xml中部|的servletc?br>
然后载入在自qWeb App根目录下的WEB-INF/web.xml中部|的servletc?br>
web.xml文g有两部分Qservletcd义和servlet映射定义
每个被蝲入的servletc都有一个名字,且被填入该Context的映表(mapping table)中,和某UURL PATTERN对应
当该Context获得hӞ查询mapping tableQ找到被h的servletQƈ执行以获得请求回?br>
分析一下所有的Context׃n的web.xml文gQ在其中定义的servlet被所有的Web App载入
<!----------------------------------------------------------------------------------------------->
<web-app>
<!-- 概述Q?br> 该文件是所有的WEB APPq的部|配|文Ӟ
每当一个WEB APP被DEPLOYQ该文g都将先被处理Q然后才是WEB APP自己?WEB-INF/web.xml
-->
<!-- +-------------------------+ -->
<!-- | servletcd义部? | -->
<!-- +-------------------------+ -->
<!-- DefaultServlet
当用L(fng)HTTPh无法匚wM一个servlet的时候,该servlet被执?br> URL PATTERN MAPPING : /
-->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- InvokerServlet
处理一个WEB APP中的匿名servlet
当一个servlet被编写ƈ~译攑օ/WEB-INF/classes/中,却没有在/WEB-INF/web.xml中定义的时?br> 该servlet被调用,把匿名servlet映射?servlet/ClassName的Ş?br> URL PATTERN MAPPING : /servlet/*
-->
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- JspServlet
当请求的是一个JSP面的时候(*.jspQ该servlet被调?br> 它是一个JSP~译器,请求的JSP面~译成ؓ(f)servlet再执?br> URL PATTERN MAPPING : *.jsp
-->
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>logVerbosityLevel</param-name>
<param-value>WARNING</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- +---------------------------+ -->
<!-- | servlet映射定义部分 | -->
<!-- +---------------------------+ -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<!-- +------------------------+ -->
<!-- | 其它部分Q略d | -->
<!-- +------------------------+ -->
... ... ... ...
</web-app>
<!----------------------------------------------------------------------------------------------->
假设来自客户的请求ؓ(f)Q?br>
http://localhost:8080/wsota/wsota_index.jsp
When developing J2EE web applications, the task of database connection management can be daunting. Best practice involves using a J2EE DataSource to provide connection pooling, but configuring DataSources in web application servers and connecting your application to them is often a cumbersome process and poorly documented.
The usual procedure requires the application developer to set up a DataSource in the web application server, specifying the driver class, JDBC URL (connect string), username, password, and various pooling options. Then, the developer must reference the DataSource in his application's web.xml configuration file, and then access it properly in his servlet or JSP. Particularly during development, setting all of this up is tedious and error-prone.
With Tomcat 5.5, the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don't have to mess with configuring the DataSource separately in the Tomcat server.xml, or referencing it in your application's web.xml file. Here's how:
Install the .jar file(s) containing the JDBC driver in Tomcat's common/lib folder. You do not need to put them in your application's WEB-INF/lib folder. When working with J2EE DataSources, the web application server manages connections for your application.
In the root of your web app directory structure, create a folder named META-INF (all caps). Inside that folder, create a file named context.xml that contains a Resource like this:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/WallyDB" auth="Container"
type="javax.sql.DataSource" username="wally" password="wally"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"
maxActive="8"
/>
</Context>
This example shows how to configure a DataSource for a SQL Server database named mytest located on the development machine. Simply edit the Resource name, driverClassName, username, password, and url to provide values appropriate for your JDBC driver.
Here's how you might access the data in a servlet:
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB");
Connection c = ds.getConnection();
...
c.close();
Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/
Here's a sample web application project that shows where all the files go. This one shows how to access data from from a JSP page: datasourcedemo.war
<Resource name="jdbc/denali" auth="Container" type="javax.sql.DataSource"
username="denali" url="jdbc:postgresql://localhost:5432/demo"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="org.postgresql.Driver"
maxActive="20" maxIdle="10"/>
<Resource name="jdbc/ccsdatasource" auth="Container" type="javax.sql.DataSource"
username="ccs" password="secret" url="jdbc:mysql://localhost:3306/ccs"
driverClassName="com.mysql.jdbc.Driver"
maxActive="20" maxIdle="10"/>
This technique is Tomcat-specific. If you deploy your web application to another application server (or even an older version of Tomcat), you will need to configure the database according to your application server's documentation, and reference it in your application's web.xml.