Friday, May 30, 2008

All about JSP : Custom Tags

What's Custom Tag ? and Whats Use ??
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
  1. Empty Tags
  2. Tags with attributes
  3. Tags with JSP code
  4. 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 ?
TWO ways
(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 PAGE...
< / jsp:root>

Above examples shows URI. It can be of many type right ?
Three Types
(1) Absolute URI :
A URI that has a protocol, a hostname, and optionally a port number.
http://localhost:8080/mytaglibs

(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.
/myLib
/mytaglib/myLib

(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.
myLib
mytaglib/myLib

< % @ 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:
  • myPackage/myTagHandler1.class
  • myPackage/myTagHandler2.class
  • myPackage/myTagHandler3.class
  • META-INF/taglib.tld
How can the jsp container populate the taglib map
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 element of a JSP document contains an example of a well-known URI: http://java.sun.com/JSP/Page

Algorithm ?
• If the value of the uri attribute matches any of the entries, the engine uses the value of the corresponding to locate the actual TLD file.
• If the value is a root-relative URI (that is, it starts with a /), the JSP engine assumes the location to be relative to the web application’s document root directory. Thus, the location for the URI http://www.manning.com/studyKit in listing 15.1 will resolve to
the TLD file /myLibs/studyKit.tld.
• If the value is a non-root relative URI (that is, it starts without a /), the JSP engine prepends /WEB-INF/ to the URI and assumes the location to be relative to the web application’s document root directory. Thus, the location for the URI http://www.manning.com/
sample.jar in listing 15.1 will resolve to the TLD file /WEB-INF/yourLibs/studyKit.tld.

• If the value of the uri attribute of the taglib directive does not match any of the entries, then the following three possibilities arise:
• 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 /jsp/test.jsp contains the directive < % @ taglib prefix="test" uri="sample.tld" % >, the engine will expect to find the sample.tld file at /jsp/sample.tld.


Ques Where will it look for TLD file ?
< % @ taglib uri="www.myserver.com/myLib.tld" prefix="mylib" % >
Ans
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
C:\tomcat\webapps\app15\test.jsp
The engine will look for the file at
C:\tomcat\webapps\app\www.myserver.com\hello.tld

Note
The value of cannot be an absolute URI. and both the and are mandatory when defining absoulte way.

2 comments:

Lavanya said...

Hey its very helpful. Thanks

Lavnish said...

Thanks
comments keep me going
I have been irregular these days
will be back soon