Nearby lessons
4 of 34Servlet - Life Cycle
- 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:
- Servlet Loading
- Servlet Instantiation
- init() Method Execution
- service() Method Execution
Second Request Onwards:
- service() Method Execution
With load-on-startup
At Server Startup or Application Deployment:
- Servlet Loading
- Servlet Instantiation
- 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 Loading | service() Method Execution |
| Servlet Instantiation | |
| init() Method Execution | |
| service() Method Execution | |
| Second Request Onwards: | |
| service() Method Execution | service() 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)
- Client (Browser) sends a request to the server
- Server checks whether the request is for static or dynamic information based on URL
- If static: server searches for the file → provides it or returns 404
- If dynamic: web server forwards the request to the web container
- Web container identifies the corresponding servlet class based on URL pattern and web.xml
- Web container checks whether servlet object is available or not:
- If not available: loads servlet class (by
Class.forName()), creates instance (bynewInstance()), executesinit() - If available: skips to step 7
- If not available: loads servlet class (by
- Web container calls
service()method which provides the required response - 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:
- Servlet Loading
- Servlet Instantiation
- init() Method Execution
- service() Method Execution
Second Request Onwards: service() Method Execution only.
With load-on-startup (Detail)
At server startup or application deployment:
- Servlet Loading
- Servlet Instantiation
- 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 ideas of Servlet - Life Cycle explained simply
- Ready-to-use code examples
- Exam-style questions at the end