Reading initialization parameters from ServletContext

In this post, you will learn:

  • How to obtain ServletConfig in a Servlet?
  • How to obtain ServletContext from ServletConfig?
  • How to obtain initialization parameters from ServletConfig & ServletContext?

Let’s understand the need of initialization parameters:

In the practical example of RequestDispatcher, we used DividerServlet & HandlerServlet. Lets have another look at the them.

DividerServlet code snippet:

package com.techmentro.learningpad;
...
public class DividerServlet extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response) 
	{
		try
		{
		...
		//RequestDispatcher is created for index.html
		RequestDispatcher rd=request.getRequestDispatcher("index.html");
		...
		}catch(Exception e)
		{
		...
		//RequestDispatcher is created for handlerServlet
		RequestDispatcher rd=request.getRequestDispatcher("/handlerServlet");
		...
		}
		
	}
}


HandlerServlet code snippet:

package com.techmentro.learningpad;
...
public class HandlerServlet extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		
		...
		//RequestDispatcher is created for index.html
		RequestDispatcher rd=request.getRequestDispatcher("index.html");
		...
		
		
	}
}

RequestDispatcher is created for index.html (the home page) and HandlerServlet in these servlets by hard coding their URI. In future, if their URI is changed in web.xml, all the referencing servlets need to be modified. In this example there are only two servlets but in real applications there may be hundreds. Imagine what a maintenance nightmare it will be? If we know that home page & the HandlerServlet are referenced by other servlets of the application then we should define <context-param> for them in web.xml as follows:

<web-app> 

<context-param>
<param-name>homePage</param-name>
<param-value>index.html</param-value>
</context-param>
<context-param>
<param-name>errorHandler</param-name>
<param-value>/handlerServlet</param-value>
</context-param>
...
</web-app>

The advantage of this approach is that their URI can be changed without modifying any of the referencing servets. Now we have to modify the servlets to read initialization parameters but before that we should learn how to do so?

Both ServletConfig & ServletContext interfaces provide following methods for initialization parameters.

1. getInitParameter(): This method is used to read the value of an initialization parameter.

Syntax:

public String getInitParameter(String paramName);

2. getInitParameterNames(): This method is used to obtain the name of all initialization parameters.

Syntax:

public Enumeration<String> getInitParameterNames();

In addition to these, ServletConfig interface provides following methods as well:

1. getServletContext(): This method is used to obtain the reference of the ServletContext from the ServletConfig.

Syntax:

public ServletContext getSevletContext();

2. getServletName(): This method is used to obtain the name of the servlet to which the ServletConfig object is associated.

Syntax:

public String getServletName();

Here is the Code snippet of modified DividerServlet:

package com.techmentro.learningpad;
...
public class DividerServlet extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response) 
	{
	  //Reference of the ServletContext object is obtained from the ServletConfig
          ServletContext ctx=getServletConfig().getServletContext();

              try
		{
		...
               
		//RequestDispatcher is created for the home page
		RequestDispatcher rd=
                request.getRequestDispatcher(ctx.getInitParameter("homePage"));
		...
		}catch(Exception e)
		{
		...
		//RequestDispatcher is created for errorHandler
		RequestDispatcher rd=
                request.getRequestDispatcher(ctx.getInitParameter("errorHandler"));
		...
		}
		
	}
}


Code snippet of modified HandlerServlet:

package com.techmentro.learningpad;
...
public class HandlerServlet extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
	   

		...
                //Reference of the ServletContext object is obtained from the ServletConfig
                ServletContext ctx=getServletConfig().getServletContext();
		//RequestDispatcher is created for the home page
		RequestDispatcher rd=
                request.getRequestDispatcher(ctx.getInitParameter("homePage"));
		...
		
		
	}
}

In this example, we have covered one use of both ServletConfig & ServleContext. We used the ServletConfig to obtain the reference of ServletContext and used ServletContext to obtain the intialization parameters. Their other uses will be covered in upcoming posts.

Note: We used the getServletConfig() life cycle method of the Servlet to obtain the reference of the ServletConfig object. This method is defined by GenericServlet class and is inherited in our servlet through HttpServlet.


Its time to recap the main concepts:

  1. getServletConfig() method of Servlet interface is used to obtain ServletConfig object.
  2. getServletContext() method of ServletConfig interface is used to obtain the reference of ServletContext in a Servlet.
  3. getInitParameter() method of ServletConfig and ServletContext interfaces is used to obtain the value of an initialization parameter.

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “Reading initialization parameters from ServletContext

  1. Nice Content!
    It is very helpful and authentic content on Java and the best one I ever found online on Java technology.Every one should read this nice and quality based content.

    Best of luck for your work!

Leave a Reply

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