1. 程式人生 > 實用技巧 >Tomcat下的4個xml配置檔案的作用

Tomcat下的4個xml配置檔案的作用

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

context.xml檔案的作用:

Context.xml是Tomcat公用的環境配置。
tomcat伺服器會定時去掃描這個檔案。一旦發現檔案被修改(時間戳改變了),就會自動重新載入這個檔案,而不需要重啟伺服器。
context.xml的三個作用範圍:
1. tomcat server級別:
在/conf/context.xml裡配置
2. Host級別:
在/conf/Catalina/${hostName}裡新增context.xml,繼而進行配置
3. web app 級別:
在/conf/Catalina/${hostName}裡新增${webAppName}.xml,繼而進行配置

Web.xml檔案的作用:

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.

<!--
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>

Server.xml檔案的作用:

server.xml是對tomcat的設定,可以設定埠號,新增虛擬機器這些的,是對伺服器的設定。

主要是部署工程的,例如:<Context path="/project" docBase="E:\work\abc\WebRoot" debug="0" reloadable="true" crossContext="true" />。
project是你的工程名稱,docBase的地址就是你的工程所在位置。

tomcat-users.xml檔案的作用:

1.關於使用者角色、管理員的資訊都在這個配置檔案中。
2.登入使用者預設是註釋掉的,把 <!-- -->去掉才能生效。
3.在配置檔案<tomcat-users>節點下新增管理員配置:
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>

轉載於:https://my.oschina.net/feanlau/blog/1506843