Nearby lessons
2 of 34Servlet - API (Servlet Interface)
- Understand the Servlet API package structure
- Learn the Servlet interface and its 5 lifecycle methods
- See complete working code examples
Servlet API defines the classes and interfaces used for developing servlets. This lesson explains the Servlet API hierarchy, the Servlet interface lifecycle, and provides complete runnable code examples.
Servlet API Packages
The Servlet API defines two main packages:
- javax.servlet — Defines classes and interfaces for protocol-independent servlets (Generic Servlets)
- javax.servlet.http — Defines classes and interfaces for HTTP protocol-based servlets
Important interfaces of javax.servlet package:
| # | Interface | Purpose |
|---|---|---|
| 1 | Servlet | Every servlet must implement this — defines lifecycle methods |
| 2 | ServletRequest | Holds end-user provided information for each request |
| 3 | ServletResponse | Used to prepare and send response to end user |
| 4 | ServletConfig | Per-servlet configuration information |
| 5 | ServletContext | Per-web-application configuration information |
| 6 | RequestDispatcher | Dispatches request from one servlet to another |
| 7 | SingleThreadModel | Marker interface for single-threaded servlet access (deprecated) |
Important classes of javax.servlet package:
| # | Class | Purpose |
|---|---|---|
| 1 | GenericServlet | Base class for protocol-independent servlets |
| 2 | ServletInputStream | Read binary data sent by end user |
| 3 | ServletOutputStream | Write binary data to the response |
| 4 | ServletException | Thrown when servlet faces a problem during request processing |
javax.servlet.http Package
The javax.servlet.http package provides HTTP-specific servlet classes and interfaces. It extends the generic servlet types with HTTP methods and session management.
ServletRequest Interface
For every request, the web container creates one ServletRequest object. This object holds end-user provided information such as form fields, query parameters, and headers.
The servlet can use this request object to retrieve all information submitted by the client.
ServletResponse & ServletConfig Interfaces
ServletResponse (I): For every request, the web container creates a response object. The servlet can use this response object to prepare and send a response to the end user.
ServletConfig (I): For every servlet, the web container creates a separate config object to hold its configuration information. The servlet can use this config object to get its configuration information.
Note: ServletConfig is per servlet, whereas ServletContext is per web application.
ServletContext & RequestDispatcher Interfaces
ServletContext (I): For every web application, the web container creates a separate context object to hold application-level configuration information. The servlet can use this context object to get application-level configuration information.
RequestDispatcher (I): We can use RequestDispatcher to dispatch a request from one servlet to another servlet. This enables request forwarding and includes.
SingleThreadModel Interface
A single servlet object can be accessed by multiple threads simultaneously, which may cause data inconsistency problems — servlets are not thread-safe by default.
If a servlet class implements SingleThreadModel, then the servlet object can be accessed by only one thread at a time.
Advantages:
- Threads are executed one by one, resolving data inconsistency problems
Disadvantages:
- Increases thread waiting time and creates performance problems
SingleThreadModel is deprecated in Servlet 2.4. Instead, use the synchronized keyword. SingleThreadModel is a marker interface — it does not contain any methods; the JVM provides the required ability internally.
Servlet Interface — 5 Methods
Every servlet in Java must implement the Servlet interface either directly or indirectly. The Servlet interface defines the following 5 methods:
| Method | Signature | Called By |
|---|---|---|
| init() | public void init(ServletConfig config) throws ServletException | Web container (once) |
| service() | public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException | Web container (every request) |
| destroy() | public void destroy() | Web container (once) |
| getServletConfig() | public ServletConfig getServletConfig() | Programmer |
| getServletInfo() | public String getServletInfo() | Programmer |
init() method: Called automatically by the web container after servlet instantiation immediately to perform initialization activities. Once init() completes, the servlet is in a position to provide service.
service() and destroy() Methods
service() method: Executed automatically by the web container for every request to provide the required response. All service logic must be written in this method.
destroy() method: Executed only once by the web container to perform cleanup activities just before taking the servlet object out of service. Once destroy() completes, the web container destroys that servlet object. This usually happens at server shutdown or application undeployment.
Note: init(), service(), and destroy() are called lifecycle methods of the servlet.
getServletConfig(): Returns the ServletConfig object. By using this object, the servlet can get its configuration information.
getServletInfo() and Callback Methods
getServletInfo(): Returns information about the servlet such as author, version, and copyright information.
Callback vs Inline Methods:
| Type | Methods | Explanation |
|---|---|---|
| Callback methods | init(), service(), destroy() | Executed automatically by the web container |
| Inline methods | getServletConfig(), getServletInfo() | Called explicitly by the programmer based on requirement |
Steps to develop a first web application:
- Step 1: Develop a servlet by implementing the Servlet interface — provide implementation for all 5 methods
- javax.servlet and javax.servlet.http package hierarchy
- Servlet interface lifecycle: init(), service(), destroy()
- Ready-to-use code examples
- Exam-style questions at the end