0% found this document useful (0 votes)
13 views3 pages

Servlet API Notes

Uploaded by

Suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Servlet API Notes

Uploaded by

Suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Servlet API - Detailed Notes

1. Servlet API Introduction

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

state across multiple server requests.

Core Packages:

- javax.servlet

- javax.servlet.http

Servlets implement the javax.servlet.Servlet interface or extend HttpServlet (for HTTP-specific functionality).

2. Servlet Life Cycle

A servlet's life cycle is managed by the servlet container and consists of:

1. Loading and Instantiation - Class is loaded and an instance is created.

2. Initialization - init() method is called once to initialize the servlet.

3. Request Handling - service() method handles client requests. In HttpServlet, service() dispatches to

doGet(), doPost(), etc.

4. Destruction - destroy() method is called once when servlet is being removed.

Key Methods:

- init(ServletConfig config)

- service(ServletRequest req, ServletResponse res)

- destroy()

3. Types of Servlets

1. GenericServlet:
Servlet API - Detailed Notes

- Protocol-independent abstract class.

- Implements Servlet and ServletConfig interfaces.

2. HttpServlet:

- Extends GenericServlet.

- Designed to handle HTTP requests.

- Common methods: doGet(), doPost(), doPut(), doDelete().

4. Servlet Configuration and Context

ServletConfig:

- One per servlet instance.

- Used to pass initialization parameters.

- Example: getInitParameter("param-name")

ServletContext:

- One per web application.

- Used for application-wide parameters, resource access, and logging.

- Shared across all servlets.

- Example: getServletContext().getInitParameter("configParam")

5. Attributes in Servlet

Attributes are used for sharing data between components within different scopes.

| Scope | Method | Lifespan |

|----------------|----------------------------------|---------------------|

| Request Scope | request.setAttribute() | Per request |

| Session Scope | session.setAttribute() | Per user session |

| Context Scope | context.setAttribute() | Application-wide

6. Response and Redirection Objects


Servlet API - Detailed Notes

HttpServletResponse is used to send data back to the client.

Common methods:

- setContentType("text/html")

- getWriter().println()

Redirection:

1. Client-side: response.sendRedirect("url")

- Browser URL changes.

2. Server-side: RequestDispatcher.forward(request, response)

- Internal forward, browser URL remains unchanged.

Summary Table

| Topic | Key Point |

|------------------------|----------------------------------------------------------|

| Servlet API | Handles server-side requests using Java classes |

| Servlet Life Cycle | init(), service(), destroy() |

| Types | GenericServlet, HttpServlet |

| Config vs Context | Config = per servlet, Context = application-wide |

| Attributes | Request, Session, Context scopes |

| Response & Redirection | sendRedirect (client), forward (server)

You might also like