小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

tomcat web.xml配置

 vavava 2010-07-16


web.xml元素介紹

我將自己知道的web.xml的元素整理了一下: 

web.xml首先是肯定要包含它的schema. 

<web-app xmlns="http://java./xml/ns/j2ee"
    xmlns:xsi="http://www./2001/XMLSchema-instance"
    xsi:schemaLocation="http://java./xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

</web-app>

其它的元素都放在<web-app></web-app>之中。

<discription></discription> 是對(duì)站臺(tái)的描述
<display-name></display-name> 定義站臺(tái)的名稱(chēng)
<distributable/> 是指定該站臺(tái)是否可分布式處理


<context-param></context-param> 用來(lái)設(shè)定web站臺(tái)的環(huán)境參數(shù),它包含兩個(gè)子元素:
    <param-name></param-name> 用來(lái)指定參數(shù)的名稱(chēng)
    <param-value></param-value> 用來(lái)設(shè)定參數(shù)值

比如:
<context-param>
    <param-name>my_param</param-name>
    <param-value>hello</param-value>
</context-param>

在此設(shè)定的參數(shù),可以在servlet中用 getServletContext().getInitParameter("my_param") 來(lái)取得


<filter></filter> 是用來(lái)聲明filter的相關(guān)設(shè)定,它包含以下子元素:
    <filter-name></filter-name> 這當(dāng)然就是指定filter的名字
    <filter-class></filter-class> 這是用來(lái)定義filter的類(lèi)的名稱(chēng)
    <init-param></init-param> 用來(lái)定義參數(shù),它有兩個(gè)子元素:
        <param-name></param-name> 用來(lái)指定參數(shù)的名稱(chēng)
        <param-value></param-value> 用來(lái)設(shè)定參數(shù)值

比如:
<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></filter>同時(shí)使用的是<filter-mapping></filter-mapping> 用來(lái)定義filter所對(duì)應(yīng)的URL,它有兩個(gè)子元素:
    <filter-name></filter-name> 指定filter的名字
    <url-pattern></url-pattern> 指定filter所對(duì)應(yīng)的URL

比如:
<filter-mapping>
        <filter-name>setCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>


<listener></listener> 用來(lái)設(shè)定Listener接口,它的主要子元素為
    <listener-class></listener-class> 定義Listener的類(lèi)名稱(chēng)

比如:
<listener>
    
<listener-class>com.myTest.ContextListener</listener-class> 

</listener>


<servlet></servlet> 用來(lái)聲明一個(gè)servlet的數(shù)據(jù),主要有以下子元素:
    <servlet-name>
</servlet-name> 指定servlet的名稱(chēng)
    <servlet-class></servlet-class> 指定servlet的類(lèi)名稱(chēng)
    <jsp-file></jsp-file> 指定web站臺(tái)中的某個(gè)JSP網(wǎng)頁(yè)的完整路徑
    <init-param></init-param> 用來(lái)定義參數(shù),和前面的<init-param>差不多

同樣,與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用來(lái)定義servlet所對(duì)應(yīng)的URL,包含兩個(gè)子元素:
    
<servlet-name></servlet-name> 指定servlet的名稱(chēng)
    <url-pattern></url-pattern> 指定servlet所對(duì)應(yīng)的URL

比如:
<servlet>
        <servlet-name>ShoppingServlet</servlet-name>
        <servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
    
<servlet-mapping>
        <servlet-name>ShoppingServlet</servlet-name>
        <url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>


<session-config></session-config> 用來(lái)定義web站臺(tái)中的session參數(shù),包含一個(gè)子元素:
    <session-timeout></session-timeout> 用來(lái)定義這個(gè)web站臺(tái)所有session的有效期限,單位為                                                              分鐘


<mime-mapping></mime-mapping> 定義某一個(gè)擴(kuò)展名和某一個(gè)MIME Type做對(duì)映,包含兩個(gè)                                                        子元素:
    <extension></extension> 擴(kuò)展名的名稱(chēng)
    <mime-type></mime-type> MIME格式

比如:
<mime-mapping>
    <extension>doc</extension>
    <mime-type>application/vnd.ms-word</mime-type>

</mime-mapping> 
<mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>

</mime-mapping> 


<welcome-file-list></welcom-file-list> 用來(lái)定義首頁(yè)的列單,包含一個(gè)子元素:
    <welcome-file></welcome-file> 指定首頁(yè)的文件名稱(chēng)

比如:
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>

</welcom-file-list>


<error-page></error-page> 用來(lái)處理錯(cuò)誤代碼或異常的頁(yè)面,有三個(gè)子元素:
    <error-code></error-code> 指定錯(cuò)誤代碼
    <exception-type></exception-type> 指定一個(gè)JAVA異常類(lèi)型
    <location></location> 指定在web站臺(tái)內(nèi)的相關(guān)資源路徑

比如:
<error-page>
    <error-code>404</error-code>
    <location>/error404.jsp</location>

</error-page>
<error-page>
    <exception-type>java.lang.Exception</exception-type>

    <location>/exception.jsp</location>
</error-page>


<taglib></taglib> 用來(lái)設(shè)定JSP網(wǎng)頁(yè)所用到的Tag Library路徑,有兩個(gè)子元素:
    <taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網(wǎng)頁(yè)中用taglib指令便可取得該URI的                                              TLD文件
    <taglib-location></taglib-location> 指定TLD文件相對(duì)于web站臺(tái)的存放位置

比如:
<taglib>
      <taglib-uri>myTaglib</taglib-uri>
      <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>


<resource-ref></resource-ref> 定義利用JNDI取得站臺(tái)可利用的資源,有五個(gè)子元素:
    <description></description> 資源說(shuō)明
    <rec-ref-name></rec-ref-name> 資源名稱(chēng)
    <res-type></res-type> 資源種類(lèi)
    <res-auth></res-auth> 資源經(jīng)由Application或Container來(lái)許可
    <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個(gè)                                                                     值,默認(rèn)為
Shareable

比如,配置數(shù)據(jù)庫(kù)連接池就可在此配置:
<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>


<jsp-config> 包括<taglib> 和<jsp-property-group> 兩個(gè)子元素。
    
    其中<taglib>元素在JSP 1.2時(shí)就已經(jīng)存在;而<jsp-property-group>是JSP 2.0 新增的元素。
    <jsp-property-group>元素主要有八個(gè)子元素,它們分別為:
    
    1.<description>:設(shè)定的說(shuō)明;
    2.<display-name>:設(shè)定名稱(chēng);
    3.<url-pattern>:設(shè)定值所影響的范圍,如:/CH2 或 /*.jsp;
    4.<el-ignored>:若為true,表示不支持EL 語(yǔ)法;
    5.<scripting-invalid>:若為true,表示不支持<% scripting %>語(yǔ)法;
    6.<page-encoding>:設(shè)定JSP 網(wǎng)頁(yè)的編碼;
    7.<include-prelude>:設(shè)置JSP 網(wǎng)頁(yè)的抬頭,擴(kuò)展名為.jspf;
    8.<include-coda>:設(shè)置JSP 網(wǎng)頁(yè)的結(jié)尾,擴(kuò)展名為.jspf。


一個(gè)簡(jiǎn)單的<jsp-config>元素完整配置:

<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group>
</jsp-config>



配置web.xml來(lái)限制對(duì)某些servlet的請(qǐng)求

有時(shí)我們只希望通過(guò)認(rèn)證的用戶(hù)才能請(qǐng)求某些servlet的話,就可以在web.xml中來(lái)進(jìn)行相應(yīng)的配置,來(lái)達(dá)到此目的。

這就要用到<security-constraint></security-constraint>元素。
對(duì)于tomcat,中web.xml使用security-constraint元素需要在位于<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中創(chuàng)建用戶(hù)名和密碼。比如下面的這個(gè)tomcat-users.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,manager"/>
  <user username="admin" password="admin" roles="admin"/>
</tomcat-users>

此XML片段包括一個(gè)tomcat-users根元素,它包含一個(gè)或多個(gè)role和user元素。


然后在Web應(yīng)用程序的web.xml中創(chuàng)建security-constraint、login-config和security-role元素。

<security-constraint>
      <web-resource-collection>
          <web-resource-name>HelloServlet</web-resource-name>
          <url-pattern>/HelloServlet</url-pattern>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
      </web-resource-collection>
      <auth-constraint>
          <description>This applies only to the "tomcat" security role</description>
          <role-name>admin</role-name>
      </auth-constraint>
      <user-data-constraint>
          <transport-guarantee>NONE</transport-guarantee>
      </user-data-constraint>
  </security-constraint>
  
  <login-config>
      <auth-method>BASIC</auth-method>
  </login-config>
  <security-role>
      <role-name>admin</role-name>
  </security-role>

其中security-constraint元素包含一個(gè)或多個(gè)web-resource-collection元素,它是描述Web應(yīng)用程序中的哪些web資源受到指定安全限制的保護(hù)。http-method元素指定安全限制覆蓋的HTTP方法。上面的例子中,當(dāng)我們對(duì)/HelloServlet的GET或POST請(qǐng)求時(shí)將觸發(fā)配置的安全機(jī)制。
auth-constraint元素用于描述允許訪問(wèn)Web組件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有當(dāng)作為admin角色的用戶(hù)才可以訪問(wèn)HelloServlet。

Web應(yīng)用程序通過(guò)login-config元素來(lái)認(rèn)證用戶(hù),并確認(rèn)該用戶(hù)是否為正確的角色。
longin-config包含的transport-guarantee子元素用來(lái)指定認(rèn)證方法,BASIC是一種常見(jiàn)的Web認(rèn)證方式,瀏覽器給用戶(hù)提示一個(gè)對(duì)話框,要求輸入用戶(hù)名和密碼,隨后Tomcat將給出的用戶(hù)名和密碼與tomcat-users.xml中的用戶(hù)名和密碼進(jìn)行比較,然后使用前面的security-constraint
配置來(lái)確定用戶(hù)是否可訪問(wèn)受保護(hù)的servlet。

(除BASIC外,還可以是FORM、CLIENT-CERT、DIGEST等)

其實(shí)這種認(rèn)證方法實(shí)際上有兩個(gè)步驟:
1、檢查提供的用戶(hù)名和密碼是否正確。
2、判斷用戶(hù)是否映射到特定的安全角色。例如,用戶(hù)可能提供了正確的用戶(hù)名和密碼,但沒(méi)有映射到特定的安全角色,也將被禁止訪問(wèn)特定的Web資源。



通過(guò)此次整理,對(duì)web.xml中元素有了更清楚的了解,以后繼續(xù)努力...

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多