Servlet Annotations in Java (@WebServlet) – Beginner’s Guide
Introduction
Earlier, Java Servlets were configured using the web.xml deployment descriptor.
While powerful, web.xml often became large, complex, and hard to maintain.
To simplify servlet configuration, Servlet Annotations were introduced—especially the @WebServlet annotation.
This lesson explains:
What servlet annotations are
What
@WebServletdoesHow to use it step by step
Why it is preferred in modern Java web development
No prior knowledge of annotations is required.
What Are Servlet Annotations?
Simple Definition:
Servlet annotations are special Java tags used to configure servlets directly in the Java class, without using web.xml.
In simple words:
Annotations replace XML configuration with readable Java code.
What Is @WebServlet?
Definition:
@WebServlet is an annotation used to define and map a servlet to a URL.
📌 It tells the servlet container:
This class is a servlet
Which URL should trigger it
Why Use @WebServlet?
Problems with web.xml
Large XML files
Hard to read
Configuration separate from code
Error-prone
Benefits of @WebServlet
Less configuration
Easy to understand
Cleaner code
Faster development
Preferred in modern applications
Most new projects use annotations instead of web.xml.
Basic Syntax of @WebServlet
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
}
This maps the servlet to:
http://localhost:8080/YourApp/hello
Complete Example Using @WebServlet
package com.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Welcome! Servlet Annotation is working.</h1>");
}
}
How @WebServlet Works (Behind the Scenes)
Tomcat Server Starts
↓
Scans Servlet Classes
↓
Finds @WebServlet Annotation
↓
Registers URL Mapping
↓
Servlet Ready to Handle Requests
No need to write mapping in web.xml.
Using Multiple URL Patterns
@WebServlet(urlPatterns = {"/login", "/signin"})
public class LoginServlet extends HttpServlet {
}
Same servlet can respond to multiple URLs.
Important Attributes of @WebServlet
| Attribute | Purpose |
|---|---|
urlPatterns | Defines servlet URL |
name | Logical servlet name |
loadOnStartup | Loads servlet at startup |
initParams | Initialization parameters |
Example: loadOnStartup
@WebServlet(urlPatterns = "/init", loadOnStartup = 1)
public class InitServlet extends HttpServlet {
}
Servlet loads when server starts, not on first request.
Example: initParams
@WebServlet(
urlPatterns = "/config",
initParams = {
@WebInitParam(name = "admin", value = "true")
}
)
public class ConfigServlet extends HttpServlet {
}
@WebServlet vs web.xml
| Feature | @WebServlet | web.xml |
|---|---|---|
| Configuration | Code-based | XML-based |
| Readability | High | Medium |
| Modern Usage | Preferred | Legacy |
| Centralized Config | No | Yes |
| Interview Importance | High | High |
Both should be understood, but annotations are used more today.
Real-Life Analogy
Think of a Doorbell
URL → Doorbell button
@WebServlet → Label on the bell
Servlet → Person answering the door
When someone presses the bell, the labeled person responds.
Best Practices for Beginners
- Use
@WebServletfor simple mappings - Keep URL patterns meaningful
- Avoid duplicate mappings
- Use annotations consistently
- Restart server if mapping doesn’t update