Servlet API Notes
Servlet API Notes
A Servlet is a Java class used to extend the capabilities of servers that host applications accessed by a
request-response model.
They are part of Java EE and typically run in servlet containers like Apache Tomcat.
Servlets process or store data submitted by users via HTML forms, generate dynamic content, and manage
Core Packages:
- javax.servlet
- javax.servlet.http
Servlets implement the javax.servlet.Servlet interface or extend HttpServlet (for HTTP-specific functionality).
A servlet's life cycle is managed by the servlet container and consists of:
3. Request Handling - service() method handles client requests. In HttpServlet, service() dispatches to
Key Methods:
- init(ServletConfig config)
- destroy()
3. Types of Servlets
1. GenericServlet:
Servlet API - Detailed Notes
2. HttpServlet:
- Extends GenericServlet.
ServletConfig:
- Example: getInitParameter("param-name")
ServletContext:
- Example: getServletContext().getInitParameter("configParam")
5. Attributes in Servlet
Attributes are used for sharing data between components within different scopes.
|----------------|----------------------------------|---------------------|
Common methods:
- setContentType("text/html")
- getWriter().println()
Redirection:
1. Client-side: response.sendRedirect("url")
Summary Table
|------------------------|----------------------------------------------------------|