Servlet – Beginner Explanation
Servlet (or Java Servlet) is a basic technology used to develop web applications in Java.
A Servlet is a Java class that runs on a special environment called a Servlet Container.
Important points about Servlets:
- Servlets do not have a
main()method - They are not executed directly
- They are controlled and managed by another Java program called a Servlet Container
Servlet Application
A Servlet application usually contains:
- One or more Servlets
- One or more JSP (JavaServer Pages)
A JSP page is internally:
- Translated into a Servlet
- Then compiled and executed
Because of this, a web application can also be called a:
- Servlet application
- JSP application
- Servlet/JSP application
All mean the same thing.
Servlet Container
A Servlet Container is like a web server, but with extra features.
A normal web server:
- Serves only static content
(HTML, CSS, images)
A Servlet Container:
- Serves static content
- Also generates dynamic content
- Can execute Servlets and JSP pages
Because of this, a Servlet Container is also called:
- Servlet Engine
- Servlet/JSP Container
Role of Servlet Container
The Servlet Container works as a middle layer between the user and the Servlet application.
It does the following:
- Receives requests from the user (browser)
- Passes the request to the Servlet
- Receives the response from the Servlet
- Sends the response back to the user
Objective of a Servlet
The main purpose of a Servlet is to:
- Read data sent by the client
(for example: form data) - Process the data and generate a result
- Send the result back to the client
(for example: HTML output)
In simple words:
Servlet = takes request → processes data → sends response