Advance Java Technologies - Servlets: Overview and Architecture

Comprehensive study notes, diagrams, and exam preparation for Advance Java Technologies - Servlets: Overview and Architecture.

Advance Java Technologies - Servlets: Overview and Architecture

Definition

A Servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although Servlets can respond to any type of request, they are most commonly used to extend the applications hosted by web servers to provide dynamic content.


Main Content

1. Servlet Container

  • The Servlet Container (also known as a Web Container) is the component of a web server that interacts with the Java servlets.
  • It is responsible for managing the lifecycle of servlets, mapping URLs to specific servlets, and ensuring the requester has the correct access rights.

2. The Servlet Interface

  • The javax.servlet package defines the core Servlet interface, which all servlets must implement.
  • It contains essential lifecycle methods like init(), service(), and destroy(), which control how the servlet behaves within the container.

3. Request and Response Objects

  • The HttpServletRequest object contains all the information sent by the client, such as form parameters, cookies, and HTTP headers.
  • The HttpServletResponse object allows the servlet to send data, status codes, and headers back to the client, forming the dynamic web page.

Working / Process

1. Request Reception

  • When a client (browser) sends an HTTP request, the web server receives it and forwards it to the Servlet Container.
  • The container determines which servlet should handle the request based on the web.xml configuration or annotations.

2. Execution of Service

  • If the servlet is not initialized, the container calls the init() method once to set it up.
  • The container then invokes the service() method, which dispatches the request to either doGet() or doPost() depending on the HTTP method used.

3. Response Delivery

  • The servlet processes the business logic (e.g., querying a database) and populates the HttpServletResponse object.
  • The container sends this formatted response back to the client's browser, completing the transaction.
[Client] ---> (HTTP Request) ---> [Web Server/Container]
                                     |
                                [Servlet Instance]
                                     |
[Client] <--- (HTTP Response) <--- [Servlet Container]

Advantages / Applications

  • Platform Independence: Being written in Java, servlets can run on any OS that supports a Java Virtual Machine (JVM).
  • Performance: Unlike CGI scripts, servlets handle requests in lightweight threads within the same process, leading to faster execution.
  • Robustness: Servlets leverage the full power of the Java API, including database connectivity (JDBC), exception handling, and memory management.

Summary

Servlets are server-side Java components that process client requests and generate dynamic web content. They operate within a Servlet Container, which manages their lifecycle and ensures efficient communication between the client and the server. Important terms to remember include Servlet Container, HTTP Request/Response, init(), service(), and destroy() methods.