Nearby lessons

2 of 34

Servlet - API (Servlet Interface)

📌 What You Will Learn
  • 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:

#InterfacePurpose
1ServletEvery servlet must implement this — defines lifecycle methods
2ServletRequestHolds end-user provided information for each request
3ServletResponseUsed to prepare and send response to end user
4ServletConfigPer-servlet configuration information
5ServletContextPer-web-application configuration information
6RequestDispatcherDispatches request from one servlet to another
7SingleThreadModelMarker interface for single-threaded servlet access (deprecated)

Important classes of javax.servlet package:

#ClassPurpose
1GenericServletBase class for protocol-independent servlets
2ServletInputStreamRead binary data sent by end user
3ServletOutputStreamWrite binary data to the response
4ServletExceptionThrown when servlet faces a problem during request processing
Example01
JCode Cell
1 
2javax.servlet package
3javax.servlet.http package
4javax.servlet:
5

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.

Example02
JCode Cell
1 
2javax.servlet.http :
3

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.

Example03
JCode Cell
1 
2ServletRequest(I):
3

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.

Example04
JCode Cell
1 
2ServletResponse(I):
3

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.

Example05
JCode Cell
1 
2ServletContext(I):
3

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.

Example06
JCode Cell
1 
2SingleThreadModel(I):
3

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:

MethodSignatureCalled By
init()public void init(ServletConfig config) throws ServletExceptionWeb container (once)
service()public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOExceptionWeb 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.

Example07
JCode Cell
1 
2service() method:
3

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.

Example08
JCode Cell
1 
2getServletInfo()
3

getServletInfo() and Callback Methods

getServletInfo(): Returns information about the servlet such as author, version, and copyright information.

Callback vs Inline Methods:

TypeMethodsExplanation
Callback methodsinit(), service(), destroy()Executed automatically by the web container
Inline methodsgetServletConfig(), 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
Example09
JCode Cell
1 
2getServletInfo()
3
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4