Servlet API Summary

This table provides a comprehensive summary of key Servlet API classes, detailing when objects are created, who creates them, how many times they are created, and when they are invoked, along with code snippets for better understanding.

Class/Interface When Object is Created Who Creates the Object How Many Times Created When It Is Invoked Code Snippet
Servlet When the servlet is loaded for the first time. Servlet Container Once per servlet definition. When a request matching the servlet’s URL pattern is received. public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) {
        // Logic here
    }
}
HttpServletRequest For every client request. Servlet Container Once per request. When a request is made by the client. @Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) {
    String parameter = req.getParameter("name");
}
HttpServletResponse For every client request. Servlet Container Once per request. When a response is sent to the client. @Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<h1>Hello, World!</h1>");
}
HttpSession When a new session is created. Servlet Container One per user session. When session-specific data needs to be stored or retrieved. HttpSession session = req.getSession();
session.setAttribute("user", "John Doe");
ServletConfig When the servlet is initialized. Servlet Container Once per servlet. When servlet-specific initialization parameters are needed. @Override
public void init(ServletConfig config) {
    String param = config.getInitParameter("paramName");
}
ServletContext When the web application is started. Servlet Container Once per web application. When application-wide parameters or resources are needed. ServletContext context = getServletContext();
String appName = context.getInitParameter("appName");
Cookie When a developer creates it in response to a request. Developer As many as needed per request/response. When client-specific data needs to persist across multiple requests. Cookie cookie = new Cookie("user", "John Doe");
res.addCookie(cookie);
ServletListener When the servlet context is initialized or destroyed. Servlet Container Once per context initialization or destruction. When the servlet container starts or stops the web application. public class MyListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Context Initialized");
    }
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Context Destroyed");
    }
}
Filter When a request is passed through a filter. Servlet Container Once per request/response cycle. When modifying request or response objects before passing them to the servlet or filter chain. public class MyFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Filtering request...");
        chain.doFilter(request, response);
    }
}