JSP custom tags in hindi-jsp कस्टम टैग्स क्या है?

हेल्लो दोस्तों ! आज इस पोस्ट में JSP custom tags in hindi के बारे में बताया गया है की क्या होता है कैसे काम करता है और भी बहुत कुछ दिया गया है तो चलिए शुरू करते है

Introduction to JSP Custom Tags

JSP custom tags बाकि standard HTML tags से अलग होते है। HTML tags browser द्वारा interpret किये जाते है और JSP custom tags web application server (JSP Engine) के द्वारा interpret किये जाते है। Interpret किये जाने पर ये tags HTML code generate करते है इसलिए end user से ये tags हमेशा hidden रहते है। JSP tags को use करना JSP page में direct java coding से कई ज्यादा आसान होता है। 

JSP custom tags को user defined tags भी कहा जाता है। ये आपके page में scriptlet tags की need को ख़त्म कर देते है। JSP custom tags की वजह से business logic (java code) और view (JSP page) separate हो जाते है जिससे उन्हें manage करना और भी आसान हो जाता है।

JSP custom tags की वजह से code re-usability बढ़ती है। आप एक ही business logic को बार बार कई JSP pages के साथ यूज़ कर सकते है। ऐसा करने से programmer का काम काफी हद तक कम हो जाता है और समय भी बचता है।

JSP Custom Tag API

JSP API में javax.servlet.jsp.tagext package होता है। इस package में JSP custom tags के support के लिए classes और interface available होते है। इसे JSP custom tag API भी कहते है। इस package की classes एक hierarchy form करती है जिसे निचे दिया जा रहा है।

Description: http://web.archive.org/web/20180303002201im_/https:/3.bp.blogspot.com/-SShkD9KwJAg/WLWK5hlNFZI/AAAAAAAACvY/Ew_8emQtWggiNxwURxq2Bv6F6BiKfUCogCLcB/s320/JSP-Custom-Tags-in-Hindi.png

इस hierarchy में represent किये गए सभी interfaces और classes के बारे में निचे detail से दिया जा रहा है।  

JspTag Interface

JSP custom tag API की hierarchy में JspTag root interface है। ये Tag interface के लिए base class की तरह काम करता है। ये interface बाकि interfaces, classes और methods के organization और type safety purposes के लिए होता है। 

Tag Interface 

ये JspTag interface का sub tag होता है। ये interface custom tags के शुरू में या अंत में action perform करने के लिए methods provide करता है। ये interface आपको 2 important methods provide करता है जिन्हें tag handler class में implement किया जाता है।   

  1. doStartTag()
  2. doEndTag()     

ये methods JSP custom tags की life cycle form करते है। आखिरी method के call होने पर control handler को pass हो जाता है।

IterationTag Interface

IterationTag interface Tag interface को extend करता है। ये interface एक additional method define करता है। ये additional method doAfterBody() होता है। ये method body के reevaluation को control करता है। यदि doStartTag() method EVAL_BODY_AGAIN return करता है तो tag की body दुबारा evaluate की जाती है। यदि ये methods SKIP_BODY return करता है तो body skip हो जाती है और doEndTag() evaluate होता है।      

TagSupport Class

ये class IterationTag interface को implement करती है। ये class new tag handlers को define करने के लिए यूज़ की जाती है। जब आप tag handler लिखेंगे तो इसी class को implement करेंगे। ये class tag में attributes के लिए getter और setter define करने की facility provide करती है। 

BodyTag Interface

JSP custom tag API में BodyTag interface custom tags की body content को manipulate करने के लिए methods provide करता है। उदाहरण के लिए आप getString() method के द्वारा body content को convert कर सकते है।

BodyTagSupport

BodyTagSupport class TagSupport class को extend और BodyTag को implement करती है। ये Tag body content को support के लिए additional methods provide करती है। 

Creating Custom JSP Tags

JSP में custom tags create करने के 3 steps होते है। 

  1. Tag handler class create करिये और tag के start या end पर action perform करिये।  
  2. TLD (Tag Library Descriptor) file create कीजिये और tags define कीजिये। 
  3. एक JSP file create कीजिये जो TLD file में define किये गए tags को यूज़ करती है।   

इन steps को निचे उदाहरण के द्वारा समझाया जा रहा है।

Create Tag Handler Class

Tag Handler class वो class होती है जिसमे आप tag का action define करते है। इस class में आप javax.servlet.jsp.tagext package को import करेंगे। साथ ही आप servlet-api .jar और jsp-api.jar files को भी अपने project में include करेंगे। 

मान लीजिये आप अपने custom tag के द्वारा current date display करवाना चाहते है तो इसके लिए Handler class इस प्रकार create करेंगे। 

import javax.servlet.jsp.tagext.*; class MyTagHandler extends TagSupport {     public static int doStartTag()     {           JspWriter out = pageContext.getout();             out.println(Calender.getInstance().getTime());      } } 

ऊपर दिए गए उदाहरण में JspWriter class का object JSP file से output display करने के लिए यूज़ किया गया है।

Create TLD (Tag Library Descriptor) File & Define Tags

TLD file में आप अपने custom tags define करते है। इसके लिए निचे कुछ important tags के बारे में बताया जा रहा है।

  1. <tag> – ये custom define करने के लिए root tag होता है। इसमें आप <name> और <tag-class> दो sub tags define करते है।       
  2. <name> – इस tag के द्वारा आप custom tag का नाम define करते है।  
  3. <tag-class> – इस tag के द्वारा आप custom tag की action handler class define करते है।  


उदाहरण के लिए आप एक date tag define करना चाहते है और ऊपर create की गयी handler class को यूज़ करते हुए current date display करता है तो ऐसा आप इस प्रकार कर सकते है। 

<tag-lib>
<tlib-version>1.0></tlib-version> <jsp-version>1.2</tlib-version> <short-name>Simple</short-name>
<tag> <name>TodayDate</name> <tag-class>com.MyPackage.MyHandlerClass</tag-class> </tag>   </tag-lib>
 

Create JSP File That Uses Custom Tags

Custom tags को आप अपने किसी JSP page में यूज़ करेंगे। इसके लिए सबसे पहले आप अपनी TLD file को taglib directive के द्वारा उस JSP file में include करवाएंगे। इसके बाद आप define किये गए prefix को यूज़ करते हुए उस page में TLD file के tags को यूज़ कर सकते है।

उदाहरण के लिए मान लीजिये आप अपने JSP page में ऊपर दी गयी TLD file को include करना चाहते है। ऐसा आप इस प्रकार कर सकते है।

<%@ taglib uri="Project1/TagLibrary/MyTaglibrary.tld" prefix="x" %>
<html> <head> <title>Custom Tag Demo</title> </head>
<body>
Date is : <TodayDate></TodayDate>
</body>
</html> 

ऊपर दिए गए उदाहरण में custom tag <TodayDate> के द्वारा current date display की जा रही है।

reference-https://www.tutorialspoint.com/jsp/jsp_custom_tags.htm

निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic (JSP custom tags in hindi) अच्छा लगा हो तो कृपया आप इस वेबसाइट के बारे में अपने दोस्तों को जरुर बताये अगर कोई topic(JSP custom tags in hindi) से संबधित प्रश्न हो तो कमेंट्स(comments) आपके लिए ही बना है और किसी Subject( JSP custom tags in hindi) के लेकर भी कोई प्रश्न( JSP custom tags in hindi) हो तो कमेंट करे

Leave a Comment