What are initialization parameters?

In this post, you will learn:

  • What are initialization parameters?
  • Why are initialization parameters needed?
  • What are the types of initialization parameters?
  • How are initialization parameters defined?
  • How are initialization parameters made available to the servlets?

Often servlets need external resources such as property files, folders, database drivers etc, for request processing. Hard coding these resources into the Servlets creates maintenance problem because if their name, value or location is changed, the Servlets need to be modified.

To facilitate the use of external resources in servlets in such a way that they can be changed without modifying the servlets, concept of initialization parameters is provided by the Servlet API. An Initialization parameter represents textual information as key value pair. It can be the name of a file, path of a folder, a driver class or any other information required by the servlets. It is defined by the programmer in web.xml file and is made available to a servlet or servlets of the application by the server.

Initialization parameters can be of two types:

  1. Servlet specific initialization parameter.
  2. Application specific initialization parameter.

Servlet specific initialization parameters are used to provide information to a specific servlet of the application. They are defined using <init-param> sub element of <servlet> element in web.xml.

Application specific initialization parameters are used to provide information to all the servlets of an application. They are defined using <context-param> sub element of <web-app> element in web.xml.

Syntax of defining initialization parameters:

<web-app>
<!-- 
application specific initializaiton parameters, 
multiple context param can be defined for the servlets.
 -->
<context-param>
<param-name>Name</param-name>
<param-value>Value</param-value>
<context-param>
...
<servlet>
<servlet-name>ObjName</servlet-name>
<servlet-class>ClassName</servlet-class>
<!-- 
Servlet specific initializaiton parameters,
multiple init param can be defined for a servlet.
-->
<init-param>
<param-name>Name</param-name>
<param-value>Value</param-value>
</init-param>
...
</servlet>
...
</web-app>

How the server provides initialization parameters to the servlets:

To provide initialization parameters to the servlets of an application, a web server uses objects of type ServletContext and ServletConfig. Both ServletContext & ServletConfig are interfaces of the Servlet API. Their implementation is provided by server vendors i.e. each java web server has classes which implement these interfaces.

For each web application, an object of type ServletContext is created by the server at the time of deployment. This object is also called application object because it has application scope i.e. it remains in the memory as long as the application remain deployed. It has following use:

  1. It is used by the server to provide its own reference to the application.
  2. It is used by the server to provide application specific initialization parameters to the servlets of the application.
  3. It facilitate sharing of application scope attributes among the servlets of the application.

For each servlet, an object of type ServletConfig is created by the server. It has following use:

  1. It is used by the server to provide the reference of ServletContext object to the servlet.
  2. It is used by the server to provide servlet specific initialization parameters to the servlet.
  3. It is used by the server to provide servlet name to the servlet.

Following diagram describes the role of ServletConfig & ServletContext:

relation between ServletConfig and ServletContext

Description of the diagram:

1.0 – Web application is deployed on the server.

1.1 – ServletContext object is created by the server and

a) – Server provides its own reference to the ServletContext so that it can communicate to the server on behalf of application components.

b) – If <context-param> are defined in web.xml, they are stored in a map (IP Map in the diagram) in it.

c) – A map (Att Map in the diagram) is created in it so that servlets can share attributes among them.

2.0 – First request is submitted by a client for a servlet of the application.

2.1 – Servlet object is created for the servlet by the server.

2.2 – ServletConfig object is created for the servlet and

a) – Reference of the ServletContext object is provided to it.

b) – If <init-param> are defined for the servlet in web.xml, they are stored in a map (IP Map in the diagram) in it.

c) – Servlet name is provided to it.

2.3 – Reference of the ServletConfig object is provided to the servlet as argument of init() method.

In the next post, we will learn how a servlet can obtain initialization parameters from the SevletConfig & Servletcontext objects.


Its time to recap the main concepts:

  1. Information stored by the programmer in web.xml file for the servlets is called initialization parameter.
  2. Initialization parameters are of two types, application specific & servlet specific.
  3. When information is to be made available to all the servlets, application specific initialization parameters are used.
  4. When information is to be made available to a single servlet, servlet specific initialization parameters are used.
  5. Application specific initialization parameters are made available to the servlets by the server through ServletContext object.
  6. Servlet specific initialization parameters are made available to a servlet by the server through ServletConfig object.
  7. Reference of ServletContext is made available to a servlet by the server through its ServletConfig object.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “What are initialization parameters?

  1. Great job!
    You are doing a great job by spreading your genuine and quality content to the masses.This is the best content I ever found online on Java technology.I hope that this should be beneficial for everyone and I don’t have words to praise for your great work and great content.

Leave a Reply

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