Tomcat加載順序
加載類和資源的順序為:
1、/Web-INF/classes
2、/Web-INF/lib/*.jar
3、Bootstrap
4、System
5、$CATALINA_HOME/common/classes
6、$CATALINA_HOME/common/endores/*.jar
7、$CATALINA_HOME/common/lib/*.jar
8、$CATALINA_HOME/shared/classes
9、$CATALINA_HOME/shared/lib/*.jar
兩個web配置文件
Servlet定義的時候,我發現在${catalina.home}/conf下以及${catalina.home}/webapps/ROOT/WEB_INF下都有web.xml這個文件。
web.xml的文件格式定義在Servlet規范中,因此所有符合Servlet規范的Java Servlet Container都會用到它。當tomcat部署應用程序時(在激活過程中,或加載應用程序后),它都會讀取通用的conf/web.xml,然后再讀取web應用程序中的WEB-INF/web.xml。其實根據他們的位置,我們就可以知道,conf/web.xml文件中的設定會應用於所有的web應用程序,而某些web應用程序的WEB-INF/web.xml中的設定只應用於該應用程序本身。
如果沒有WEB-INF/web.xml文件,tomcat會輸出找不到的消息,但仍然會部署並使用web應用程序,servlet規范的作者想要實現一種能迅速並簡易設定新范圍的方法,以用作測試,因此,這個web.xml並不是必要的,不過通常最好還是讓每一個上線的web應用程序都有一個自己的WEB-INF/web.xml。
系統web.xml詳解
<?xml version="1.0" encoding="GB2312"?> <!-- Web.xml依次定議了如下元素: <web-app> <display-name></display-name> 定義了WEB應用的名字 <description></description> 聲明WEB應用的描述信息 <filter></filter> <filter-mapping></filter-mapping> <servlet></servlet> <servlet-mapping></servlet-mapping> <session-config></session-config> <welcome-file-list></welcome-file-list> <taglib></taglib> <resource-ref></resource-ref> <security-constraint></security-constraint> <login-config></login-config> </web-app> 在web.xml中元素定義的先后順序不能顛倒,否則Tomcat服務器可能會拋出SAXParseException. --> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Sample Application</display-name> <description> This is a Sample Application </description> <!-- filter 配置Servlet過濾器 filter-name 定義過濾器的名字。當有多個過濾器時,不能同名 filter-class 指定實現這一過濾的類,這個類負責具體的過濾事務 --> <filter> <filter-name>SampleFilter</filter-name> <filter-class>mypack.SampleFilter</filter-class> </filter> <!-- filter-mapping 設定過濾器負責過濾的URL filter-name 過濾器名。這里的名字一定要和filter中的過濾器名匹配 url-pattern 指定過濾器負責過濾的URL --> <filter-mapping> <filter-name>SampleFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- servlet 配置Servlet. servlet-name 定義Servlet的名字 servlet-class 指定實現這個servlet的類 init-param 定義Servlet的初始化參數和參數值,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數 load-on-startup 指定當Web應用啟動時,裝載Servlet的次序。 當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet. 當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它 --> <servlet> <servlet-name>SampleServlet</servlet-name> <servlet-class>mypack.SampleServlet</servlet-class> <init-param> <param-name>initParam1</param-name> <param-value>2</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置servlet映射(下面代碼為SampleServlet指定的相對URL為"/sample": servlet-name 指定servlet的名字,這里的名字應該和<Servlet>元素中定義的名字匹配。 url-pattern 指定訪問這個servlet的URL。只需給出相對路徑。 --> <servlet-mapping> <servlet-name>SampleServlet</servlet-name> <url-pattern>/sample</url-pattern> </servlet-mapping> <!--配置session session用來設定HttpSession的生命周期。單位(秒)--> <session-config> <session-timeout>30</session-timeout> </session-config> <!--配置Wel0come0文件清單--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <!-- 配置Tag Library taglib-uri 設定Tag Library的唯一標識符,在Web應用中將根據這一標識符來引用Tag Library taglib-location 指定和Tag Library對應的TLD文件的位置 --> <taglib> <taglib-uri>/mytaglib</taglib-uri> <taglib-location>/WEB-INF/mytaglib.tld</taglib-location> </taglib> <!-- 配置資源引用 description 對所引用的資源的說明 res-ref-name 指定所引用資源的JNDI名字 res-type 指定所引用資源的類名字 res-auth 指定管理所引用資源的Manager,它有兩個可選值: Container:由容器來創建和管理resource Application:同WEB應用來創建和管理Resource --> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/sampleDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <!-- 配置安全約束(以下代碼指定當用戶訪問該WEB應用下的所有資源時,必須具備guest角色) web-resource-collection 聲明受保護的WEB資源 auth-constraint 聲明可以訪問受保護資源的角色,可以包含多個<role-name>子元素 web-resource-name 標識受保護的WEB資源 url-pattern 指定受保護的URL路徑 --> <Security-constraint> <web-resource-collection> <web-resource-name>sample appliction</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>guest</role-name> </auth-constraint> </Security-constraint> <!-- 配置安全驗證登錄界面:指定當WEB客戶訪問受保護的WEB資源時,系統彈出的登錄對話框的類型。 auth-method 指定驗證方法,它有三個可選值:BASIC(基本驗證)、DIGEST(摘要驗證)、FORM(表單驗證) realm-name 設定安全域的名稱 form-login-config 當驗證方法為FORM時,配置驗證網頁和出錯網頁 form-login-page 當驗證方法為FORM時,設定驗證網頁 form-error-page 當驗證方法為FORM時,設定出錯網頁 --> <login-config> <auth-method>FORM</auth-method> <realm-name> Tomcat Server Configuration form-Based Authentication Area </realm-name> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/error.jsp</form-error-page> </form-login-config> </login-config> <!--配置對安全驗證角色的引用--> <security-role> <description> The role that is required to log into the sample application </description> <role-name>guest</role-name> </security-role> </web-app>
五、web.xml配置簡介:
1、默認(歡迎)文件的設置
在tomcat4\conf\web.xml中,與IIS中的默認文件意思相同。
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
2、報錯文件的設置
<error-page> <error-code>404</error-code> <location>/notFileFound.jsp</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/null.jsp</location> </error-page>
如果某文件資源沒有找到,服務器要報404錯誤,按上述配置則會調用\webapps\ROOT\notFileFound.jsp。
如果執行的某個JSP文件產生NullPointException ,則會調用\webapps\ROOT\null.jsp
3、會話超時的設置
設置session 的過期時間,單位是分鍾;
<session-config> <session-timeout>30</session-timeout> </session-config>
4、過濾器的設置
<filter> <filter-name>FilterSource</filter-name> <filter-class>project4. FilterSource </filter-class> </filter> <filter-mapping> <filter-name>FilterSource</filter-name> <url-pattern>/WwwServlet</url-pattern> (<url-pattern>/haha/*</url-pattern>) </filter-mapping>
過濾:
1) 身份驗證的過濾Authentication Filters
2) 日志和審核的過濾Logging and Auditing Filters
3) 圖片轉化的過濾Image conversion Filters
4) 數據壓縮的過濾Data compression Filters
5) 加密過濾Encryption Filters
6) Tokenizing Filters
7) 資源訪問事件觸發的過濾Filters that trigger resource access events XSL/T 過濾XSL/T filters
8) 內容類型的過濾Mime-type chain Filter 注意監聽器的順序,如:先安全過濾,然后資源,然后內容類型等,這個順序可以自己定。(更加詳細的解釋,見參考文檔)
參考文檔
tomcat中加載順序
首先可以肯定的是,加載順序與它們在 web.xml 文件中的先后順序無關。即不會因為 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:listener -> filter -> servlet
同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。我們的 listener, filter 等在初始化時會用到這些上下文中的信息,那么 context-param 配置節是不是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,因此真正的加載順序為:context-param -> listener -> filter -> servlet
對於某類配置節而言,與它們出現的順序是有關的。以 filter 為例,web.xml 中當然可以定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這里一定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出現在 filter 之后,否則當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。web 容器啟動時初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的。
servlet 同 filter 類似 ,此處不再贅述。
由此,可以看出,web.xml 的加載順序是:context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。
web.xml文件詳解
Web.xml常用元素
<web-app> <display-name></display-name>定義了WEB應用的名字 <description></description> 聲明WEB應用的描述信息 <context-param></context-param> context-param元素聲明應用范圍內的初始化參數。 <filter></filter> 過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯。 <filter-mapping></filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。 <listener></listener>servlet API的版本2.3增加了對事件監聽程序的支持,事件監聽程序在建立、修改和刪除會話或servlet環境時得到通知.Listener元素指出事件監聽程序類。 <servlet></servlet> 在向servlet或JSP頁面制定初始化參數或定制URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。 <servlet-mapping></servlet-mapping> 服務器一般為servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。 但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。 <session-config></session-config> 如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節省內存。 可通過使用HttpSession的setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。 <mime-mapping></mime-mapping>如果Web應用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。 <welcome-file-list></welcome-file-list> 指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件。 <error-page></error-page> 在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。 <taglib></taglib> 對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面。 <resource-env-ref></resource-env-ref>聲明與資源相關的一個管理對象。 <resource-ref></resource-ref> 聲明一個資源工廠使用的外部資源。 <security-constraint></security-constraint> 制定應該保護的URL。它與login-config元素聯合使用 <login-config></login-config> 指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯合使用。 <security-role></security-role>給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更為容易。 <env-entry></env-entry>聲明Web應用的環境項。 <ejb-ref></ejb-ref>聲明一個EJB的主目錄的引用。 < ejb-local-ref></ ejb-local-ref>聲明一個EJB的本地主目錄的應用。 </web-app>
相應元素配置
1、Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標
<icon> <small-icon>/images/app_small.gif</small-icon> <large-icon>/images/app_large.gif</large-icon> </icon>
2、Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱
<display-name>Tomcat Example</display-name>
3、Web 應用描述: 給出於此相關的說明性文本
<disciption>Tomcat Example servlets and JSP pages.</disciption>
4、上下文參數:聲明應用范圍內的初始化參數。
<context-param> <param-name>ContextParameter</para-name> <param-value>test</param-value> <description>It is a test parameter.</description> </context-param>
在servlet里面可以通過getServletContext().getInitParameter(“context/param”)得到
5、過濾器配置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯。
<filter> <filter-name>setCharacterEncoding</filter-name> <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6、監聽器配置
<listener> <listerner-class>listener.SessionListener</listener-class> </listener>
7、Servlet配置
基本配置
<servlet> <servlet-name>snoop</servlet-name> <servlet-class>SnoopServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>snoop</servlet-name> <url-pattern>/snoop</url-pattern> </servlet-mapping>
高級配置
<servlet> <servlet-name>snoop</servlet-name> <servlet-class>SnoopServlet</servlet-class> <init-param> <param-name>foo</param-name> <param-value>bar</param-value> </init-param> <run-as> <description>Security role for anonymous access</description> <role-name>tomcat</role-name> </run-as> </servlet> <servlet-mapping> <servlet-name>snoop</servlet-name> <url-pattern>/snoop</url-pattern> </servlet-mapping>
元素說明
<servlet></servlet> 用來聲明一個servlet的數據,主要有以下子元素: <servlet-name></servlet-name> 指定servlet的名稱 <servlet-class></servlet-class> 指定servlet的類名稱 <jsp-file></jsp-file> 指定web站台中的某個JSP網頁的完整路徑 <init-param></init-param> 用來定義參數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數 <load-on-startup></load-on-startup>指定當Web應用啟動時,裝載Servlet的次序。 當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet. 當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它 <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素 <servlet-name></servlet-name> 指定servlet的名稱 <url-pattern></url-pattern> 指定servlet所對應的URL
8、會話超時配置(單位為分鍾)
<session-config> <session-timeout>120</session-timeout> </session-config>
9、MIME類型配置
<mime-mapping> <extension>htm</extension> <mime-type>text/html</mime-type> </mime-mapping>
10、指定歡迎文件頁配置
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list>
11、配置錯誤頁面
一、 通過錯誤碼來配置error-page
<error-page> <error-code>404</error-code> <location>/NotFound.jsp</location> </error-page>
上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。
二、通過異常的類型配置error-page
<error-page> <exception-type>java.lang.NullException</exception-type> <location>/error.jsp</location> </error-page>
如果MyEclipse一直在報錯,應該把 放到 中
<jsp-config> <taglib> <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location> </taglib> </jsp-config>
13、資源管理對象配置
<resource-env-ref> <resource-env-ref-name>jms/StockQueue</resource-env-ref-name> </resource-env-ref>
14、資源工廠配置
<resource-ref> <res-ref-name>mail/Session</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> </resource-ref>
配置數據庫連接池就可在此配置:
<resource-ref> <description>JNDI JDBC DataSource of shop</description> <res-ref-name>jdbc/sample_db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
15、安全限制配置
<security-constraint> <display-name>Example Security Constraint</display-name> <web-resource-collection> <web-resource-name>Protected Area</web-resource-name> <url-pattern>/jsp/security/protected/*</url-pattern> <http-method>DELETE</http-method> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> </web-resource-collection> <auth-constraint> <role-name>tomcat</role-name> <role-name>role1</role-name> </auth-constraint> </security-constraint>
16、登陸驗證配置
<login-config> <auth-method>FORM</auth-method> <realm-name>Example-Based Authentiation Area</realm-name> <form-login-config> <form-login-page>/jsp/security/protected/login.jsp</form-login-page> <form-error-page>/jsp/security/protected/error.jsp</form-error-page> </form-login-config> </login-config>
17、安全角色:security-role元素給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級IDE處理安全信息更為容易。
<security-role> <role-name>tomcat</role-name> </security-role>
18、Web環境參數:env-entry元素聲明Web應用的環境項
<env-entry> <env-entry-name>minExemptions</env-entry-name> <env-entry-value>1</env-entry-value> <env-entry-type>java.lang.Integer</env-entry-type> </env-entry>
19、EJB 聲明
<ejb-ref> <description>Example EJB reference</decription> <ejb-ref-name>ejb/Account</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.mycompany.mypackage.AccountHome</home> <remote>com.mycompany.mypackage.Account</remote> </ejb-ref>
20、本地EJB聲明
<ejb-local-ref> <description>Example Loacal EJB reference</decription> <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home> <local>com.mycompany.mypackage.ProcessOrder</local> </ejb-local-ref>
21、配置DWR
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
22、配置Struts
<display-name>Struts Blank Application</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>struts-bean</taglib-uri> <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-html</taglib-uri> <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-nested</taglib-uri> <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-logic</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-tiles</taglib-uri> <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> </taglib>
23、配置Spring(基本上都是在Struts中配置的)
<!-- 指定spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> <!--加載多個spring配置文件 --> /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml </param-value> </context-param> <!-- 定義SPRING監聽器,加載spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>