UNIT III SERVER SIDE PROGRAMMING
3.1Java Servlets & Architecture
What are Servlets?
A Servlet is a small Java program that runs on a server. It is used to handle requests from clients (usually web browsers) and send back responses. Servlets are commonly used to create dynamic web pages, interact with databases, and process form data. Servlets are part of the Java EE (Enterprise Edition) platform and provide a way to extend the capabilities of a web server. They are used for processing HTTP requests and responses.
Java Servlets Architecture
The architecture of a Java Servlet consists of several key components that work together to handle client requests:
- Client: The client is typically a web browser that sends an HTTP request to the server.
- Web Server: A web server (like Apache Tomcat) listens for incoming HTTP requests from clients.
- Servlet Container: The servlet container (also called the servlet engine) is part of the web server. It is responsible for managing and executing servlets. It takes care of:
- Receiving the HTTP request from the client.
- Creating a new thread to handle the request.
- Forwarding the request to the appropriate servlet.
- Generating a response and sending it back to the client.
- Servlet: The servlet processes the request, performs necessary operations (e.g., database access, calculations), and generates the response.
- HTTP Response: The response is typically an HTML page, JSON data, or a redirect, sent back to the client’s browser.
Basic Workflow of a Servlet:
- Client sends HTTP request: When a client (e.g., a browser) requests a page or resource, it sends an HTTP request to the server.
- Servlet container receives the request: The servlet container on the server receives the request and maps it to a specific servlet.
- Servlet processes the request: The servlet uses methods like doGet() or doPost() to process the request. It can interact with databases, perform calculations, or retrieve data.
- Servlet generates a response: After processing the request, the servlet generates a response (typically an HTML page or data).
- Servlet container sends the response: The servlet container sends the response back to the client.
- Client receives the response: The client’s browser displays the result.
Servlet Architecture Diagram:
Here’s a simple diagram of how the servlet architecture works:

Example of a Simple Servlet:
1. Servlet Code (Java)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set content type for response
response.setContentType("text/html");
// Get the output stream to write the response
PrintWriter out = response.getWriter();
// Write the response
out.println("<html><body>");
out.println("<h1>Hello, World!</h1>");
out.println("<p>This is a simple servlet example.</p>");
out.println("</body></html>");
}
}
Explanation:
The doGet() method is called when the server receives a GET request from the client. The response is sent back as HTML content, including a message to the user.
2. web.xml (Deployment Descriptor)
<web-app>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
</web-app>
Explanation:
The servlet is mapped to the /simple URL pattern, which means it will be invoked when the client accesses http://<server-address>/simple.
Key Points about Servlets:
- Servlet Life Cycle: A servlet goes through several stages during its life:
- Loading: The servlet is loaded into memory when the server starts or the first time it is accessed.
- Initialization: The init() method is called when the servlet is initialized.
- Request Handling: The servlet processes client requests in the doGet() or doPost() methods.
- Destruction: When the servlet is destroyed (e.g., server shutdown), the destroy() method is called.
- HttpServlet: Most servlets extend HttpServlet, which provides methods like doGet() and doPost() for handling HTTP requests.
- Servlet Container: The servlet container (e.g., Tomcat) handles the request-response cycle and manages multiple servlet instances.
Benefits of Servlets:
- Performance: Servlets are more efficient than CGI (Common Gateway Interface) scripts because they are persistent and can handle multiple requests.
- Scalability: Servlets can be easily scaled by adding more server resources or using load balancing.
- Platform Independence: Servlets are platform-independent, as they run in any environment where a Java Virtual Machine (JVM) is available.
Comments
Post a Comment