HttpServlet
and GenericServlet
?ServletConfig
and ServletContext
object.HttpServlet
and GenericServlet
?
What is a Servlet?
A Servlet is a Java class that is used to build dynamic web applications. It runs on a Java-enabled web server (like Apache Tomcat) and handles client requests and generates responses, typically over the HTTP protocol.
Servlets are a core component of Java EE (Enterprise Edition) and are managed by a Servlet Container, which is part of a web server responsible for the lifecycle and mapping of servlets.
Key Features of Servlets
Feature | Description |
---|---|
Server-side Technology | Executes on the server to generate dynamic content |
Protocol Support | Primarily handles HTTP (via HttpServlet ) |
Platform Independent | Written in Java, hence cross-platform |
Persistent | Servlet objects are instantiated once and reused |
Secure | Java’s security features help mitigate threats |
Efficient | Uses threads instead of creating new processes (unlike CGI) |
Servlet Architecture
The Servlet architecture follows a request-response model where the client (browser) sends a request to the server, and the Servlet processes this request to return a response.
Servlet Architecture Components
Component | Description |
---|---|
Client (Browser) | Sends the HTTP request (GET/POST) |
Web Server | Receives the request and passes it to the Servlet Container |
Servlet Container | Loads the servlet, manages its lifecycle, and handles mapping |
Servlet | Processes the request and returns the response |
Response | The result (usually HTML or JSON) sent back to the client |
Servlet Request-Response Cycle
/loginServlet
).web.xml
or annotations).init()
if the servlet is not initialized.service()
method which dispatches to doGet()
or doPost()
.ServletResponse
object.Servlet Lifecycle (Managed by Container)
Phase | Method | Purpose |
---|---|---|
Initialization | init(ServletConfig) |
Initializes servlet instance (runs once) |
Request Handling | service() |
Handles all requests (delegates to doGet() or doPost() ) |
Destruction | destroy() |
Releases resources before servlet is removed |
Difference Between GenericServlet
and HttpServlet
GenericServlet
and HttpServlet
are two important classes in the javax.servlet
package. GenericServlet
is protocol-independent, while HttpServlet
is specifically designed for handling HTTP requests.
Feature | GenericServlet |
HttpServlet |
---|---|---|
Defined In | javax.servlet.GenericServlet |
javax.servlet.http.HttpServlet |
Protocol Support | Protocol-independent | HTTP-specific |
Methods Provided | Must override service(ServletRequest, ServletResponse) |
Provides doGet() , doPost() , doPut() , doDelete() , etc. |
Use Case | For custom protocols (FTP, SMTP) or generic server applications | For web-based applications using HTTP |
Subclass Responsibility | Subclass must implement service() |
Subclass implements doGet() , doPost() depending on request type |
Session Support | Not available | Supports session management via HttpSession |
Code Example of GenericServlet
public class MyGenericServlet extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.getWriter().println("This is a GenericServlet");
}
}
Code Example of HttpServlet
public class MyHttpServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().println("This is an HttpServlet handling GET");
}
}