Describe all the interface and classes present in the jakarta.servlet Package
Answer:-
jakarta.servlet Package – Overview
The jakarta.servlet package is part of the Jakarta EE (formerly Java EE) platform. It provides the core interfaces and classes for building Java-based web applications, especially Servlets.
Servlets are Java classes used to handle HTTP requests and responses on the server side.
1. Key Interfaces in jakarta.servlet Package
| Interface | Description |
|---|---|
| Servlet | The central interface that all servlets must implement. It defines lifecycle methods like init(), service(), and destroy(). |
| ServletRequest | Represents a client’s request to the server. Provides access to request parameters, headers, etc. |
| ServletResponse | Represents the response from the server to the client. Used to write output. |
| RequestDispatcher | Used to forward or include content from another resource (JSP or servlet). |
| ServletConfig | Provides initialization parameters for a servlet and access to the ServletContext. |
| ServletContext | Represents the web application environment. Shared among all servlets. Useful for global attributes. |
| SingleThreadModel (Deprecated) | Was used to indicate that a servlet handles one request at a time. Now obsolete. |
| Filter | Used to intercept requests and responses, often for logging, authentication, etc. |
| FilterChain | Allows chaining multiple filters together. |
| FilterConfig | Provides configuration information for a filter. |
| AsyncContext | Supports asynchronous processing of servlet requests. |
| DispatcherType | Enum used to define the type of dispatch (FORWARD, INCLUDE, REQUEST, ASYNC, ERROR). |
| ServletRequestAttributeListener | Listens for changes to request attributes. |
| ServletContextListener | Listens to lifecycle events of the servlet context (like startup/shutdown). |
| ServletRequestListener | Listens to request lifecycle events. |
2. Key Classes in jakarta.servlet Package
| Class | Description |
|---|---|
| GenericServlet | An abstract class that implements Servlet and provides default implementations for init() and destroy(). You override service(). Used as a base for non-HTTP servlets. |
| ServletInputStream | Abstract class used to read binary data from a request. |
| ServletOutputStream | Abstract class used to send binary data in a response. |
| ServletContextEvent | Used with context listeners to handle lifecycle events. |
| ServletContextAttributeEvent | Represents changes to attributes in the servlet context. |
| ServletRequestEvent | Encapsulates servlet request lifecycle events. |
| ServletRequestAttributeEvent | Encapsulates changes to attributes in the request scope. |
3. Life Cycle of a Servlet (using interfaces)
- Initialization:
init(ServletConfig config) - Handling Request:
service(ServletRequest req, ServletResponse res) - Destruction:
destroy()
4. Example Servlet using jakarta.servlet Interfaces
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class MyServlet extends GenericServlet {
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Hello from Jakarta Servlet</h2>");
}
}
Summary Table
| Type | Names |
|---|---|
| Core Interfaces | Servlet, ServletRequest, ServletResponse, ServletConfig, ServletContext |
| Dispatcher & Events | RequestDispatcher, AsyncContext, DispatcherType |
| Filter Interfaces | Filter, FilterChain, FilterConfig |
| Listeners | ServletContextListener, ServletRequestListener, etc. |
| Classes | GenericServlet, ServletInputStream, ServletOutputStream |
