Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Servlet Getting Started

Setup and Prerequisites

To develop Servlets you need:

  • JDK 8+ — Set JAVA_HOME environment variable
  • Apache Tomcat 9+ — Download from tomcat.apache.org
  • IDE — Eclipse EE or IntelliJ IDEA Ultimate
  • Servlet API JARjavax.servlet-api-4.0.1.jar (provided by Tomcat, or add via Maven)
Maven pom.xml for Servlet Project
<?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.example</groupId>
    <artifactId>servlet-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!-- Servlet API (provided by Tomcat at runtime) -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- JSP API -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

First Servlet - HelloServlet

HelloServlet with Annotation Mapping
package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.*;

// @WebServlet annotation maps this servlet to /hello URL
// Servlet 3.0+ supports annotation-based configuration
@WebServlet(
    name = "HelloServlet",
    urlPatterns = {"/hello", "/greet"},
    loadOnStartup = 1
)
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // Read init parameters from web.xml or @WebInitParam
        String greeting = config.getInitParameter("greeting");
        System.out.println("Init param greeting: " + greeting);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String name = req.getParameter("name");
        if (name == null || name.trim().isEmpty()) name = "World";

        out.println("<!DOCTYPE html><html><body>");
        out.println("<h1>Hello, " + name + "!</h1>");
        out.println("<p>Request URI: " + req.getRequestURI() + "</p>");
        out.println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // Delegate POST to GET handler
        doGet(req, resp);
    }
}
<!-- web.xml alternative to @WebServlet annotation -->
<web-app>
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.HelloServlet</servlet-class>
        <init-param>
            <param-name>greeting</param-name>
            <param-value>Hello</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Project Structure and Deployment

servlet-demo/
├── src/main/java/com/example/
│   └── HelloServlet.java
├── src/main/webapp/
│   ├── index.html
│   └── WEB-INF/
│       └── web.xml
└── pom.xml

Build with Maven: mvn clean package — this creates a .war file in the target/ directory. Copy the WAR to Tomcat's webapps/ folder and start Tomcat. Access at http://localhost:8080/servlet-demo/hello?name=Alice.


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.