Department of Information Technology
Experiment No: 09 Batch: A-S4 Date:24/9/25
Name: Aaditya Mishra Roll no: A-368
Aim : To study and implement Servlet.
Theory :
1. Introduction
A Servlet is a Java program that runs on a web server and is used to handle client
requests and generate dynamic responses. It works as the middle layer between
client (browser) and server (database or application). Servlets are part of Java EE
(Jakarta EE).
2. Need for Servlets
• Traditional CGI (Common Gateway Interface) was slow and less efficient.
• Servlets are more efficient, portable, secure, and scalable.
• They can process requests (like form data) and give responses (like HTML
pages, JSON, XML).
3. Features of Servlet
• Platform-independent (written in Java).
• Efficient and secure.
• Supports multithreading (can handle multiple requests simultaneously).
• Reusable and easy to maintain.
• Provides better performance compared to CGI.
4. Servlet Lifecycle
The lifecycle of a servlet is managed by the Servlet Container (e.g., Apache
Tomcat). It has three main methods:
1. init() → called once when the servlet is loaded.
Department of Information Technology
2. service() → called for every client request; handles doGet() or doPost().
3. destroy() → called once before servlet is unloaded from memory.
Flow: Load → Initialize → Service Requests → Destroy
5. Types of Servlets
1. Generic Servlet → Protocol-independent, abstract class.
2. HTTP Servlet → Most commonly used, handles HTTP requests (GET,
POST).
6. Example Flow of a Servlet
1. Client sends request (URL in browser).
2. Web server forwards it to servlet container.
3. Servlet processes request and generates response (HTML/JSON).
4. Response is sent back to client’s browser.
Program 1: Write a Servlet application for login page, to verify the
username and password. If username and password are matched, display
welcome message.
Program :
File : [Link]
Department of Information Technology
Department of Information Technology
Department of Information Technology
File : [Link]
File : [Link]
Department of Information Technology
Output :
Department of Information Technology
Program 2 : Write a Servlet application to count the total number of
visits on your website.
Program :
File : [Link]
Department of Information Technology
Output :