Java ServletConfig tutorial shows how to pass initialization data to a servlet with ServletConfig.
last modified July 13, 2020
Java ServletConfig tutorial shows how to pass initialization data to a servlet with ServletConfig.
ServletConfig is a servlet configuration object used by a servlet container to pass information to a servlet during initialization. Servlet container creates a ServletConfig for each servlet in a web application.
Servlet is a Java class which responds to a particular type of network request - most commonly an HTTP request. Java servlets are used to create web applications. They run in servlet containers such as Tomcat or Jetty. Modern-day Java web development uses frameworks that are built on top of servlets.
Pure.css is a set of small, responsive CSS modules that can be used in every web project. The library is created by Yahoo.
In the following web application, we have a simple web form. We sent a name parameter to the servlet. If the parameter is empty, we read an initialization parameter through ServletConfig. In the example, we also use Yahoo’s Pure.css library.
$ tree . ├── nb-configuration.xml ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ └── web │ │ └── MyServlet.java │ └── webapp │ ├── index.html │ ├── META-INF │ │ └── context.xml │ └── WEB-INF └── test └── java
This is the project structure.
pom.xml
<?xml version=“1.0” encoding=“UTF-8”?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zetcode</groupId>
<artifactId>JavaServletConfigEx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JavaServletConfigEx</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is the Maven pom.xml file. The javax.servlet-api artifact is used for Java servlets. The maven-war-plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive (WAR).
context.xml
<?xml version=“1.0” encoding=“UTF-8”?> <Context path="/JavaServletConfigEx”/>
In the Tomcat context.xml file, we define the context path. It is the name of the web application.
index.html
<!DOCTYPE html> <html> <head> <title>Home Page</title> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1”> <link rel=“stylesheet” href=“https://unpkg.com/purecss@1.0.0/build/pure-min.css"> </head> <body> <form class=“pure-form” action=“MyServlet”> <fieldset> <legend>Enter your name</legend>
<input type="text" name="name">
<button type="submit" class="pure-button pure-button-primary">Submit</button>
</fieldset>
</form>
</body>
</html>
This is the home page. It contains an HTML form. Upon submitting the form, the processing is sent to the MyServlet.
<link rel=“stylesheet” href=“https://unpkg.com/purecss@1.0.0/build/pure-min.css">
We include the Pure.css library.
<form class=“pure-form” action=“MyServlet”>
The form tag uses the pure-form class from the Pure.css library. The action attribute points to the MyServlet.
<input type=“text” name=“name”>
The name value entered by the user will be sent to the servlet as a name parameter.
<button type=“submit” class=“pure-button pure-button-primary”>Submit</button>
The Submit button uses pure-button and pure-button-primary classes.
com/zetcode/MyServlet.java
package com.zetcode.web;
import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet(name = “MyServlet”, urlPatterns = {"/MyServlet”}, initParams = { @WebInitParam(name = “name”, value = “Guest”)}) public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
String name = request.getParameter("name");
name = name.trim();
if (name.isEmpty()) {
ServletConfig sc = getServletConfig();
name = sc.getInitParameter("name");
}
ServletOutputStream os = response.getOutputStream();
os.println("Hello " + name);
}
}
The MyServlet reads the name attribute from the request and generates output. The output is plain text.
@WebServlet(name = “MyServlet”, urlPatterns = {"/MyServlet”}, initParams = { @WebInitParam(name = “name”, value = “Guest”)})
With @WebInitParam, we initialize a name parameter to the “Guest” value.
String name = request.getParameter(“name”); name = name.trim();
We read the name parameter from the request object and trim spaces.
if (name.isEmpty()) {
ServletConfig sc = getServletConfig();
name = sc.getInitParameter("name");
}
If no value was entered by the user, we read the name initialization parameter using ServletConfig. ServletConfig is retrieved with getServletConfig. The parameter is retrieved with getInitParameter.
ServletOutputStream os = response.getOutputStream(); os.println(“Hello " + name);
We write the text message to the ServletOutputStream.
In this tutorial, we have used ServletConfig to read an initialization parameter in a Java servlet.