配置Servlet时使用<load-on-startup>的作用 !

参考:http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html

Servletspecification:
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.

根据该配置可知: com.lifecycle.ServletCycle 将先于 com.first.MyServlet 被加载

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.first.MyServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
      <servlet-name>ServletCycle</servlet-name>
      <servlet-class>com.lifecycle.ServletCycle</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>ServletCycle</servlet-name>
  <url-pattern>/ServletCycle</url-pattern>
  </servlet-mapping>
</web-app>

 

posted @ 2013-12-15 13:04  龍變  阅读(818)  评论(0编辑  收藏  举报