Java ServletConfig

Java ServletConfig tutorial shows how to pass initialization data to a servlet with ServletConfig.

Java ServletConfig

Java 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.

Java Servlet

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

Pure.css is a set of small, responsive CSS modules that can be used in every web project. The library is created by Yahoo.

Java ServletConfig example

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">

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

&lt;groupId&gt;com.zetcode&lt;/groupId&gt;
&lt;artifactId&gt;JavaServletConfigEx&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;

&lt;name&gt;JavaServletConfigEx&lt;/name&gt;

&lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
        &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
        &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;build&gt;
    &lt;plugins&gt;

        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.3&lt;/version&gt;
            &lt;configuration&gt;
                &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;

    &lt;/plugins&gt;
&lt;/build&gt;

</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>

            &lt;input type="text" name="name"&gt;
            &lt;button type="submit" class="pure-button pure-button-primary"&gt;Submit&lt;/button&gt;
        &lt;/fieldset&gt;
    &lt;/form&gt;
&lt;/body&gt;

</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.

ad ad