Master java skills

ServletConfig Object

ServletConfig object is created for each servlet by the web container. This object is used by the servlet container to pass informaion to a servlet during initialization.

Method summary of ServletConfig

MethodsDescription
public String getInitParameter(String name)Returns the value of against an initial parameter
public Enumeration getInitParameterNames()Returns the enumeration of all initial parameters
public String getServletName()Returns the servlet name
public ServletContext getServletContext()Returns the ServletContext object

Getting ServletConfig object

Servlet interface’s method getServletConfig method gives the object of ServletConfig. Since, every servlet implements Servlet interface, this method is available in all the the servlet classes.

ServletConfig config = getServletConfig();

ServletConfig example

Create a dynamic web project using maven. If you don’t know how to create one, click here.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javatrainingschool</groupId>
	<artifactId>my-first-servlet-app</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
			</plugin>
		</plugins>
	</build>
</project>

ServletConfigExample.java

package com.sks;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletConfigExample extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();

		ServletConfig config = getServletConfig();

		String initParam1 = config.getInitParameter("initParam1");
		out.println("Value of initParam1 : " + initParam1 + "<br>");

		Enumeration<String> initParams = config.getInitParameterNames();

		while (initParams.hasMoreElements()) {
			String str = initParams.nextElement();
			out.println("<br><br> Init param name: " + str +  "<br>");
			out.println("Init param value: " + config.getInitParameter(str));
		}

		out.close();
	}
}

web.xml

<?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>my-first-servlet-app</display-name>
	
	<servlet>
		<servlet-class>com.sks.ServletConfigExample</servlet-class>
		<servlet-name>configServlet</servlet-name>

		<init-param>
			<param-name>initParam1</param-name>
			<param-value>ABC</param-value>
		</init-param>
		
		<init-param>
			<param-name>initParam2</param-name>
			<param-value>XYZ</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>configServlet</servlet-name>
		<url-pattern>/configExample</url-pattern>
	</servlet-mapping>

</web-app>

Deploy the application on tomcat and run the server.

Test using below url

http://localhost:8080/my-first-servlet-app/configServletExample