Describe all the interface and classes present in the jakarta.servlet Package

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

InterfaceDescription
ServletThe central interface that all servlets must implement. It defines lifecycle methods like init(), service(), and destroy().
ServletRequestRepresents a client’s request to the server. Provides access to request parameters, headers, etc.
ServletResponseRepresents the response from the server to the client. Used to write output.
RequestDispatcherUsed to forward or include content from another resource (JSP or servlet).
ServletConfigProvides initialization parameters for a servlet and access to the ServletContext.
ServletContextRepresents 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.
FilterUsed to intercept requests and responses, often for logging, authentication, etc.
FilterChainAllows chaining multiple filters together.
FilterConfigProvides configuration information for a filter.
AsyncContextSupports asynchronous processing of servlet requests.
DispatcherTypeEnum used to define the type of dispatch (FORWARD, INCLUDE, REQUEST, ASYNC, ERROR).
ServletRequestAttributeListenerListens for changes to request attributes.
ServletContextListenerListens to lifecycle events of the servlet context (like startup/shutdown).
ServletRequestListenerListens to request lifecycle events.

2. Key Classes in jakarta.servlet Package

ClassDescription
GenericServletAn abstract class that implements Servlet and provides default implementations for init() and destroy(). You override service(). Used as a base for non-HTTP servlets.
ServletInputStreamAbstract class used to read binary data from a request.
ServletOutputStreamAbstract class used to send binary data in a response.
ServletContextEventUsed with context listeners to handle lifecycle events.
ServletContextAttributeEventRepresents changes to attributes in the servlet context.
ServletRequestEventEncapsulates servlet request lifecycle events.
ServletRequestAttributeEventEncapsulates 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

TypeNames
Core InterfacesServlet, ServletRequest, ServletResponse, ServletConfig, ServletContext
Dispatcher & EventsRequestDispatcher, AsyncContext, DispatcherType
Filter InterfacesFilter, FilterChain, FilterConfig
ListenersServletContextListener, ServletRequestListener, etc.
ClassesGenericServlet, ServletInputStream, ServletOutputStream

Leave a Reply

Your email address will not be published. Required fields are marked *