Feedback Form

Custom Tag Lifecycle: Tag Handler init(), doTag(), release()

Custom Tag Lifecycle in JSP: Tag Handler init(), doTag(), release()

जब हम JSP (Java Server Pages) में Custom Tag बनाते हैं, तो उसका lifecycle (जीवनचक्र) बिल्कुल वैसे ही काम करता है जैसे किसी servlet या JSP page का होता है। लेकिन यहाँ खास बात यह है कि custom tag का lifecycle तीन मुख्य methods से control किया जाता है — init(), doTag() और release()। ये तीनों methods tag handler class में define होते हैं और tag के execution के दौरान JSP container द्वारा call किए जाते हैं।

Introduction to Custom Tag Lifecycle

JSP में custom tag का use कोड को reusable और readable बनाने के लिए किया जाता है। जब भी कोई JSP page load होता है जिसमें custom tag use हुआ है, container सबसे पहले उस tag से जुड़ी Tag Handler Class को initialize करता है, फिर tag body को execute करता है और अंत में resources को free करता है। इस पूरे process को ही custom tag lifecycle कहा जाता है।

Custom Tag Lifecycle के तीन चरण:

  • Initialization Phase – जहाँ tag handler initialize होता है (init() method)
  • Execution Phase – जहाँ tag की actual logic चलती है (doTag() method)
  • Cleanup Phase – जहाँ memory और resources release किए जाते हैं (release() method)

1. init() Method

init() method custom tag lifecycle का पहला step है। इस method को JSP container तब call करता है जब tag handler object पहली बार बनाया जाता है। इसका main काम tag से जुड़ी initial setup और configuration को तैयार करना होता है। जैसे किसी servlet में init() method initialization करता है, वैसे ही यहाँ भी करता है।

init() Method का उपयोग:

  • Initial values या attributes set करने के लिए।
  • Database connection या resource lookup करने के लिए।
  • Logging या tracking शुरू करने के लिए।

init() Method का Example:

public class MyCustomTag extends SimpleTagSupport {
  public void init() {
    System.out.println("Tag initialized...");
  }
}

ऊपर दिए गए example में, जैसे ही JSP page में यह tag load होगा, container init() method को call करेगा और initialization process शुरू हो जाएगी।

2. doTag() Method

यह method custom tag का core part होता है। जब JSP container tag को execute करता है, तब doTag() method call होता है। यह method define करता है कि tag क्या काम करेगा — यानी उसकी main functionality। इसे tag handler class में override किया जाता है।

doTag() Method का काम:

  • Tag body को process करना।
  • Dynamic content generate करना।
  • Output को JSP writer पर भेजना।

doTag() Method का Example:

public class MyCustomTag extends SimpleTagSupport {
  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.println("Hello from Custom Tag!");
  }
}

यहाँ doTag() method JSP page पर “Hello from Custom Tag!” print करेगा। जब JSP container इस tag को execute करेगा, तो यही output page पर दिखाई देगा।

doTag() Method में Important Points:

  • doTag() method में आप JSP context access कर सकते हैं।
  • Tag body को dynamically evaluate किया जा सकता है।
  • इस method में business logic, database interaction या output generation का code लिखा जा सकता है।

3. release() Method

जब tag का execution पूरा हो जाता है और JSP container को उस tag की आवश्यकता नहीं रहती, तब release() method call किया जाता है। यह method cleanup phase का हिस्सा होता है। इसका main काम memory और resources को free करना होता है ताकि server performance बेहतर बनी रहे।

release() Method का काम:

  • Database connection बंद करना।
  • File या stream resources release करना।
  • Variables को null करना ताकि garbage collector उन्हें हटा सके।

release() Method का Example:

public class MyCustomTag extends SimpleTagSupport {
  public void release() {
    System.out.println("Resources released...");
  }
}

जब JSP page पूरी तरह process हो जाता है, तब container release() method को call करता है और सभी resources को साफ करता है। इससे memory leak की समस्या नहीं होती।

Complete Custom Tag Lifecycle Flow

अब आइए देखें कि ये तीनों methods actual execution order में कैसे काम करते हैं।

Step Method Purpose
1 init() Tag Handler को initialize करना और resources allocate करना
2 doTag() Tag की main logic execute करना और output generate करना
3 release() Resources को release करना और memory साफ करना

Lifecycle का Example Flow:

init() → doTag() → release()

हर बार जब JSP page पर custom tag run होता है, यही sequence follow होता है। इस sequence से clear होता है कि हर tag अपने initialization से लेकर cleanup तक एक defined lifecycle follow करता है।

Tag Handler Class Structure

Custom tag के लिए एक tag handler class बनाई जाती है जो SimpleTagSupport या TagSupport class को extend करती है। इसमें ऊपर बताए गए तीन methods को override किया जा सकता है।

Example of Complete Custom Tag Handler:

public class MyCustomTag extends SimpleTagSupport {
  public void init() {
    System.out.println("Initialization done...");
  }

  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.println("Executing custom tag logic...");
  }

  public void release() {
    System.out.println("Cleanup complete...");
  }
}

ऊपर के code में तीनों lifecycle methods override किए गए हैं — जिससे tag initialization, execution और cleanup तीनों stages पर control मिलता है।

Real-World Example: Displaying Current Date

अब एक practical example देखें जो custom tag के तीनों methods का use करता है और JSP page पर current date show करता है।

public class DateTag extends SimpleTagSupport {
  public void init() {
    System.out.println("DateTag initialized");
  }

  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    java.util.Date date = new java.util.Date();
    out.println("Current Date: " + date.toString());
  }

  public void release() {
    System.out.println("DateTag resources released");
  }
}

यह tag JSP page पर हर बार current date print करेगा और साथ ही console में initialization और release message भी दिखाएगा।

Important Points for Exams

  • init() method tag handler object के creation पर call होता है।
  • doTag() method tag की main functionality execute करता है।
  • release() method tag execution के बाद resources free करता है।
  • Custom tag की lifecycle JSP container द्वारा manage की जाती है।
  • Custom tags reusable और modular web components बनाने में help करते हैं।

Summary Table

Method Phase Description
init() Initialization Tag handler setup और resource allocation
doTag() Execution Tag की main processing logic
release() Cleanup Resources release और memory cleanup

Notes for Students

  • Exam में custom tag lifecycle का question short answer या 5 marks question में आता है।
  • Sequence हमेशा याद रखें — init() → doTag() → release()
  • हर method का main purpose clear रखें — initialization, execution और cleanup।
  • अगर example पूछे जाएँ, तो simple output generating tag का example दें।
  • Custom tag से related classes: SimpleTagSupport, TagSupport, JspContext