0
Sponsored Links


Ad by Google
In web application we have web.xml file which is also known as deployment descriptor, In web.xml file we have <load-on-startup> tag inside servlet tag.

The <load-on-startup> is an optional tag in web.xml. The Server initializes this servlet when Server starts up.

This tag is optional in web.xml file. The tag is define inside servlet tag only. The optional content of this element must be a positive integer indicating the order in which the servlet should be loaded.

Priorities start from lower integer, lower integers are loaded before higher integers for example if you pass values as 1,2,3.. in respective servlets. Server will starts initialization of servlet from 1,2,3.. If no value is specified, or if the value specified is not a positive integer, then Server can load the servlet in any order during application startup

Below is the example of web.xml with three different servlet with different priority:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>Load-On-Start Up</display-name>

	<servlet>
		<servlet-name>servlet1</servlet-name>
		<servlet-class>org.myorg.Servlet1</servlet-class>
		<load-on-startup>1</load-on-startup>  <!-- higher priority load first -->
	</servlet>
	<servlet>
		<servlet-name>servlet2</servlet-name>
		<servlet-class>org.myorg.Servlet2</servlet-class>
		<load-on-startup>2</load-on-startup>  <!-- load second -->
	</servlet>

	<servlet>
		<servlet-name>servlet3</servlet-name>
		<servlet-class>org.myorg.Servlet3</servlet-class>
		<load-on-startup>3</load-on-startup>  <!-- load third -->
	</servlet>

	<!-- Servlet mappings -->

	<servlet-mapping>
		<servlet-name>servlet1</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>servlet2</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>servlet3</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

In short load-on-startup with value 1 tells server that, this servlet needs more priority than others do initialize this servlet as soon as you are ready(server startup).


References:
http://docs.oracle.com/cd/E24329_01/web.1211/e21049/web_xml.htm#WBAPP521
Sponsored Links

0 comments:

Post a Comment