JSP Life Cycle: From Page Load to Unload
JSP Life Cycle: From Page Load to Unload
JSP (Java Server Pages) का Life Cycle समझना बहुत ज़रूरी है अगर आप Web Application Development सीख रहे हैं या Exam की तैयारी कर रहे हैं। JSP एक Server-Side Technology है जो Dynamic Web Pages बनाने के लिए use होती है। जब भी कोई Client (जैसे Browser) JSP Page को Request करता है, तो JSP Container उसके पीछे बहुत सारी processes चलाता है। इन्हीं processes को हम JSP Life Cycle कहते हैं।
What is JSP Life Cycle?
JSP Life Cycle का मतलब है – एक JSP Page कब और कैसे Load होता है, Execute होता है और फिर Unload होता है। इसमें JSP Page के Creation से लेकर उसके Destruction तक के सारे steps शामिल होते हैं। जब कोई User JSP Page को Access करता है, तो Server उस Page को Compile करता है, Run करता है और फिर Response को Client को भेजता है।
JSP का पूरा Life Cycle Servlet Architecture पर आधारित होता है। मतलब JSP internally Servlet में Convert होता है और वही Servlet Execute होता है। JSP का Life Cycle पाँच मुख्य Stages में Divide किया गया है।
JSP Life Cycle Stages
JSP Life Cycle में पाँच Main Phases होते हैं, जो इस तरह हैं:
- 1. Translation Phase – JSP को Servlet में Convert किया जाता है।
- 2. Compilation Phase – Servlet Code को Compile किया जाता है।
- 3. Initialization Phase – JSP को Initialize किया जाता है।
- 4. Execution Phase – JSP Request को Process करता है और Response Generate करता है।
- 5. Destruction Phase – JSP को Memory से Remove किया जाता है।
1. Translation Phase
जब कोई JSP Page पहली बार Server पर Load होता है, तो JSP Engine उस Page को Java Servlet Code में Convert करता है। यह Process Translation कहलाती है। Example के लिए:
index.jsp → index_jsp.java
इस Stage में JSP के अंदर लिखे गए HTML, Java Code, JSP Tags, और Directives को Servlet Compatible Java Code में बदल दिया जाता है।
2. Compilation Phase
Translation के बाद, Generated Java File (Servlet Code) को Compile किया जाता है ताकि वह Bytecode में बदल जाए। इसका Result एक .class file होता है। Example:
index_jsp.java → index_jsp.class
यह Compiled Class File अब JSP के Execution के लिए Ready होती है। यह वही Servlet होता है जो Server पर Run करता है।
3. Initialization Phase
जब JSP पहली बार Run होता है, तो Container उस Servlet Class को Load करता है और उसका Object Create करता है। फिर jspInit() Method Call किया जाता है। यह Method सिर्फ एक बार JSP के Life Cycle में चलता है, जब JSP पहली बार Initialize होता है।
Initialization Phase का काम JSP Page को Execution के लिए Prepare करना होता है। जैसे Database Connection Establish करना, Configuration Settings Load करना आदि।
public void jspInit() {
// Initialization code like DB connection, loading config
}
4. Execution Phase
Initialization के बाद JSP को Client से Request मिलती है। इस Request को Process करने के लिए _jspService() Method Call होता है। यह JSP का सबसे Important Method है क्योंकि यही Method Dynamic Content Generate करता है।
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Request Handling and Response Generation Code
}
इस Method में JSP के अंदर लिखा गया HTML और Java Code Mix होकर Final Output बनाता है, जिसे Server Client को भेज देता है। हर बार जब कोई JSP Request होती है, तो _jspService() Method Call होता है।
5. Destruction Phase
जब JSP Page की जरूरत नहीं रहती, तो Container उस JSP Servlet Object को Destroy कर देता है। इस Process में jspDestroy() Method Call होता है। यह Method JSP Page के End होने से पहले Resource Clean-up करने के लिए Use होता है।
public void jspDestroy() {
// Release resources like DB connections, file handles
}
इस Stage के बाद JSP Page का Life Cycle समाप्त हो जाता है।
JSP Life Cycle Flow Diagram (Conceptually)
JSP Life Cycle को समझने के लिए नीचे एक Simple Flow देखें:
| Stage | Process | Method |
|---|---|---|
| Translation | JSP to Servlet Conversion | - |
| Compilation | Servlet Compilation | - |
| Initialization | Servlet Object Creation | jspInit() |
| Execution | Request Processing and Response Generation | _jspService() |
| Destruction | Resource Cleanup | jspDestroy() |
Important JSP Life Cycle Methods
JSP Life Cycle में तीन Important Methods होते हैं जो JSP Page के Execution को Control करते हैं:
- jspInit() – JSP के Load होने पर एक बार Call होता है। Initialization Code यहीं लिखा जाता है।
- _jspService() – हर Request पर Call होता है और Response Generate करता है।
- jspDestroy() – JSP के Unload होने से पहले Resource Clean-up के लिए Call होता है।
इन Methods को JSP Developer Override नहीं कर सकता, लेकिन Initialization और Destruction में Custom Code Add कर सकता है।
JSP Internal Architecture
जब JSP Run होता है, तो उसके पीछे JSP Container और Servlet Engine मिलकर काम करते हैं। JSP Container Request को Handle करने के लिए निम्न Steps Follow करता है:
- Client JSP Page को Request करता है।
- JSP Container Check करता है कि क्या JSP पहले से Compile है।
- अगर नहीं, तो Translation और Compilation Phases Run होते हैं।
- Generated Servlet Class Load होती है।
- Initialization → Execution → Destruction Steps Sequentially Execute होते हैं।
इस तरह हर JSP Request के पीछे Servlet Engine Responsible होता है जो JSP को Efficiently Manage करता है।
Example: JSP Life Cycle Practical Example
मान लो हमारे पास एक Simple JSP File है – hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h3>Welcome to JSP Life Cycle Example</h3>
<%
out.println("Current Time: " + new java.util.Date());
%>
</body>
</html>
जब यह JSP पहली बार Run होती है:
- यह Servlet में Translate होती है।
- Servlet Compile होकर
.classFile बनाता है। jspInit()Method Call होता है।_jspService()Method Response Generate करता है।- जब JSP Unload होती है, तब
jspDestroy()Method Call होता है।
Why JSP Life Cycle is Important?
JSP Life Cycle को समझने के कई Practical Benefits हैं:
- आपको पता चलता है कि Server JSP को कैसे Process करता है।
- Performance Optimization के लिए Useful है।
- Resource Management बेहतर तरीके से किया जा सकता है।
- Debugging आसान होती है क्योंकि आपको Internal Flow पता होता है।
JSP vs Servlet in Life Cycle
JSP और Servlet दोनों का Life Cycle लगभग समान होता है, बस JSP Developer-Friendly Format में होता है। नीचे तुलना देखें:
| Parameter | JSP | Servlet |
|---|---|---|
| Translation | JSP to Servlet में Convert होता है | Already Java Code में होता है |
| Initialization Method | jspInit() | init() |
| Service Method | _jspService() | service() |
| Destruction Method | jspDestroy() | destroy() |
Key Points to Remember for Exams
- JSP Life Cycle के पाँच Stages होते हैं।
- JSP internally Servlet में Convert होता है।
- jspInit(), _jspService(), jspDestroy() Methods Core Part हैं।
- हर Request पर केवल _jspService() Method Call होता है।
- Initialization और Destruction सिर्फ एक बार होते हैं।
Short Notes for Quick Revision
- Translation: JSP → Servlet Conversion
- Compilation: Servlet → Bytecode (.class)
- Initialization: jspInit() → Prepare Resources
- Execution: _jspService() → Handle Request
- Destruction: jspDestroy() → Release Resources
JSP Life Cycle की यह पूरी Journey JSP के Internal Working को समझने में मदद करती है। अगर आप Exam की तैयारी कर रहे हैं, तो हर Stage का Function और Method याद रखें। यही Concepts Practical Development और Interview दोनों में काम आते हैं।