Welcome File List in Servlet
In this article, I am going to discuss the Welcome File List in Java Servlet Application. Please read our previous article where we discussed WAR File in Java Application.
In general, all together in the online applications some pages like login pages, index pages, home pages are the primary pages. In the above context, to access the primary pages we’ve to specify the respective HTML page name or JSP page name as resource name in the URL even though they’re common for each and every user.
To overcome the matter, we’ve to declare the respective HTML or JSP page as a welcome file. The welcome file is that on the first page of the online application, it must be executed by the container automatically once we access the respective application without specifying the resource name in the URL.
To declare welcome, enter the web.xml file, we’ve to use the subsequent XML tags.
<web-app> <welcome-file-list> <welcome-file>file1</welcome-file> <welcome-file>file1</welcome-file> …………………………………………………………. </welcome-file-list> ……………………………… </web-app>
From the above tag’s representation, it’s possible to supply quite one welcome file with a single web application but with respect to multiple no. of modules. If we offer quite one welcome file with a single web application with respect to. modules the container will look for the respective welcome file as per the order during which we configured the web.xml file.
Explain <welcome-file-list> tag.
- The first web page of the web application that comes automatically when a request is given to the web application is called a welcome page or home page of the web application.
- In a java-based web application, the HTML or JSP program can be configured as a welcome page.
- When no welcome file is explicitly configured the java web application looks to take either index.jsp or index.html as the default HTML file.
- If both are available the index.html will be taken as the default welcome file.
- If multiple welcome files are configured then the web application picks up one welcome file based on the availability and configured order.
- If all explicitly configured welcome files are not available and if index,jsp, or index.html are available then also web application runs without a welcome file.
- We cannot configure a servlet program as a welcome file
In general, we’ll prepare web applications with the Tomcat server by creating the whole web application directly structure under webapps folder. In this case, once we start the server then automatically the prepared web application is going to be deployed into the server.
The above approach to deploying the online applications is named Hard Deployment, it’s not suggestible. To perform Smooth Deployment for web applications we’ve to use the subsequent steps:
- Prepare web application separately.
- Create the war file for the web application by using the following command.
- Start the server (Tomcat) and open Manager Applications.
- Upload the war file to deploy the web application.
- Access the application.
The optional load-on-startup Servlet element during a web.xml indicates that the associated Servlet is to be loaded and initialized as a part of the startup of the online application that declares it.
Note: It is possible to specify an empty load-on-startup element employing a Servlet 2.5-based web.xml, the limitation applies to Servlet 2.4-based web.xml only.
The optional content of this element is an integer indicating the order during which the Servlet is to be loaded and initialized with reference to the online application’s other Servlets. An empty load-on-Servlet indicates that the order is irrelevant, as long as the Servlet is loaded and initialized during the startup of its containing web application.
The Servlet 2.4 schema for web.xml no longer supports an empty <load-on-startup/>, meaning that an integer must be specified when using a Servlet 2.4 based web.xml. If specifying an empty <load-on-startup/>, as in <load-on-startup/>, the web.xml will fail validation against the Servlet 2.4 schema for web.xml, causing deployment of the Web application to fail.
Problem: Specifying an empty <load-on-startup/> still works with Servlet 2.3 based web.xml.
Solution: Specify <load-on-startup/>0</load-on-startup/> when using a Servlet 2.4 based web.xml to indicate that Servlet load order does not matter.
- If you will pass a positive value, the container loads the servlets in ascending integer value, the value 0 will be loaded first then 1, 2, 3, and so on.
- Servlet will be loaded at request time, at first request if you will pass the negative value.
Advantage of load-on-servlet
As servlet is loaded at first request, it consumes more time at the first request. So we are specifying the load-on-startup in web.xml, so the servlet will be loaded at project deployment time or server start which will take less time for responding to the first request.
In the next article, I am going to discuss how to use different Servlet IDE to create a Java Servlet Application. Here, in this article, I try to explain Welcome-file-list in Java Servlet Application. I hope you enjoy this Welcome-file-list in Java Servlet Application article.