Custom tags allow us to move the presentation logic outside the JSP pages into independent Java classes. One may choose to make custom tag for particular project or as an independent jar file.
There are diff kinds of custom tags
- Empty Tags
- Tags with attributes
- Tags with JSP code
- Tags with nested tags
Whats TagHandler ?
As name suggest its something that handles Tag and that something can only be java class. A tag handler is a Java class that implements one of the tag interfaces—Tag, IterationTag, or BodyTag—of the package javax.servlet.jsp.tagext.
The JSP specification defines a tag handler as a runtime, container-managed object that evaluates custom actions during the execution of a JSP page.
Whats Tag library descriptor ?
Its the way to let JSP engine know the tag handler classes for custom tags.
How to inform JSP Engine about custom tag library ?
(1) Use the taglib directive
< % @ taglib prefix="test" uri="testLib.tld" % >
(2) Use jsp root
< jsp="http://java.sun.com/JSP/Page" test="testLib.tld" version="1.2">
< / jsp:root>
Above examples shows URI. It can be of many type right ?
(1) Absolute URI :
A URI that has a protocol, a hostname, and optionally a port number.
(2) Root Relative URI :
A URI that starts with a / "and" that does not have a protocol, a hostname, or a port number. It is interpreted as relative to the document root of the web application.
(3) Non-root Relative URI :
does not start with a / "and" that does not have a protocol, a hostname, or a port number. It is interpreted as relative to either the current JSP page "or" the WEB-INF, depending on where it is used.
< % @ taglib prefix="test" uri="sampleLib.tld" % >
The JSP engine searches for the file in the same directory as the JSP page.
Though keeping all the JSP pages and the TLD files in the same directory is the simplest way to use a taglib directive, it has two major drawbacks: security and flexibility.
How is the URI mapped to TLD ?
Through mapping ... standard reason ... achieve security & flexibility by maintaining a level of redirection ... If we wanted to switch to a newer version of the library, say myLib2.tld, then we would have to manually modify all the JSP pages that are affected by this change. The JSP container maintains a map between the URIs that we use in taglib directives and the actual physical location of the TLD files in the file system.
With this approach, instead of using a page-relative path, we use an absolute URI path:
< % @ taglib prefix="test" uri="http://www.myserver.com/myLib" % >
When the JSP engine reads the above URI, it refers to its internal map to find the corresponding
TLD file location.
The JSP specification mandates that, when deployed in a JAR file, a TLD file be
placed either directly under or inside a subdirectory of the META-INF directory. In
addition, the name of the TLD file must be taglib.tld. Thus, a JAR file containing
a packaged tag library is typically structured like this:
Three ways ...
• First, the container reads the user-defined mapping entries present in the deployment descriptor. This is called explicit mapping. We will learn how to add new entries to the deployment descriptor in the next section.
• Then, the container reads all the taglib.tld files present in the packaged JARs. For each jarred TLD file that contains information about its own URI, the JSP container automatically creates a mapping between the specified URI and the current location of the JAR file. This is called implicit mapping. We will learn how to add the URI information to a TLD file in the next chapter, where we will create a custom tag library.
• Finally, the JSP container adds entries for the URIs that are known to the container by default. These URIs are called well-known URIs. The
• If the value of the uri attribute matches any of the
• If the
the TLD file
• If the
sample.jar in listing 15.1 will resolve to the TLD file
• If the value of the uri attribute of the taglib directive does not match any of the
• If the specified uri attribute is an absolute URI, then it is an error and is reported at translation time.
• If the specified uri attribute is a root-relative URI, it is assumed to be relative to the web application’s document root directory.
• If the specified uri attribute is a non-root-relative URI, it is assumed to be relative to the current JSP page. Thus, if the JSP file
Ques Where will it look for TLD file ?
< % @ taglib uri="www.myserver.com/myLib.tld" prefix="mylib" % >
The URI www.myserver.com/myLib.tld does not contain a protocol; therefore, it is not an absolute URI. It does not start with a /, so it is not a root-relative URI either. It is a page-relative URI. After failing to find an entry in the map, the engine will search for the file hello.tld at the location relative to the current page. Suppose the JSP page is at location
The engine will look for the file at
The value of