In this article, we are going to understand how to read the servlet configurations using the ServletConfig object.
These configurations are set in the deployment descriptor file, web.xml.
The Servlet configuration could point to -
The name of the Servlet.
The initialization parameters associated with a Servlet.
During the initialization of a Servlet, the Servlet Container uses a ServletConfig object to communicate with a Servlet and pass the
configurations specific to it.
ServletConfig is an interface and it is a part of the Servlet API. The full path to import and access all the methods provided by ServletConfig is
javax.servlet.ServletConfig.
ServletConfig methods
Let's take a look at the methods part of javax.servlet.ServletConfig interface.
Methods
Description
StringgetInitParameter(String str)
This method returns the value of a parameter named str .
EnumerationgetParameterNames()
This method returns all the names of the parameters associated with the servlet.
ServletContextgetServletContext()
This method returns a reference to the ServletContext.
ServletNamegetServletName()
This method returns the name of the Servlet being executing.
Creating a webpage which calls the Servlet
We are creating a webpage which asks the user to click a button named Yes, let's read!,
which when clicked will execute a Servlet to display the Servlet Configurations, such as -
Name of the Servlet
Initialization parameters of Servlet
Both of these configuration details are mentioned in the deployment descriptor file(web.xml). But before we see the deployment descriptor file, let's see
the webpage which reads the configuration details assoiciated with a servlet.
Webpage1.jsp
<html>
<head>
<title> ServletRequest Demo </title>
</head>
<body>
<b>Do you want to read the Initialization parameters and name of the Servlet?</b>
<br/>
<br/>
<br/>
<form action = "MyServlet">
<input type = "submit" value = "Yes, let's read!" />
</form>
</body>
</html>
Using ServletConfig
Next, we are creating a servlet, which extends the GenericServlet abstract class. The GenericServlet class implementsServlet and ServletConfig interface, hence
we can directly call the methods of ServletConfig within this Servlet.
In this Servlet class, we are going to read the initialization parameters of a Servlet by calling the methods of ServletConfig, such as -
getInitParameterNames()
getInitParameter()
Next, we are going to read the name of the servlet by calling a method of ServletConfig -
getServletName()
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyServlet1 extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2> Using ServletConfig object</h2> ");
//Reading one parameter
out.println("<b>Reading only one parameter its value </b> <br/>");
out.println("The Subject is " + getInitParameter("Subject"));
out.println("<br/>");
out.println("<br/>");
//Reading all the parameters
out.println("</b> Reading all the parameter and their values </b> <br/>");
Enumeration enum1 = getInitParameterNames();
while(enum1.hasMoreElements())
{
String paramName = (String)enum1.nextElement();
String paramValue = getInitParameter(paramName);
out.println("Parameter "+ paramName + " has a value " + paramValue);
out.println("<br/>");
}
out.println("<br/>");
//Reading the name of the Servlet
out.println("</b> Reading the name of Servlet</b> <br/>");
out.println("Servlet Name : " + getServletName());
}
}
Advertisement
Directory Structure of Servlet files
The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-
Project Folder - We have created a project folder named ServletConfigEx within
the webapps folder of Tomcat installation folder. This folder contains the Servlet class(.java) file and WEB-INF folder.
WEB-INF - Within your project folder, you create another folder named WEB-INF and this folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - Within the WEB-INF folder you should create a folder named classes. This folder should contain the compiled(.class) form of your Java Servlet class.
Creating the Deployment Descriptor file
As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml.
So, let's create one and this time, add some configurations specific to our Servlet i.e. initialization parameters and name of the Servlet -
In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class, we have given it a unique name ServletConfigExample.
<servlet-class> tag is used to specify the full qualified name of the Servlet class and in our example our Servlet class is named MyServlet1.
We have also specified three initialization parameters specific to this Servlet using <init-param> tag.
This tag has two child tags <param-name> and <param-value> :
<param-name> tag is used to specify the name of a parameter.
<param-value> tag is used to specify the value of this parameter associated with our Servlet class named MyServlet1.
Note
Name of the parameters must be unique.
The child tag <servlet-name> of <servlet> tag is matched with the <servlet-name> child tag of
<servlet-mapping>. The
<url-pattern> child tag is used to specify the URL to access Servlet and we have named this URL MyServlet.
Setting the classpath
Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java.
Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named
servlet-api.jar.
This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of
Tomcat installation folder.
For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib
There are two ways to set the classpath -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
Compiling the Servlet class
After setting the classpath, you need to compile the Servlet class by entering the command at the folder where you've stored the Servlet class file.
javac -d WEB-INF/classes MyServlet1.java
Executing the Servlet
First we execute the webpage containing the form, asking the user to enter the name and press submit button.
As soon as the user enters the name and submits the form, the request goes to the Servlet.
Hence, you will see the URL containing the full path to the file with the request parameter -
The project folder - ServletConfigEx
The url-pattern of the Servlet class declared in web.xml - MyServlet
When executed, this Servlet displays all the initialization parameters associated wth the Servlet and its name using the
methods of ServletConfig object.