﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-honzeland-文章分类-Tomcat</title><link>http://www.blogjava.net/honzeland/category/24427.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 26 Jul 2007 12:58:16 GMT</lastBuildDate><pubDate>Thu, 26 Jul 2007 12:58:16 GMT</pubDate><ttl>60</ttl><item><title>Tomcat启动分析</title><link>http://www.blogjava.net/honzeland/articles/132618.html</link><dc:creator>honzeland</dc:creator><author>honzeland</author><pubDate>Thu, 26 Jul 2007 11:13:00 GMT</pubDate><guid>http://www.blogjava.net/honzeland/articles/132618.html</guid><wfw:comment>http://www.blogjava.net/honzeland/comments/132618.html</wfw:comment><comments>http://www.blogjava.net/honzeland/articles/132618.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/honzeland/comments/commentRss/132618.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/honzeland/services/trackbacks/132618.html</trackback:ping><description><![CDATA[<h3>ZZ: http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html<br></h3>
<h3>
1 - Tomcat Server的组成部分
</h3>
<p><strong>
1.1 - Server
</strong></p>
<p>
A Server element represents the entire Catalina servlet container. (Singleton)
</p>
<p><strong>
1.2 - Service
</strong></p>
<p>
A Service element represents the combination of one or more Connector components that share a single Engine<br>
Service是这样一个集合：它由一个或者多个Connector组成，以及一个Engine，负责处理所有Connector所获得的客户请求<br>
</p>
<p><strong>
1.3 - Connector
</strong></p>
<p>
一个Connector将在某个指定端口上侦听客户请求，并将获得的请求交给Engine来处理，从Engine处获得回应并返回客户<br>
TOMCAT有两个典型的Connector，一个直接侦听来自browser的http请求，一个侦听来自其它WebServer的请求<br>
Coyote Http/1.1 Connector 在端口8080处侦听来自客户browser的http请求<br>
Coyote JK2 Connector 在端口8009处侦听来自其它WebServer(Apache)的servlet/jsp代理请求<br>
</p>
<p><strong>
1.4 - Engine
</strong></p>
<p>
The Engine element represents the entire request processing machinery associated with a particular Service<br>
It receives and processes all requests from one or more Connectors<br>
and returns the completed response to the Connector for ultimate transmission back to the client<br>
Engine下可以配置多个虚拟主机Virtual Host，每个虚拟主机都有一个域名<br>
当Engine获得一个请求时，它把该请求匹配到某个Host上，然后把该请求交给该Host来处理<br>
Engine有一个默认虚拟主机，当请求无法匹配到任何一个Host上的时候，将交给该默认Host来处理<br>
</p>
<p><strong>
1.5 - Host
</strong></p>
<p>
</p>
<p>
代表一个Virtual Host，虚拟主机，每个虚拟主机和某个网络域名Domain Name相匹配<br>
每个虚拟主机下都可以部署(deploy)一个或者多个Web App，每个Web App对应于一个Context，有一个Context path<br>
当Host获得一个请求时，将把该请求匹配到某个Context上，然后把该请求交给该Context来处理<br>
匹配的方法是&#8220;最长匹配&#8221;，所以一个path==""的Context将成为该Host的默认Context<br>
所有无法和其它Context的路径名匹配的请求都将最终和该默认Context匹配<br>
</p>
<p><strong>
1.6 - Context
</strong></p>
<p>
一个Context对应于一个Web Application，一个Web Application由一个或者多个Servlet组成<br>
Context在创建的时候将根据配置文件$CATALINA_HOME/conf/web.xml和$WEBAPP_HOME/WEB-INF/web.xml载入Servlet类<br>
当Context获得请求时，将在自己的映射表(mapping table)中寻找相匹配的Servlet类<br>
如果找到，则执行该类，获得请求的回应，并返回<br>
</p>
<h3>
2 - Tomcat Server的结构图
</h3>
<p>
<img  src="http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.gif" tppabs="http://zooo.51.net/heavyz/cs/tomcat/notes/01-startup.gif">
</p>
<h3>
3 - 配置文件$CATALINA_HOME/conf/server.xml的说明
</h3>
<p>
该文件描述了如何启动Tomcat Server
</p>
<p>
<tt>
<pre><font color="green"><br>&lt;!-----------------------------------------------------------------------------------------------&gt;<br></font><br><br><font color="green"><br>&lt;!-- 启动Server<br>     在端口8005处等待关闭命令<br>     如果接受到"SHUTDOWN"字符串则关闭服务器<br>     --&gt;<br></font><br>&lt;Server port="8005" shutdown="SHUTDOWN" debug="0"&gt;<br><br><font color="green"><br>  &lt;!-- Listener ???<br>       目前没有看到这里<br>       --&gt;<br></font><br>  &lt;Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/&gt;<br>  &lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/&gt;<br><br><font color="green"><br>  &lt;!-- Global JNDI resources ???<br>       目前没有看到这里，先略去<br>       --&gt;<br></font><br>  &lt;GlobalNamingResources&gt;<br>    ... ... ... ...<br>  &lt;/GlobalNamingResources&gt;<br><br><font color="green"><br>  &lt;!-- Tomcat的Standalone Service<br>       Service是一组Connector的集合<br>       它们共用一个Engine来处理所有Connector收到的请求<br>       --&gt;<br></font><br>  &lt;Service name="Tomcat-Standalone"&gt;<br><br><font color="green"><br>    &lt;!-- Coyote HTTP/1.1 Connector<br>         className : 该Connector的实现类是org.apache.coyote.tomcat4.CoyoteConnector<br>         port : 在端口号8080处侦听来自客户browser的HTTP1.1请求<br>         minProcessors : 该Connector先创建5个线程等待客户请求，每个请求由一个线程负责<br>         maxProcessors : 当现有的线程不够服务客户请求时，若线程总数不足75个，则创建新线程来处理请求<br>         acceptCount : 当现有线程已经达到最大数75时，为客户请求排队<br>                       当队列中请求数超过100时，后来的请求返回Connection refused错误<br>         redirectport : 当客户请求是https时，把该请求转发到端口8443去<br>         其它属性略<br>         --&gt;<br></font><br>    &lt;Connector className="org.apache.coyote.tomcat4.CoyoteConnector" <br>               port="8080" <br>               minProcessors="5" maxProcessors="75" acceptCount="100" <br>               enableLookups="true" <br>               redirectPort="8443" <br>               debug="0" <br>               connectionTimeout="20000" <br>               useURIValidationHack="false" <br>               disableUploadTimeout="true" /&gt;<br><br><font color="green"><br>    &lt;!-- Engine用来处理Connector收到的Http请求<br>         它将匹配请求和自己的虚拟主机，并把请求转交给对应的Host来处理<br>         默认虚拟主机是localhost<br>         --&gt;<br></font><br>    &lt;Engine name="Standalone" defaultHost="localhost" debug="0"&gt;<br>    <br><font color="green"><br>      &lt;!-- 日志类，目前没有看到，略去先 --&gt;<br></font><br>      &lt;Logger className="org.apache.catalina.logger.FileLogger" .../&gt;<br><font color="green"><br>      &lt;!-- Realm，目前没有看到，略去先 --&gt;<br></font><br>      &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" .../&gt;<br><br><font color="green"><br>      &lt;!-- 虚拟主机localhost<br>           appBase : 该虚拟主机的根目录是webapps/<br>           它将匹配请求和自己的Context的路径，并把请求转交给对应的Context来处理<br>           --&gt;<br></font><br>      &lt;Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true"&gt;<br>      <br><font color="green"><br>        &lt;!-- 日志类，目前没有看到，略去先 --&gt;<br></font><br>        &lt;Logger className="org.apache.catalina.logger.FileLogger" .../&gt;<br>      <br><font color="green"><br>        &lt;!-- Context，对应于一个Web App<br>             path : 该Context的路径名是""，故该Context是该Host的默认Context<br>             docBase : 该Context的根目录是webapps/mycontext/<br>             --&gt;<br></font><br>        &lt;Context path="" docBase="mycontext" debug="0"/&gt;<br>        <br><font color="green"><br>        &lt;!-- 另外一个Context，路径名是/wsota --&gt;<br></font><br>        &lt;Context path="/wsota" docBase="wsotaProject" debug="0"/&gt;<br>             <br>        <br>      &lt;/Host&gt;<br>      <br>    &lt;/Engine&gt;<br><br>  &lt;/Service&gt;<br><br>&lt;/Server&gt;<br><br><font color="green"><br>&lt;!-----------------------------------------------------------------------------------------------&gt;<br></font>
</pre>
</tt>
</p>
<h3>
4 - Context的部署配置文件web.xml的说明
</h3>
<p>
一个Context对应于一个Web App，每个Web App是由一个或者多个servlet组成的<br>
当一个Web App被初始化的时候，它将用自己的ClassLoader对象载入&#8220;部署配置文件web.xml&#8221;中定义的每个servlet类<br>
它首先载入在$CATALINA_HOME/conf/web.xml中部署的servlet类<br>
然后载入在自己的Web App根目录下的WEB-INF/web.xml中部署的servlet类<br>
web.xml文件有两部分：servlet类定义和servlet映射定义<br>
每个被载入的servlet类都有一个名字，且被填入该Context的映射表(mapping table)中，和某种URL PATTERN对应<br>
当该Context获得请求时，将查询mapping table，找到被请求的servlet，并执行以获得请求回应<br>
</p>
<p>
分析一下所有的Context共享的web.xml文件，在其中定义的servlet被所有的Web App载入<br>
</p>
<p>
</p>
<pre><tt><font color="green"><br>&lt;!-----------------------------------------------------------------------------------------------&gt;<br></font><br><br>&lt;web-app&gt;<br><br><font color="green"><br>  &lt;!-- 概述：<br>       该文件是所有的WEB APP共用的部署配置文件，<br>       每当一个WEB APP被DEPLOY，该文件都将先被处理，然后才是WEB APP自己的/WEB-INF/web.xml<br>       --&gt;<br></font><br><br><font color="green"><br>  &lt;!--  +-------------------------+  --&gt;<br>  &lt;!--  |    servlet类定义部分    |  --&gt;<br>  &lt;!--  +-------------------------+  --&gt;<br></font><br>  <br><font color="green"><br>  &lt;!-- DefaultServlet<br>       当用户的HTTP请求无法匹配任何一个servlet的时候，该servlet被执行<br>       URL PATTERN MAPPING : /<br>       --&gt;<br></font><br>    &lt;servlet&gt;<br>        &lt;servlet-name&gt;default&lt;/servlet-name&gt;<br>        &lt;servlet-class&gt;<br>          org.apache.catalina.servlets.DefaultServlet<br>        &lt;/servlet-class&gt;<br>        &lt;init-param&gt;<br>            &lt;param-name&gt;debug&lt;/param-name&gt;<br>            &lt;param-value&gt;0&lt;/param-value&gt;<br>        &lt;/init-param&gt;<br>        &lt;init-param&gt;<br>            &lt;param-name&gt;listings&lt;/param-name&gt;<br>            &lt;param-value&gt;true&lt;/param-value&gt;<br>        &lt;/init-param&gt;<br>        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;<br>    &lt;/servlet&gt;<br><br><font color="green"><br>  &lt;!-- InvokerServlet<br>       处理一个WEB APP中的匿名servlet<br>       当一个servlet被编写并编译放入/WEB-INF/classes/中，却没有在/WEB-INF/web.xml中定义的时候<br>       该servlet被调用，把匿名servlet映射成/servlet/ClassName的形式<br>       URL PATTERN MAPPING : /servlet/*<br>       --&gt;<br></font><br>    &lt;servlet&gt;<br>        &lt;servlet-name&gt;invoker&lt;/servlet-name&gt;<br>        &lt;servlet-class&gt;<br>          org.apache.catalina.servlets.InvokerServlet<br>        &lt;/servlet-class&gt;<br>        &lt;init-param&gt;<br>            &lt;param-name&gt;debug&lt;/param-name&gt;<br>            &lt;param-value&gt;0&lt;/param-value&gt;<br>        &lt;/init-param&gt;<br>        &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;<br>    &lt;/servlet&gt;<br><br><font color="green"><br>  &lt;!-- JspServlet<br>       当请求的是一个JSP页面的时候（*.jsp）该servlet被调用<br>       它是一个JSP编译器，将请求的JSP页面编译成为servlet再执行<br>       URL PATTERN MAPPING : *.jsp<br>       --&gt;<br></font><br>    &lt;servlet&gt;<br>        &lt;servlet-name&gt;jsp&lt;/servlet-name&gt;<br>        &lt;servlet-class&gt;org.apache.jasper.servlet.JspServlet&lt;/servlet-class&gt;<br>        &lt;init-param&gt;<br>            &lt;param-name&gt;logVerbosityLevel&lt;/param-name&gt;<br>            &lt;param-value&gt;WARNING&lt;/param-value&gt;<br>        &lt;/init-param&gt;<br>        &lt;load-on-startup&gt;3&lt;/load-on-startup&gt;<br>    &lt;/servlet&gt;<br><br><br><font color="green"><br>  &lt;!--  +---------------------------+  --&gt;<br>  &lt;!--  |    servlet映射定义部分    |  --&gt;<br>  &lt;!--  +---------------------------+  --&gt;<br></font><br>    <br>    &lt;servlet-mapping&gt;<br>        &lt;servlet-name&gt;default&lt;/servlet-name&gt;<br>        &lt;url-pattern&gt;/&lt;/url-pattern&gt;<br>    &lt;/servlet-mapping&gt;<br><br>    &lt;servlet-mapping&gt;<br>        &lt;servlet-name&gt;invoker&lt;/servlet-name&gt;<br>        &lt;url-pattern&gt;/servlet/*&lt;/url-pattern&gt;<br>    &lt;/servlet-mapping&gt;<br><br>    &lt;servlet-mapping&gt;<br>        &lt;servlet-name&gt;jsp&lt;/servlet-name&gt;<br>        &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;<br>    &lt;/servlet-mapping&gt;<br><br><font color="green"><br>  &lt;!--  +------------------------+  --&gt;<br>  &lt;!--  |    其它部分，略去先    |  --&gt;<br>  &lt;!--  +------------------------+  --&gt;<br></font><br>    ... ... ... ...<br><br>&lt;/web-app&gt;<br><br><font color="green"><br>&lt;!-----------------------------------------------------------------------------------------------&gt;<br></font>
</tt></pre>
<h3>
5 - Tomcat Server处理一个http请求的过程
</h3>
<p>
假设来自客户的请求为：<br>
http://localhost:8080/wsota/wsota_index.jsp<br>
</p>
1) 请求被发送到本机端口8080，被在那里侦听的Coyote HTTP/1.1 Connector获得<br>
2) Connector把该请求交给它所在的Service的Engine来处理，并等待来自Engine的回应<br>
3) Engine获得请求localhost/wsota/wsota_index.jsp，匹配它所拥有的所有虚拟主机Host<br>
4) Engine匹配到名为localhost的Host（即使匹配不到也把请求交给该Host处理，因为该Host被定义为该Engine的默认主机）<br>
5) localhost Host获得请求/wsota/wsota_index.jsp，匹配它所拥有的所有Context<br>
6) Host匹配到路径为/wsota的Context（如果匹配不到就把该请求交给路径名为""的Context去处理）<br>
7) path="/wsota"的Context获得请求/wsota_index.jsp，在它的mapping table中寻找对应的servlet<br>
8) Context匹配到URL PATTERN为*.jsp的servlet，对应于JspServlet类<br>
9) 构造HttpServletRequest对象和HttpServletResponse对象，作为参数调用JspServlet的doGet或doPost方法<br>
10)Context把执行完了之后的HttpServletResponse对象返回给Host<br>
11)Host把HttpServletResponse对象返回给Engine<br>
12)Engine把HttpServletResponse对象返回给Connector<br>
13)Connector把HttpServletResponse对象返回给客户browser<img src ="http://www.blogjava.net/honzeland/aggbug/132618.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/honzeland/" target="_blank">honzeland</a> 2007-07-26 19:13 <a href="http://www.blogjava.net/honzeland/articles/132618.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>web.xml Reference Guide for Tomcat</title><link>http://www.blogjava.net/honzeland/articles/132616.html</link><dc:creator>honzeland</dc:creator><author>honzeland</author><pubDate>Thu, 26 Jul 2007 11:11:00 GMT</pubDate><guid>http://www.blogjava.net/honzeland/articles/132616.html</guid><wfw:comment>http://www.blogjava.net/honzeland/comments/132616.html</wfw:comment><comments>http://www.blogjava.net/honzeland/articles/132616.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/honzeland/comments/commentRss/132616.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/honzeland/services/trackbacks/132616.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: ZZ: http://wiki.metawerx.net/Wiki.jsp?page=web.xmlIntroductionThe web.xml Deployment Descriptor file describes how to deploy a web application in a servlet container such as Tomcat.This file i...&nbsp;&nbsp;<a href='http://www.blogjava.net/honzeland/articles/132616.html'>阅读全文</a><img src ="http://www.blogjava.net/honzeland/aggbug/132616.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/honzeland/" target="_blank">honzeland</a> 2007-07-26 19:11 <a href="http://www.blogjava.net/honzeland/articles/132616.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How do I use DataSources with Tomcat?</title><link>http://www.blogjava.net/honzeland/articles/132570.html</link><dc:creator>honzeland</dc:creator><author>honzeland</author><pubDate>Thu, 26 Jul 2007 08:22:00 GMT</pubDate><guid>http://www.blogjava.net/honzeland/articles/132570.html</guid><wfw:comment>http://www.blogjava.net/honzeland/comments/132570.html</wfw:comment><comments>http://www.blogjava.net/honzeland/articles/132570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/honzeland/comments/commentRss/132570.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/honzeland/services/trackbacks/132570.html</trackback:ping><description><![CDATA[ZZ from: http://wiki.apache.org/tomcat/UsingDataSources<br><br>
<p>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. </p>
<p>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. </p>
<p>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: </p>
<h4 id="head-89abcb33986a520c0310bded49e81303611bbabd">Install the JDBC Driver</h4>
<p>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. </p>
<h4 id="head-170676815a596cd6cd78ef65944e3f728bd98ed0">Create META-INF/context.xml</h4>
<p>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: </p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br><br>&lt;Context&gt;<br><br>  &lt;Resource name="jdbc/WallyDB" auth="Container"<br>            type="javax.sql.DataSource" username="wally" password="wally"<br>            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" <br>            url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"<br>            maxActive="8" <br>            /&gt;<br><br>&lt;/Context&gt;<br><br></pre>
<p>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. </p>
<h4 id="head-448e07c1bb14df46da230370b280872f0ea43846">Access the DataSource in Your Application</h4>
<h5 id="head-5a35811e97b73a35cbb2a5337b629e8c2faa7168">From a Servlet</h5>
<p>Here's how you might access the data in a servlet: </p>
<pre>  InitialContext ic = new InitialContext();<br>  DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB");<br>  Connection c = ds.getConnection();<br>  ...<br>  c.close();<br></pre>
<p>Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with <em>java:comp/env/</em> </p>
<h4 id="head-f16b381f602940f23df01c213c8dd044b912b2bd">Sample Project</h4>
<p>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: <a href="http://wiki.apache.org/tomcat-data/attachments/UsingDataSources/attachments/datasourcedemo.war">datasourcedemo.war</a> </p>
<h4 id="head-9cfd473060cbd22febe52f149e8e6509c28f296a">Known-Working examples for other Databases</h4>
<pre>    &lt;Resource name="jdbc/denali" auth="Container" type="javax.sql.DataSource"<br>              username="denali" url="jdbc:postgresql://localhost:5432/demo"<br>              factory="org.apache.commons.dbcp.BasicDataSourceFactory"<br>              driverClassName="org.postgresql.Driver"<br>              maxActive="20" maxIdle="10"/&gt;<br></pre>
<pre>    &lt;Resource name="jdbc/ccsdatasource" auth="Container" type="javax.sql.DataSource"<br>              username="ccs" password="secret" url="jdbc:mysql://localhost:3306/ccs"<br>              driverClassName="com.mysql.jdbc.Driver"<br>              maxActive="20" maxIdle="10"/&gt;<br></pre>
<h4 id="head-abcb8f3c4cace820181ca04ce60d53a488bc2bff">Please Note</h4>
<p>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. </p>
<br><br> <img src ="http://www.blogjava.net/honzeland/aggbug/132570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/honzeland/" target="_blank">honzeland</a> 2007-07-26 16:22 <a href="http://www.blogjava.net/honzeland/articles/132570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>