【java】itoo項目實戰之java Web 中web的作用

來源:互聯網
上載者:User

標籤:

在ITOO中,每個war包下的web.xml都是一樣的.它的結構如下:


itoo中含有多個war包,而且每個war包中web.xml都和的結構一樣,而且配置內容都是一樣的.


       在Javaweb應用中可以包含HTML 文檔,Servlet,JSP和相關java類等,為了Servlet容器能夠順利地找到javaWeb應用中的各個組件,Servlet規範規定,javaWeb應用必須採取固定的目錄結構,即每種類型的組件在Web應用中都有固定的存放目錄.Servlet規範還規定,javaWeb應用的配置資訊存放在WEB-INF/web.xml檔案中,Servlet容器從該檔案中讀取配置資訊.


WEB應用的組成結構:

qrc工程名

|

|----index.html,jsp....【根目錄下的檔案外界可以直接存取】

|

|----WEB-INF目錄【該目錄下的檔案外界無法直接存取,有web伺服器負責調用】

|            |---classes目錄(java類)

|            |---lib目錄(java類運行所需要的jar)

|            |---web.xml檔案(web應用的設定檔)


 

每一個javaWeb工程都有一個web.xml設定檔,那麼他到底有什麼作用呢?它是每一個web工程都必的必須的嗎?

 

      web.xml檔案是用來初始化工程配置資訊的,比如說welcome頁面,filter,listener,servlet,servlet-mapping,啟動載入層級等等,web.xml檔案是javaWeb應用的XML格式的設定檔,存放於WEB-INF子目錄下.Web.xml檔案由開發人員編寫,供Servlet容器訪問.Web.xml檔案也稱為javaWeb應用發布描述檔案,Servlet容器在載入和啟動javaWeb應用時會讀取它的web.xml檔案,從中獲得關於當前web應用的發布資訊.當你的web工程中沒用到這些當然也就不需要這個xml檔案來配置你的apllication了

 

     每一個xml檔案都有定義他書寫規範的schema檔案,web.xml所對應的xml Schema檔案中定義了多少種標籤元素,web.xml中就可以出現它所定義的標籤元素,也就具備哪些特定的功能。web.xml的模式檔案是由Sun公司定義的,每個web.xml檔案的根項目為<web-app>中,必須標明這個web.xml使用的是哪個模式檔案。

所以,web.xml檔案是web應用中不可缺少的設定檔。他本身是一個XML檔案。遵守XML檔案的文法。在web.xml檔案中

可包含以下配置資訊:

.Servlet定義.

.servlet的初始化參數

.servlet及JSP的映射

.安全網域設定參數

.welcome檔案清單.

.資源引用

.環境變數的定義.

 

Servlet2.5版本的web.xml採用了XMLSchema來定義XML元素;

 

web.xml的元素說明:

 

<span style="font-family:FangSong_GB2312;font-size:18px;"><web-app> <description> <description>描述當前web應用的,這是一個描述性的資訊,可有可無</description> <display-name> <display-name>在web應用中顯示在web應用管理頁面的名字,可有可無</display-name> <distributable> <distributable/>  <!--Server Load Balancer,應用是分布的--> <filter> <filter> <filter-name>AuthenticationFilter</filter-name> <filter-class>webbook.guestbook.AuthenticationFilter</filter-class> <init-param> <param-name>url</param-name> <param-value>/admin/login.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-mapping> <filter-name>AuthenticationFilter</filter-name> <url-pattern>/admin/secure/*</url-pattern> <url-pattern>/admin/manage/*</url-pattren> </filter-mapping> <context-param> <context-param> <param-name>pageSize</param-name> <param-value>30</param-value> </context-param> <listener> <listener> <listener-class>webbook.user.OnlineUserListener</listener-class> </listener> <servlet> <servlet> <servlet-name>AddMessageServlet</servlet-name> <servlet-class>webbook.guestbook.AddMessageServlet<servlet-class> <init-param> <param-name>type</param-name> <param-value>text</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name>AddMessageServlet</servlet-name> <url-pattern>/servlet/AddMessage</url-pattern> </servlet-mapping> <session-config> <session-config> <session-timeout>60</session-timeout> </session-config> <mime-mapping> <mime-mapping> <extension>xls</extension> <mime-type>application/vnd.ms-excel</mime-type> </mime-mapping> <welcome-file-list> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>addMessage.jsp</welcome-file> </welcome-file-list> <error-page> <error-page> <error-code>404</error-code> <location>/error404.htm</location> </error-page> <error-page> <exception-type>java.io.IOException</exception-type> <location>/error500.jsp</location> </error-page></span>


 

--------------------------------------------------------------------------

 

web應用初始化

 

1、首先,把在web.xml檔案中<listener>元素所申明的那些監聽器進行執行個體化;

 

2、然後,調用那些實現了ServletContextListener介面的監聽器的contextInitialized()方法;

 

3、執行個體化<filter>元素中所申明的過濾器類,調用過濾器類的init()方法;

 

4、最後,如果<servlet>元素中使用了<load-on-startup>子項目,則按照數值有小到大依次對

 

servlet進行執行個體化,同時調用對應的servlet的init()方法;

 

說了這麼多,用以下兩張圖來解釋以下web.xml在javaweb中的作用:


 Web伺服器與Web應用這兩個不同的軟體系統協作,需要一個中介Servlet容器來處理.


Web.xml中配置實現servlet介面的實現.

 

     Web伺服器在載入Web應用時,就會把相應的web.xml檔案中的資料讀入到記憶體中,因此當web伺服器需要參考web.xml檔案時,實際上只需要從記憶體中讀取相關資料就酷刑了,無需再到檔案系統中讀取Web.xml檔案.

所以,web檔案可以提前讀取到記憶體中的.

【java】itoo項目實戰之java Web 中web的作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.