ServletConfig Interface Example (Jakarta EE)

The ServletConfig interface is part of the Jakarta Servlet API and is used to pass configuration information to a servlet at initialization time. In this blog post, we will explore the ServletConfig interface and demonstrate how to use it in a Java servlet application.

Step 1: Add Servlet Dependency to pom.xml

First, add the Jakarta Servlet dependency to your pom.xml file. We are using version 6.1.0 in this example.

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides.servlet.tutorial</groupId>
    <artifactId>java-servlet-tutorial</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>java-servlet-tutorial Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- Jakarta Servlet dependency -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>java-servlet-tutorial</finalName>
    </build>
</project>

Step 2: Create Servlet using ServletConfig

Let's create a servlet that demonstrates the use of ServletConfig to retrieve initialization parameters.

MyServlet.java

package net.javaguides.servlet.tutorial.httpservlet;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/config")
public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private String email;
    private String phone;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        email = config.getInitParameter("email");
        phone = config.getInitParameter("phone");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>ServletConfig Example</h1>");
        out.println("<p>Email: " + email + "</p>");
        out.println("<p>Phone: " + phone + "</p>");
        out.println("</body></html>");
        out.close();
    }
}

In this example, the MyServlet class uses the ServletConfig interface to retrieve initialization parameters email and phone defined in the servlet configuration.

Step 3: Configure Initialization Parameters

To configure initialization parameters, use the @WebInitParam annotation in the servlet class.

MyServlet.java (with @WebInitParam)

package net.javaguides.servlet.tutorial.httpservlet;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
    urlPatterns = "/config",
    initParams = {
        @WebInitParam(name = "email", value = "[email protected]"),
        @WebInitParam(name = "phone", value = "123-456-7890")
    }
)
public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private String email;
    private String phone;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        email = config.getInitParameter("email");
        phone = config.getInitParameter("phone");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>ServletConfig Example</h1>");
        out.println("<p>Email: " + email + "</p>");
        out.println("<p>Phone: " + phone + "</p>");
        out.println("</body></html>");
        out.close();
    }
}

In this example, the @WebInitParam annotation is used to specify the initialization parameters directly in the servlet class.

Conclusion

In this blog post, we explored the ServletConfig interface and demonstrated how to use it in a Java servlet application to retrieve initialization parameters. We also showed how to configure these parameters using the @WebInitParam annotation. The ServletConfig interface is essential for passing configuration information to a servlet at initialization time, making it a powerful tool for servlet developers.

References

Related Servlet Posts

Comments

  1. Hello Ramesh,

    Nice blog! I am editor at Java Code Geeks (www.javacodegeeks.com). We have the JCG program (see www.javacodegeeks.com/join-us/jcg/), that I think you’d be perfect for.

    If you’re interested, send me an email to [email protected] and we can discuss further.

    Best regards,
    Eleftheria Drosopoulou

    ReplyDelete

Post a Comment

Leave Comment