Tutorial: Create JSP custom tag library

A Custom tag is a user defined JSP language element. When a JSP page containing custom tag is translated into a Servlet, the tag is converted to operations on an object called tag handler. The Web container then invokes those operations when the JSP page’s servlet is executed. It speeds up web application development because of code reuse feasibility. Custom tags can access all the objects available in JSP pages. Custom tags can modify the response generated by the calling page. Custom tags can be nested. Custom tag library consists of one or more Java classes called Tag Handlers and an XML tag library descriptor file (tag library). A class which has to be a tag handler needs to implement Tag interface or IterationTag interface or BodyTag interface or it can also extend TagSupport class or BodyTagSupport class. All the classes that support custom tags are present inside a package called javax.servlet.jsp.tagext. Let us create a custom tag which will does substring operation on given input. For this we will first create our Tag Handler class. Create a class called SubstrTagHandler and copy/paste following code in it.
package net.viralpatel.jsp.custom.taglib; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class SubstrTagHandler extends TagSupport { private String input; private int start; private int end; @Override public int doStartTag() throws JspException { try { //Get the writer object for output. JspWriter out = pageContext.getOut(); //Perform substr operation on string. out.println(input.substring(start, end)); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } public String getInput() { return input; } public void setInput(String input) { this.input = input; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } }
Code language: Java (java)
In above code we have created three attributes: input, start and end. These are the inputs that we will get when the custom tag will be invoked from a JSP file. Also note that, these attributes have getter and setter methods which will be used to set the property values. Now we will create the tag descriptor file also called TLD. Create a file called SubstrDescriptor.tld in WEB-INF directory of your web project and copy / paste following content in it.
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>substr</shortname> <info>Sample taglib for Substr operation</info> <uri>https://www.viralpatel.net/jsp/taglib/substr</uri> <tag> <name>substring</name> <tagclass>net.viralpatel.jsp.custom.taglib.SubstrTagHandler</tagclass> <info>Substring function.</info> <attribute> <name>input</name> <required>true</required> </attribute> <attribute> <name>start</name> <required>true</required> </attribute> <attribute> <name>end</name> <required>true</required> </attribute> </tag> </taglib>
Code language: HTML, XML (xml)
In above tld file, <tag> is used to define a custom tag. Each new tag will have its own tag handler class which we specify in <tagclass> tag. Also <name> tag in <tag> represents the tag name that we use in jsp file. We have provided the three attributes with this tag: input, start and end. input is the String whose sub string needs to be parsed. start is the start index and end is the end index. We have just created our first Custom Taglib for JSP. Now let us use the custom taglib in a JSP file. For this create index.jsp in your web application (if it exists, modify it and add this code) and add following code in it. <required> true make these attribute mandatory.
<%@taglib prefix="test" uri="/WEB-INF/SubstrDescriptor.tld"%> <html> <head> <title>JSP Custom Taglib example: Substr function</title> </head> <body> SUBSTR(GOODMORNING, 1, 6) is <font color="blue"> <test:substring input="GOODMORNING" start="1" end="6"/> </font> </body> </html>
Code language: HTML, XML (xml)
Following is the screenshot of the output.
Get our Articles via Email. Enter your email address.

You may also like...

49 Comments

  1. shah vishram says:

    I tried your example but then it fails since it does not recognise the url part in the tld file.

    • abcd says:

      include that .tld in ur web.xml

    • Badr says:

      You should include this library http://tomcat.apache.org/download-taglibs.cgi , in your [WEB-INF/lib] directory then add the jar in the build path of your project…..afterward it should work!!

  2. mahmoud says:

    thank you for the explanation I wish if you put more examples, some real examples i mean the ones you usually use in your code, because i cannot understand exactly why i should use it when I can use EL function

  3. akhil says:

    Its gud to provide such facility.thank u.

    • guest says:

      thanks.

  4. Viman says:

    I tried your example and it works perfectly fine for me. Thanks for posting this.

  5. Rajan says:

    This is really a very good site. The example given is very fine and it worked very well.
    Thank you very much :)

  6. Chandima says:

    This is very clear and helpful. Thanks for posting :)

  7. baskar says:

    it is very clear:)

  8. Nishant says:

    Very Good and quite simple implementation. Thanks Dude

  9. sreekanth says:

    Thanks…:-)
    its easy to understand..

  10. Sculler says:

    Thanks Viral. The tutorial was quite helpful.

  11. sekhar says:

    Thank you very much viral for providing useful example

  12. Ashish Gupta says:

    Awesome..You explained it to easily…Keep up the good work!

  13. Adarsh says:

    Thanks :D

  14. Djinn says:

    Thanks for the quickly and very clear explanation of custom tags in a very short article.

  15. seema says:

    good one !

    Thanks

  16. vadivel says:

    Really very good example.

  17. Srikanth says:

    nice example….. very clear.

  18. Ajay Bhayani says:

    good example……my totaly concept is clear about custom tag

  19. Johnson George says:

    nice one.. its very useful

  20. Vasanth says:

    Thank you so much. It worked without any changes .

    Very nice and simple example.

  21. TienCuong says:

    Thank you so much. Very nice and easy to understand tutorial.

  22. Ingrid says:

    This is the 2nd blog, of your site I actually read through.

    And yet I actually enjoy this specific 1, “Tutorial: Create JSP custom tag library | JSP Custom Tag
    | Taglib” the very best. Thanks -Bobbie withprimesolarwindowshades.
    com/window-shades

  23. Vasant says:

    Thanks

  24. monireh says:

    how do i create jsp textbox custom tag?

  25. ramesh says:

    Nice exampal, this really help ful for new tag library development thank lot for nice work viral

  26. TomEk says:

    Can you explain what is the difference between

    and

    How is interpreted – for example namespace?

  27. murali says:

    like can we create the tags in spring mvc … i need to create the calendar tag how can i… or is any other way to use it.

  28. Madhur says:

    Can you please answer to this question. http://stackoverflow.com/questions/17136255/unable-to-access-nested-jsp-tags-in-freemarker

  29. Sunitha says:

    Nice Example. Easy to understand. Thanks for posting.

  30. prasad says:

    Thanks for the basic example

  31. Anshuman says:

    when you have defined URI then you should provide same URI in JSP page. Nevertheless it will work .But then what ‘s use of giving URI when you use relative path of TLD file ?
    It seems unnecessary

  32. venkatesh says:

    why are you asking to copy paste ,Instead you have been used comments to explain what exactly is happening

  33. Binh Thanh Nguyen says:

    Thanks, nice post

  34. Gregory says:

    I got mine to work easily, the first time by following your example. Thank you for providing a complete example.

  35. kranth....pilla says:

    very nicee………….

    • pratap says:

      Thanks .very nice example ……….

  36. Abhijit Chowdhury says:

    This is very helpful for all…………………. Nice

  37. shashank says:

    Thanks dude… your tutorials are really easy to understand and implement. This is a really good example.

  38. karan says:

    I am getting an error in my code. can you answer my this question.

    http://stackoverflow.com/questions/22758863/unable-to-find-setter-method-for-attribute-in-jstl?noredirect=1#comment34693594_22758863

  39. Vignesh says:

    Very helpful article, I could quickly understand how/what tags are for.

  40. Raju Kapadne says:

    Hi Viral ,
    Very nice you have explain Custom Tag Example.
    i understood very clearly , Thanks a lot !!!

  41. mohit says:

    can u answer my this question ?

    http://stackoverflow.com/questions/23284948/custom-tag-is-not-working-properly

  42. Ron says:

    Thanks! this is simple, clear example and can be duplicated.

  43. David says:

    Thanks, it helped!

  44. Neha says:

    Thank You for the example..!!

  45. Dheeraj says:

    Very good tutorial.finally i learn about jsp custom tag here thank u very much……

Leave a Reply

Your email address will not be published. Required fields are marked *