GenericServlet Class Example

In this example, we will see how to use GenericServlet class with an example. GenericServlet class defines a generic, protocol-independent servlet. It means GenericServlet class can handle any type of request so it is protocol-independent.

GenericServlet Class Diagram

The below diagram shows that the GenericServlet class implements the Servlet and ServletConfig interfaces.

GenericServlet Class Example

Let's demonstrate the usage of GenericServlet class with an example.

Step 1: Add servlet dependency to pom.xml

Let's first add servlet 4.0.1 dependency to our 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 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>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>java-servlet-tutorial</finalName>
    </build>
</project>

Step 2: Create HelloWorldServlet which extends GenericServlet

Let's see the simple example of the servlet by inheriting the GenericServlet class. Note that we are using getServletName() and getInitParameter() methods within this example.
package net.javaguides.servlet.tutorial.genericservlet;

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(name = "HelloWorld", initParams = {
    @WebInitParam(name = "username", value = "Ramesh"),
    @WebInitParam(name = "password", value = "Pass@123")
}, urlPatterns = "/hello")
public class HelloWorldServlet extends GenericServlet {

    private static final long serialVersionUID = 1 L;

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter printWriter = res.getWriter();
        printWriter.print("<html>");
        printWriter.print("<body>");
        printWriter.print("<h1>Hello World GenericServlet Class Example</h1>");
        printWriter.print("<p> Servlet name :: " + getServletName() + "</p>");
        printWriter
            .print("<p> Servlet username init parameter :: " + getInitParameter("username") + "</p>");
        printWriter
            .print("<p> Servlet password init parameter :: " + getInitParameter("password") + "</p>");
        printWriter.print("<a href=\"http://www.javaguides.net\">Java Guides</a>");
        printWriter.print("</body>");
        printWriter.print("</html>");
        printWriter.close();
    }
}
Note that we are using @WebServlet annotation to create Servlet component.

Step 3: Demo

Let's deploy above servlet into a servlet container, for example, Tomcat 8. Once tomcat server is up and running without any errors then hit http://localhost:8080/java-servlet-tutorial/hello URL in a browser. You can see below web page on the browser:

Comments