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. welcome-file-list is used to display the default web page to a user when user do not point out which page he want to browse in url. load-on-startup is used to set servlet startup order and priority when servlet container startup.
1. welcome-file-list xml element
When a user browse a url like http://localhost:8080/Dev2qaWebAppExample/, he dose not tell web server which page to show. He only tell web server he want to browse web application Dev2qaWebAppExample, so which page will web server send back to client? This is decided by the welcome-file-list xml element in web.xml file. Below is a part of web.xml
You can see that there has a list of welcome-file in welcome-file-list. Then when you browse url http://localhost:8080/Dev2qaWebAppExample/ again. The web server will find below url in the order that 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
When one of above web page 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 above list will be ignored by web server.
2. load-on-startup xml element
We all know that servlet will be loaded and startup when container receive the first request to the servlet, this will cost more time for the first request of it. But when you add load-on-startup element for servlet definition in web.xml, there will have following benefits when it is startup.
Benefits for use load-on-startup
- Servlet will be loaded when the container startup. This will make the response time shorter for the first request to it.
- If you have multiple servlet defined in web.xml, and they should startup in correct order, then you can use load-on-startup value to fix the starup order of them.
load-on-startup Value
The load-on-startup element’s value can be positive or negative.
- Negative : Means the servlet will be loaded when it is first requested.
- Positive : Define the order number that servlet will be loaded when container startup. More bigger the value is more later the servlet will be loaded.
load-on-startup Example
There has 3 servlet defined in below web.xml file. LoadFirst.java, LoadSecond.java and LoadOnRequest.java. Their load-on-startup value is 0,1 and -1.
We also write below java code in each servlet’s init() method.
LoadFirst.java
@Override public void init() throws ServletException { System.out.println("LoadFirst init."); }
LoadSecond.java
@Override public void init() throws ServletException { System.out.println("LoadSecond init."); }
LoadOnRequest.java
@Override public void init() throws ServletException { System.out.println("LoadOnRequest init."); }
When you start tomcat, you will see below output in the tomcat console.
This means the container load and initialize LoadFirst before LoadSecond.
When you browse url http://localhost:8080/Dev2qaWebAppExample/LoadOnRequest , you can see below output in tomcat console.
This means LoadOnRequest is loaded by container when it is requested for the first time.
[download id=”1135″]