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鏄繖鏍蜂竴涓泦鍚堬細瀹冪敱涓涓垨鑰呭涓狢onnector緇勬垚錛屼互鍙婁竴涓狤ngine錛岃礋璐e鐞嗘墍鏈塁onnector鎵鑾峰緱鐨勫鎴瘋姹?br>
1.3 - Connector
涓涓狢onnector灝嗗湪鏌愪釜鎸囧畾绔彛涓婁睛鍚鎴瘋姹傦紝騫跺皢鑾峰緱鐨勮姹備氦緇橢ngine鏉ュ鐞嗭紝浠嶦ngine澶勮幏寰楀洖搴斿茍榪斿洖瀹㈡埛
TOMCAT鏈変袱涓吀鍨嬬殑Connector錛屼竴涓洿鎺ヤ睛鍚潵鑷猙rowser鐨刪ttp璇鋒眰錛屼竴涓睛鍚潵鑷叾瀹僕ebServer鐨勮姹?br>
Coyote Http/1.1 Connector 鍦ㄧ鍙?080澶勪睛鍚潵鑷鎴穊rowser鐨刪ttp璇鋒眰
Coyote JK2 Connector 鍦ㄧ鍙?009澶勪睛鍚潵鑷叾瀹僕ebServer(Apache)鐨剆ervlet/jsp浠g悊璇鋒眰
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涓嬪彲浠ラ厤緗涓櫄鎷熶富鏈篤irtual Host錛屾瘡涓櫄鎷熶富鏈洪兘鏈変竴涓煙鍚?br>
褰揈ngine鑾峰緱涓涓姹傛椂錛屽畠鎶婅璇鋒眰鍖歸厤鍒版煇涓狧ost涓婏紝鐒跺悗鎶婅璇鋒眰浜ょ粰璇ost鏉ュ鐞?br>
Engine鏈変竴涓粯璁よ櫄鎷熶富鏈猴紝褰撹姹傛棤娉曞尮閰嶅埌浠諱綍涓涓狧ost涓婄殑鏃跺欙紝灝嗕氦緇欒榛樿Host鏉ュ鐞?br>
1.5 - Host
浠h〃涓涓猇irtual Host錛岃櫄鎷熶富鏈猴紝姣忎釜铏氭嫙涓繪満鍜屾煇涓綉緇滃煙鍚岲omain Name鐩稿尮閰?br>
姣忎釜铏氭嫙涓繪満涓嬮兘鍙互閮ㄧ講(deploy)涓涓垨鑰呭涓猈eb App錛屾瘡涓猈eb App瀵瑰簲浜庝竴涓狢ontext錛屾湁涓涓狢ontext path
褰揌ost鑾峰緱涓涓姹傛椂錛屽皢鎶婅璇鋒眰鍖歸厤鍒版煇涓狢ontext涓婏紝鐒跺悗鎶婅璇鋒眰浜ょ粰璇ontext鏉ュ鐞?br>
鍖歸厤鐨勬柟娉曟槸“鏈闀垮尮閰?#8221;錛屾墍浠ヤ竴涓猵ath==""鐨凜ontext灝嗘垚涓鴻Host鐨勯粯璁ontext
鎵鏈夋棤娉曞拰鍏跺畠Context鐨勮礬寰勫悕鍖歸厤鐨勮姹傞兘灝嗘渶緇堝拰璇ラ粯璁ontext鍖歸厤
1.6 - Context
涓涓狢ontext瀵瑰簲浜庝竴涓猈eb Application錛屼竴涓猈eb Application鐢變竴涓垨鑰呭涓猄ervlet緇勬垚
Context鍦ㄥ垱寤虹殑鏃跺欏皢鏍規嵁閰嶇疆鏂囦歡$CATALINA_HOME/conf/web.xml鍜?WEBAPP_HOME/WEB-INF/web.xml杞藉叆Servlet綾?br>
褰揅ontext鑾峰緱璇鋒眰鏃訛紝灝嗗湪鑷繁鐨勬槧灝勮〃(mapping table)涓鎵劇浉鍖歸厤鐨凷ervlet綾?br>
濡傛灉鎵懼埌錛屽垯鎵ц璇ョ被錛岃幏寰楄姹傜殑鍥炲簲錛屽茍榪斿洖
璇ユ枃浠舵弿榪頒簡濡備綍鍚姩Tomcat Server
<!----------------------------------------------------------------------------------------------->
<!-- 鍚姩Server
鍦ㄧ鍙?005澶勭瓑寰呭叧闂懡浠?br> 濡傛灉鎺ュ彈鍒?SHUTDOWN"瀛楃涓插垯鍏抽棴鏈嶅姟鍣?br> -->
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<!-- Listener ???
鐩墠娌℃湁鐪嬪埌榪欓噷
-->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
<!-- Global JNDI resources ???
鐩墠娌℃湁鐪嬪埌榪欓噷錛屽厛鐣ュ幓
-->
<GlobalNamingResources>
... ... ... ...
</GlobalNamingResources>
<!-- Tomcat鐨凷tandalone Service
Service鏄竴緇凜onnector鐨勯泦鍚?br> 瀹冧滑鍏辯敤涓涓狤ngine鏉ュ鐞嗘墍鏈塁onnector鏀跺埌鐨勮姹?br> -->
<Service name="Tomcat-Standalone">
<!-- Coyote HTTP/1.1 Connector
className : 璇onnector鐨勫疄鐜扮被鏄痮rg.apache.coyote.tomcat4.CoyoteConnector
port : 鍦ㄧ鍙e彿8080澶勪睛鍚潵鑷鎴穊rowser鐨凥TTP1.1璇鋒眰
minProcessors : 璇onnector鍏堝垱寤?涓嚎紼嬬瓑寰呭鎴瘋姹傦紝姣忎釜璇鋒眰鐢變竴涓嚎紼嬭礋璐?br> maxProcessors : 褰撶幇鏈夌殑綰跨▼涓嶅鏈嶅姟瀹㈡埛璇鋒眰鏃訛紝鑻ョ嚎紼嬫繪暟涓嶈凍75涓紝鍒欏垱寤烘柊綰跨▼鏉ュ鐞嗚姹?br> acceptCount : 褰撶幇鏈夌嚎紼嬪凡緇忚揪鍒版渶澶ф暟75鏃訛紝涓哄鎴瘋姹傛帓闃?br> 褰撻槦鍒椾腑璇鋒眰鏁拌秴榪?00鏃訛紝鍚庢潵鐨勮姹傝繑鍥濩onnection refused閿欒
redirectport : 褰撳鎴瘋姹傛槸https鏃訛紝鎶婅璇鋒眰杞彂鍒扮鍙?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鏀跺埌鐨凥ttp璇鋒眰
瀹冨皢鍖歸厤璇鋒眰鍜岃嚜宸辯殑铏氭嫙涓繪満錛屽茍鎶婅姹傝漿浜ょ粰瀵瑰簲鐨凥ost鏉ュ鐞?br> 榛樿铏氭嫙涓繪満鏄痩ocalhost
-->
<Engine name="Standalone" defaultHost="localhost" debug="0">
<!-- 鏃ュ織綾伙紝鐩墠娌℃湁鐪嬪埌錛岀暐鍘誨厛 -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- Realm錛岀洰鍓嶆病鏈夌湅鍒幫紝鐣ュ幓鍏?-->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" .../>
<!-- 铏氭嫙涓繪満localhost
appBase : 璇ヨ櫄鎷熶富鏈虹殑鏍圭洰褰曟槸webapps/
瀹冨皢鍖歸厤璇鋒眰鍜岃嚜宸辯殑Context鐨勮礬寰勶紝騫舵妸璇鋒眰杞氦緇欏搴旂殑Context鏉ュ鐞?br> -->
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- 鏃ュ織綾伙紝鐩墠娌℃湁鐪嬪埌錛岀暐鍘誨厛 -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- Context錛屽搴斾簬涓涓猈eb App
path : 璇ontext鐨勮礬寰勫悕鏄?"錛屾晠璇ontext鏄Host鐨勯粯璁ontext
docBase : 璇ontext鐨勬牴鐩綍鏄痺ebapps/mycontext/
-->
<Context path="" docBase="mycontext" debug="0"/>
<!-- 鍙﹀涓涓狢ontext錛岃礬寰勫悕鏄?wsota -->
<Context path="/wsota" docBase="wsotaProject" debug="0"/>
</Host>
</Engine>
</Service>
</Server>
<!----------------------------------------------------------------------------------------------->
涓涓狢ontext瀵瑰簲浜庝竴涓猈eb App錛屾瘡涓猈eb App鏄敱涓涓垨鑰呭涓猻ervlet緇勬垚鐨?br>
褰撲竴涓猈eb App琚垵濮嬪寲鐨勬椂鍊欙紝瀹冨皢鐢ㄨ嚜宸辯殑ClassLoader瀵硅薄杞藉叆“閮ㄧ講閰嶇疆鏂囦歡web.xml”涓畾涔夌殑姣忎釜servlet綾?br>
瀹冮鍏堣澆鍏ュ湪$CATALINA_HOME/conf/web.xml涓儴緗茬殑servlet綾?br>
鐒跺悗杞藉叆鍦ㄨ嚜宸辯殑Web App鏍圭洰褰曚笅鐨刉EB-INF/web.xml涓儴緗茬殑servlet綾?br>
web.xml鏂囦歡鏈変袱閮ㄥ垎錛歴ervlet綾誨畾涔夊拰servlet鏄犲皠瀹氫箟
姣忎釜琚澆鍏ョ殑servlet綾婚兘鏈変竴涓悕瀛楋紝涓旇濉叆璇ontext鐨勬槧灝勮〃(mapping table)涓紝鍜屾煇縐峌RL PATTERN瀵瑰簲
褰撹Context鑾峰緱璇鋒眰鏃訛紝灝嗘煡璇apping table錛屾壘鍒拌璇鋒眰鐨剆ervlet錛屽茍鎵ц浠ヨ幏寰楄姹傚洖搴?br>
鍒嗘瀽涓涓嬫墍鏈夌殑Context鍏變韓鐨剋eb.xml鏂囦歡錛屽湪鍏朵腑瀹氫箟鐨剆ervlet琚墍鏈夌殑Web App杞藉叆
<!----------------------------------------------------------------------------------------------->
<web-app>
<!-- 姒傝堪錛?br> 璇ユ枃浠舵槸鎵鏈夌殑WEB APP鍏辯敤鐨勯儴緗查厤緗枃浠訛紝
姣忓綋涓涓猈EB APP琚獶EPLOY錛岃鏂囦歡閮藉皢鍏堣澶勭悊錛岀劧鍚庢墠鏄疻EB APP鑷繁鐨?WEB-INF/web.xml
-->
<!-- +-------------------------+ -->
<!-- | servlet綾誨畾涔夐儴鍒? | -->
<!-- +-------------------------+ -->
<!-- DefaultServlet
褰撶敤鎴風殑HTTP璇鋒眰鏃犳硶鍖歸厤浠諱綍涓涓猻ervlet鐨勬椂鍊欙紝璇ervlet琚墽琛?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
澶勭悊涓涓猈EB APP涓殑鍖垮悕servlet
褰撲竴涓猻ervlet琚紪鍐欏茍緙栬瘧鏀懼叆/WEB-INF/classes/涓紝鍗存病鏈夊湪/WEB-INF/web.xml涓畾涔夌殑鏃跺?br> 璇ervlet琚皟鐢紝鎶婂尶鍚峴ervlet鏄犲皠鎴?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
褰撹姹傜殑鏄竴涓狫SP欏甸潰鐨勬椂鍊欙紙*.jsp錛夎servlet琚皟鐢?br> 瀹冩槸涓涓狫SP緙栬瘧鍣紝灝嗚姹傜殑JSP欏甸潰緙栬瘧鎴愪負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>
<!-- +------------------------+ -->
<!-- | 鍏跺畠閮ㄥ垎錛岀暐鍘誨厛 | -->
<!-- +------------------------+ -->
... ... ... ...
</web-app>
<!----------------------------------------------------------------------------------------------->
鍋囪鏉ヨ嚜瀹㈡埛鐨勮姹備負錛?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.