Nearby lessons

4 of 34

Servlet - Life Cycle

📌 What You Will Learn
  • Understand First Request
  • Understand Second Request onwards
  • Understand Second Request
  • Understand Life Cycle of Servlet that implements Servlet interface
  • See complete working code examples

Life Cycle is an essential part of the Java Servlet technology. This lesson explains First Request, Second Request onwards and Second Request with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Without load-on-startup

First Request:

  1. Servlet Loading
  2. Servlet Instantiation
  3. init() Method Execution
  4. service() Method Execution

Second Request Onwards:

  • service() Method Execution

With load-on-startup

At Server Startup or Application Deployment:

  1. Servlet Loading
  2. Servlet Instantiation
  3. init() Method Execution

First Request (and onwards):

  • service() Method Execution

Comparison: Without vs With load-on-startup

Without <load-on-startup>With <load-on-startup>
At Server Startup / Deployment:
Servlet Loading
Servlet Instantiation
init() Method Execution
First Request:
Servlet Loadingservice() Method Execution
Servlet Instantiation
init() Method Execution
service() Method Execution
Second Request Onwards:
service() Method Executionservice() Method Execution

Multiple URL Patterns (Servlet 2.5+)

From Servlet 2.5 Version onwards we can define multiple url-patterns for the same servlet in web.xml:

<web-app>
  .....
  <servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/test</url-pattern>
    <url-pattern>/demo</url-pattern>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

All three URLs will invoke the same servlet:

  • http://localhost:7777/advapps1A/test
  • http://localhost:7777/advapps1A/demo
  • http://localhost:7777/advapps1A/hello

Life Cycle of Servlet (Implementing Servlet Interface)

  1. Client (Browser) sends a request to the server
  2. Server checks whether the request is for static or dynamic information based on URL
  3. If static: server searches for the file → provides it or returns 404
  4. If dynamic: web server forwards the request to the web container
  5. Web container identifies the corresponding servlet class based on URL pattern and web.xml
  6. Web container checks whether servlet object is available or not:
    • If not available: loads servlet class (by Class.forName()), creates instance (by newInstance()), executes init()
    • If available: skips to step 7
  7. Web container calls service() method which provides the required response
  8. Finally web container calls destroy() method to perform cleanup (at undeployment or shutdown)

Flow Chart Summary

Key points:

  • Steps 1-6 (loading, instantiation, init) happen at the time of first request (without load-on-startup)
  • If <load-on-startup> is configured, these steps happen at server startup or deployment
  • destroy() is called at application undeployment or server shutdown

Without load-on-startup (Detail)

At the time of first request:

  1. Servlet Loading
  2. Servlet Instantiation
  3. init() Method Execution
  4. service() Method Execution

Second Request Onwards: service() Method Execution only.

With load-on-startup (Detail)

At server startup or application deployment:

  1. Servlet Loading
  2. Servlet Instantiation
  3. init() Method Execution

First Request onwards: service() Method Execution only.

Note: At the time of application undeployment or server shutdown, destroy() method will be executed.

📝 Key Takeaways
  • Key ideas of Servlet - Life Cycle explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4