2011-10-18 29 views

回答

10

load-on-startup通知servlet容器在服務器啓動時加載指定的資源。 如果存在多個加載啓動標記,則您看到的數字將指示啓動順序。

<load-on-startup>1</load-on-startup> 
<load-on-startup>2</load-on-startup> 

將導致負載在啓動1上的資源首先被加載。這是爲了控制如果存在依賴性的加載順序。查看解釋加載順序的servlet規範。

我在下面我的評論中提到的答案(參考http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd):

<xsd:element name="load-on-startup" 
      type="javaee:load-on-startupType" 
      minOccurs="0"> 
    <xsd:annotation> 
     <xsd:documentation> 

     The load-on-startup element indicates that this 
     servlet should be loaded (instantiated and have 
     its init() called) on the startup of the web 
     application. The optional contents of these 
     element must be an integer indicating the order in 
     which the servlet should be loaded. If the value 
     is a negative integer, or the element is not 
     present, the container is free to load the servlet 
     whenever it chooses. If the value is a positive 
     integer or 0, the container must load and 
     initialize the servlet as the application is 
     deployed. The container must guarantee that 
     servlets marked with lower integers are loaded 
     before servlets marked with higher integers. The 
     container may choose the order of loading of 
     servlets with the same load-on-start-up value. 

     </xsd:documentation> 
    </xsd:annotation> 
    </xsd:element> 

請仔細閱讀文檔。

+0

如果我寫 1它會創建任何區別嗎?可以提供一些鏈接嗎? – amod

+0

@ amod0017因爲您正在查找詳細信息...在瀏覽器中點擊此鏈接「http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd」並搜索元素「「並閱讀文檔。沒有比servlet規範(docs和XSDs)和API更好的地方了:-)希望這有助於。 – Ayusman

+1

在我的回答中爲每個人的好處添加內容... – Ayusman

8

請參閱http://struts.apache.org/1.x/userGuide/configuration.html

load-on-startup意味着servlet必須在webapp啓動時(即,只要部署它,而不等待對servlet的請求)就加載並初始化。數字表示初始化的順序。如果另一個servlet有1,它將在之前加載。如果另一個有3個,它將在之後加載。

+0

所以,如果我寫<負載導通啓動> 1在web.xml中它不會產生任何區別?我猜這不會是嗎?在網絡中,我總是發現 1被寫入。 – amod

+1

如果servlet是web.xml中的唯一一個,它不會產生任何差異。如果其他servlet有1,那麼該命令是不確定的,AFAIK。 –

2

load-on-startup通知容器在應用程序啓動過程中加載servlet。 分配的編號是servlet的等級,它指示應該加載servlet的加載順序。

3

如果您使用的是Tomcat,也有已加載的每一個web應用一對夫婦的servlet:

  • 默認的servlet(通常提供靜態內容以及任何未映射網址作出反應)
  • jsp中的servlet

考慮看看默認的Tomcat的web.xml配置文件...

<servlet> 
    <servlet-name>default</servlet-name> 
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> 
    ... 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet> 
    <servlet-name>jsp</servlet-name> 
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
    ... 
    <load-on-startup>3</load-on-startup> 
</servlet> 

注意缺省值是1,而JSP是在3

因此,使用<load-on-startup>2</load-on-startup>意味着你的servlet將在部署時被加載,默認的servlet之後,但在JSP的servlet之前。

+0

這很有意義@ namero999 - 如果使用spring,這同樣適用嗎?我的意思是我仍然應該給web.xml中的servlet啓動時加載值2? – Nirmal

+0

我想是的。我的意思是,這個概念一般適用,不管春天,因此你應該是好的。 – namero999

+0

我正試圖找出關於此的權威性。它在這個前端的春天必須有所不同,因爲spring的上下文加載監聽器很早就開始了,我們沒有爲此指定任何東西。 – Nirmal

0

1)是在「web.xml」中使用的元素。

2)該元素指示web容器加載由此元素指示的服務器。

3)的順序是基於標籤 實施例內提供的號碼:1 1 -server首先執行,然後將其轉移到2 ..,

+0

重複已經給出的答案沒有意義。這是一個問答網站,而不是一個老式的討論論壇,在這個討論論壇中,每個人通過協議重複彼此成爲無法挽回的混亂。 – BalusC

相關問題