How To Use Welcome File List And Load On Startup In Web.xml

There are a lot of XML elements in web.xml in a web application project. And the most important two we will introduce are welcome-file-list and load-on-startup. The welcome-file-list XML element is used to display the default web page to a user when the user does not point out which page he wants to browse in URL. The load-on-startup XML element is used to set servlet startup order and priority when servlet container startup.

1. The welcome-file-list XML Element.

  1. When a user browses a URL like http://localhost:8080/Dev2qaWebAppExample/, he does not tell the web server which page to show. He only tells the web server he wants to browse web application Dev2qaWebAppExample.
  2. So which page will the web server send back to the client? This is decided by the welcome-file-list XML element in the web application web.xml file. Below is a part of web.xml
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
  3. You can see that there has a list of welcome-file XML elements in welcome-file-list XML elements. When you browse URL http://localhost:8080/Dev2qaWebAppExample/ again. The web server will find the below URL in the order defined in web.xml.
    http://localhost:8080/Dev2qaWebAppExample/index.html
    
    http://localhost:8080/Dev2qaWebAppExample/index.htm
    
    http://localhost:8080/Dev2qaWebAppExample/index.jsp
    
    http://localhost:8080/Dev2qaWebAppExample/default.html
    
    http://localhost:8080/Dev2qaWebAppExample/default.htm
    
    http://localhost:8080/Dev2qaWebAppExample/default.jsp
  4. When one of the above web pages is found, the client will be redirected to it. Then client user will see the content of that webpage. Other pages except the found one in the above list will be ignored by the web server.
  5. If you want to invoke a java servlet when the client requests the web app, you can set the servlet-mapping URL in the first welcome-file XML element value.
    <welcome-file-list>
      <welcome-file>LoadFirst</welcome-file>
    </welcome-file-list>
    

2. The load-on-startup XML Element.

  1. We all know that the servlet will be loaded and startup when the servlet container receives the first request to the servlet, this will cost more time for the first request to the servlet.
  2. But when you add the load-on-startup XML element for servlet definition in web.xml, there will have the following benefits when it is startup.

2.1 Benefits for use load-on-startup XML element.

  1. Servlet will be loaded when the container startup. This will make the response time shorter for the first request to the servlet.
  2. If you have multiple servlets defined in web.xml, and they should startup in the correct order, then you can use load-on-startup value to fix the startup order of those servlets.

2.2 The load-on-startup XML element value.

  1. The load-on-startup XML element’s value can be positive or negative.
  2. A negative value means the servlet will be loaded when it is first requested, it will not be loaded when the servlet container startup.
  3. A positive value defines the order number that servlet will be loaded when container startup. The bigger value means the servlet will be loaded more later.

2.3 The load-on-startup Example.

  1. There are 3 servlets defined in the below web.xml file. LoadFirst.java, LoadSecond.java, and LoadOnRequest.java. Their load-on-startup value is 0,1 and -1.
    <servlet>
      <description></description>
      <display-name>LoadFirst</display-name>
      <servlet-name>LoadFirst</servlet-name>
      <servlet-class>com.dev2qa.example.loadservlet.LoadFirst</servlet-class>
      <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>LoadFirst</servlet-name>
      <url-pattern>/LoadFirst</url-pattern>
    </servlet-mapping>
    <servlet>
      <description></description>
      <display-name>LoadSecond</display-name>
      <servlet-name>LoadSecond</servlet-name>
      <servlet-class>com.dev2qa.example.loadservlet.LoadSecond</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>LoadSecond</servlet-name>
      <url-pattern>/LoadSecond</url-pattern>
    </servlet-mapping>
    <servlet>
      <description></description>
      <display-name>LoadOnRequest</display-name>
      <servlet-name>LoadOnRequest</servlet-name>
      <servlet-class>com.dev2qa.example.loadservlet.LoadOnRequest</servlet-class>
      <load-on-startup>-1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>LoadOnRequest</servlet-name>
      <url-pattern>/LoadOnRequest</url-pattern>
    </servlet-mapping>
  2. We also write below java code in each servlet’s init() method.
  3. LoadFirst.java
     @Override
     public void init() throws ServletException {
     System.out.println("LoadFirst init.");
     }
  4. LoadSecond.java
     @Override
     public void init() throws ServletException {
     System.out.println("LoadSecond init.");
     }
  5. LoadOnRequest.java
     @Override
     public void init() throws ServletException {
     System.out.println("LoadOnRequest init.");
     }
  6. When you start tomcat, you will see the below output in the tomcat console.
    // This means the container load and initialize LoadFirst before LoadSecond.
    LoadFirst init.
    
    LoadSecond init.
  7. When you browse URL http://localhost:8080/Dev2qaWebAppExample/LoadOnRequest, you can see the below output in the tomcat console.

    // This means LoadOnRequest is loaded by container when it is requested for the first time.
    LoadOnRequest init.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.