
Servlet architecture is a core concept in Java programming used for building dynamic web applications. It is part of Java’s server-side technology, enabling developers to handle client requests and generate dynamic responses efficiently. Before servlets, web applications primarily relied on CGI (Common Gateway Interface), which was slower and less scalable. Servlets improved performance, scalability, and request handling.
Servlets are widely used for:
- Processing client requests dynamically
- Generating dynamic web content
- Implementing business logic on the server
- Managing server-side operations efficiently
- Supporting scalable enterprise applications
Types of Servlets
Servlets are mainly classified into two types:
1. Generic Servlet
A protocol-independent servlet that belongs to the javax.servlet package. It is suitable for applications where protocol-specific functionality is not required.
Key features:
- Protocol-independent
- Part of the javax.servlet package
- Suitable for generic request processing
2. HTTP Servlet
An HTTP Servlet (HttpServlet) is the most commonly used type of servlet. It is specifically designed to work with the HTTP protocol and supports various HTTP request methods.
Key features:
- Designed specifically for HTTP-based applications
- Supports GET, POST, PUT, DELETE, and other HTTP methods
- Widely used in modern Java web applications
Ways to Create a Servlet
A servlet can be created in three ways:
- Implementing the Servlet interface
- Extending the GenericServlet class
- Extending the HttpServlet class
Among these, developers most commonly extend HttpServlet in modern web applications.
Servlet Life Cycle Methods
Every servlet follows a defined life cycle managed by the web container:
1. init()
The servlet container calls the init() method only once when it first loads the servlet into memory. Developers use this method to perform initialization tasks such as loading resources or establishing database connections.
2. service()
The service() method is invoked for every client request. It receives the request, processes it, and generates the appropriate response.
3. destroy()
The servlet container calls the destroy() method before it removes the servlet from memory. Developers use this method to release resources and perform any necessary shutdown tasks.
Components of Servlet Architecture
Below is a diagram showing how components work in the servlet architecture.
1. Client
In this architecture, the web browser acts as a Client. Client or user connected with a web browser. The client is responsible for sending requests or HTTP requests to the web server and processing the Web server’s responses.
2. Web Server
The web server controls how web users access hosted files and processes user requests and responses. Here, the server is software that understands URLs and the HTTP protocol. Whenever a browser needs to access a file on the web server, it sends an HTTP request; if it finds the requested file, it sends it back to the browser in an HTTP Response. Static web servers send the file as it is, while dynamic web servers update the server-hosted file before sending it to the browser.
3. Web Container
A web container is a component of a web server that works with Java servlets. It manages the servlet lifecycle, maps URLs, and processes server-side requests for servlets, JSP files, and other resources. The critical tasks performed by servlets include loading and unloading servlets, creating and managing request and response objects, and performing overall servlet management tasks.
Servlet Request Flow
The servlet request processing follows these steps:
- The client sends a request.
- The web server accepts the request and forwards it to the web container.
- The web container searches the web.xml file for the request URL pattern and retrieves the servlet’s address.
- If the servlet does not already exist, the server creates it and initializes it by calling the init() method.
- The container calls the public service() by passing ServletRequest and ServletResponse objects.
- Public service() method typecasts ServletRequest and ServletResponse objects to HttpServletRequest and HttpServletResponse objects, respectively.
- The public service() method calls for protected service().
- The protected service() method checks the client request & the corresponding do___() method is called.
- The request is handled by sending the result generated by do___() to the client.
Advantages of Servlet Architecture
Below are some essential advantages of the servlet as follows:
- Servlets are server-independent, as they are compatible with any web server. Compared to server-side web technologies like ASP and JavaScript, these are server-specific.
- Servlets are protocol-independent, i.e., they support FTP, SMTP, etc. Mainly, it provides extended support for the HTTP protocol.
- Servlets are persistent because they remain in memory until explicitly destroyed; this helps with request processing, and a single database connection can handle multiple requests.
- Servlets are portable because they are written in Java and run on any web server that supports Java.
- Servlets execute faster than other scripting languages because they compile into bytecode. Byte code conversion gives better performance and helps in type checking and error.
Uses of Servlet Architecture
Let us see some of the uses of the servlet that are given below:
- Servlets are used to form data manipulation, like accepting form data and generating dynamic HTML pages.
- It helps develop server load-balancing applications that balance load across different servers.
- Servlets are the middle tier in enterprise network platforms for connecting to the SQL database.
- Integration of servlets with applets enables the generation of high-level interactivity and dynamic web content.
- Developers use servlets to build applications in which they act as active agents in the middle tier, facilitating data sharing.
- Since the servlet supports protocols such as HTTP and FTP, it helps develop file-server and chat-enabled applications.
Example of Servlet Architecture
To better understand Servlet Architecture, let us look at a simple example of a servlet that responds with a greeting message.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Welcome to Servlet Architecture</h1>");
out.println("</body></html>");
}
}
How does this example work?
- The client sends an HTTP request to the server.
- The web server forwards the request to the servlet container.
- If the servlet container has not already loaded HelloServlet, it loads it.
- The container calls the doGet() method for the GET request.
- The servlet generates an HTML response.
- The servlet container returns the response to the client’s browser.
Conclusion
Servlet Architecture is a powerful foundation for building robust, scalable, and dynamic Java web applications. It improves performance compared to older technologies like CGI and provides better control over request-response handling. With features like platform independence, high scalability, and lifecycle management, Servlets remain a crucial part of Java enterprise development even today.
Recommended Articles
This is a guide to Servlet Architecture. Here, we discuss components, request flow, advantages, and uses of Servlet Architecture. You may also look at the following articles to learn more-
