Servlets
So far, we have written Java programs that run locally. But real-world Java applications often run on servers and respond to user requests.
Servlets are the core Java technology used to build server-side web applications. They handle requests from browsers and send responses back.
What Is a Servlet?
A Servlet is a Java class that runs on a web server and handles HTTP requests such as:
- User submitting a form
- Browser requesting a web page
- Client calling an API endpoint
Servlets act as the bridge between the client (browser) and backend Java logic.
Why Servlets Are Important
Servlets are the foundation of Java web development. Frameworks like Spring are built on top of servlet technology.
Servlets are used to:
- Process user input
- Interact with databases
- Generate dynamic web content
- Build REST APIs
Servlet Architecture (High-Level)
The request–response flow works like this:
- User sends a request from the browser
- Web server forwards it to the servlet
- Servlet processes the request
- Servlet sends a response back to the browser
Servlet Container
Servlets do not run by themselves. They run inside a Servlet Container.
Popular servlet containers:
- Apache Tomcat
- Jetty
- WildFly
The container manages servlet lifecycle and request handling.
Creating a Simple Servlet
Let us create a basic servlet that responds with text.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello from Java Servlet");
}
}
Understanding doGet and doPost
Servlets mainly handle two HTTP methods:
- doGet() – handles GET requests (fetching data)
- doPost() – handles POST requests (submitting data)
Example with doPost():
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
response.getWriter().println("Welcome " + name);
}
Handling User Input
Servlets can read data sent from HTML forms.
String email = request.getParameter("email");
String password = request.getParameter("password");
This is commonly used in login and registration systems.
Real-World Example
Consider a login system:
- User enters username and password
- Servlet validates credentials
- Servlet redirects user or shows error
Servlets handle this logic behind the scenes.
Servlet Lifecycle
A servlet goes through three main phases:
- init() – servlet is initialized
- service() – handles requests
- destroy() – servlet is removed
The container controls this lifecycle automatically.
Advantages of Servlets
- Platform independent
- High performance
- Secure and scalable
- Foundation of Java web frameworks
Where Servlets Are Used
- Java web applications
- REST APIs
- Enterprise systems
- Microservices architecture
Key Takeaways
- Servlets handle HTTP requests and responses
- They run inside a servlet container
- Core technology behind Java web apps
In the next lesson, we will learn about JSP and how Java generates dynamic web pages.