Explain the Java Servlet Architecture with a neat diagram
A Java Servlet is a server-side program that runs inside a web server.
It helps to create dynamic web pages by processing requests from the user (client) and sending back responses.
Servlets are written in Java and they run inside a Servlet Container such as Apache Tomcat.
Need for Servlets
Before servlets, websites used CGI (Common Gateway Interface) programs for dynamic content.
But CGI created a new process for every request, which made it slow and less efficient.
Servlets solved these problems by:
● Using threads instead of creating new processes.
● Being faster, more secure, and platform independent.
Main Components of Servlet Architecture
1. Client (Web Browser):
The client sends a request to the server using a web browser.
Example: Submitting a login form.
2. Web Server:
The web server receives the request and identifies that it needs to be processed by a servlet.
3. Servlet Container:
○ The servlet container (like Tomcat) is a part of the web server that runs the servlet.
○ It performs tasks like:
■ Loading the servlet class.
■ Managing the servlet life cycle.
■ Handling requests and responses.
■ Providing network services and security.
4. Servlet (Java Program):
○ The servlet contains the main logic.
○ It processes the request and generates a response.
5. Database / Application:
○ The servlet can interact with databases or other programs to fetch or store information.
6. Response:
○ The servlet sends the output (HTML, XML, or JSON) back to the client browser.
Servlet Architecture Diagram
Step-by-Step Working of Servlet Architecture
1. Client Request:
○ The user sends a request (e.g., submitting a form).
○ The request goes to the Web Server.
2. Web Server → Servlet Container:
○ The web server forwards the request to the Servlet Container.
3. Container Loads Servlet:
○ If the servlet is not already loaded, the container loads it and calls the init() method once.
4. Processing the Request:
○ The container calls the service() method of the servlet.
○ The service method decides whether to call doGet() or doPost() based on the request type.
5. Servlet Interacts with Database:
○ If needed, the servlet connects to a database to fetch or update information.
6. Response Creation:
○ The servlet generates a response (HTML or text) using the PrintWriter object.
7. Sending Response Back:
○ The response goes back through the container → web server → client browser.
Important Interfaces and Classes
Component Purpose
Servlet Basic interface for all servlets.
HttpServlet Used for handling HTTP requests.
ServletRequest Used to get input from the client.
ServletResponse Used to send output to the client.
ServletConfig Used for servlet configuration.
ServletContext Gives information about the web application.
Flow Summary
1. Client → Web Server → Servlet Container → Servlet → Database → Back to Client
2. The Servlet Container manages loading, execution, and unloading of the servlet.
3. The servlet uses service(), doGet(), and doPost() methods to process requests.
Advantages of Servlet Architecture
1. Fast:
Handles multiple requests using threads.
2. Portable:
Written in Java — runs on any platform with JVM.
3. Secure:
Provides built-in security features.
4. Efficient:
Only one instance is created, reducing memory usage.
5. Reusable:
Code can be easily reused in multiple web applications.
6. Easily Connected:
Can interact with databases using JDBC.