JSP initialization parameters

In the previous tutorial, we have learned different scopes in JSPs like Session Sope, Context Scope, and PageContext Scope. In this tutorial we will learn the concept of initialization parameters that we have learned inĀ  Servlets, here we will learn how this concept maps with JSPs. We will also override the init() method.

Introduction of Initialization Parameters

We know that we can initialize parameter on the servlet to servlet basis. By using our Deployment Descriptor(Web.xml) or by using annotations and by specifying the name and value in the init param annotations or the XML node. Now we will look how to do this in JSPs; there are few differences that should be noticeable. Let’s try to understand with an Example.

Example of initialization parameters

First, create a JSP file, we will configure the init parameter for this JSP file, we will have a default user name which is used when the user passes no value. The created JSP file will look like this.

Initialization Parameter defined in Web.xml

To configure the userName initialization parameter for this page, we will update our Deployment Descryptor (Web.xml) first as shown below. Notice that we have used <servlet></servlet> node here to deffine <init-param></init-param> inside it.

Accessing Initialization Parameter through Servlet Config

To access this default user defined in the <init-param></init-param> above we will use the following JSP code below. we will use Servlet Config object to access it from the JSP.

Try to run this file as we have learned in our previous tutorial; the result will automatically fetch the default username from the Deployment Descriptor(Web.xml) and display it in the output without passing the query string in the URL. In the same way, we can also define as many parameters as we want on our requirements.

1 - Example init-param test run

Accessing Initialization Parameter through Servlet Context

In the above Example. We have accessed the initialized parameter with the help of servlet config object, if you want to make it accessible for the context scope (scope within the application), we can do this by overriding the jspInit() function as shown in the below source.


In this tutorial, we have learned following things.

  • JSP Initialization parameters
  • How to define the Initialization parameters
  • How to make use of init Param in JSPs