Taglib are also termed as JSP tag extensions. They provide a way of encapsulating reusable functionality on JSP pages. One of the biggest drawbacks of scripting environments such as JSP is that it’s easy to get carried away without thinking about how it will be maintained and grown in the future. For example, the ability to generate dynamic content by using Java code embedded in the page is a very powerful feature of the JSP specification. Custom tags allow such functionality to be encapsulated into reusable components. You can make write your reusable class in JAVA and call the same using XML tag.
There are four files which play an important role:-
- Main class file which encapsulates the logic.
- Tag library descriptor file.
- Web.xml file which has the tag library descriptor file location.
- Finally the JSP file which calls it.
Below is the image which shows the four files in one go.
Figure: – taglib directive in action
The first file is the class file which will has the reusable code which will be called in the JSP file. The above class is also called as tag handler class. One of the important things to note is doStartTag() and doEndTag(). doStartTag is called when the JSP engine encounters an open tag and doEndTag is called when it encounters a closed tag.
The second important file is the tag descriptor file. This file maps the class name with a name which will be used to call this class.
The third file is the web.xml file. We need to define the tag library descriptor file location in web.xml file.
Finally is the JSP file which calls the class. There are two things to be noted in the JSP file first is the taglib which refers to the URI. Second is the custom tag which calls the datetime class.
See the following video on Java which describes introduction to EJB (Enterprise Java Beans): –
Click to get Java Training
Get more Java training stuffs from author’s blog