Chapter 15 - Using custom tags

Chapter 15 explains the following things:
  1. The use of web.xml to map <taglib-uri> with <taglib-location>
  2. The use of the <%@taglib> directive to import a tag library in JSP pages
  3. The use of custom tags in JSP pages.
  4. The use of JSTL commands in JSP pages.

Note:

The actual implementation of the tag handler classes is explained in chapter 16. However, we have copied the same classes into the chapter15 web application so that you can understand and practice the above objectives.

How to use the chapter15 web application

There are four TLD files in this web application. The contents of all four are the same but they either have different names or they are in different locations so that you can test the different ways of using the taglib directive.

To use this locationUse this taglib directiveDescription
/sampleLib.tld<%@ taglib prefix="test"
uri="sampleLib.tld"%>
As shown in listing 15.2
/mylibs/studyKit.tld<%@ taglib prefix="test"
uri="http://www.manning.com/studyKit" %>
As mapped in web.xml (See listing 15.1)
/META-INF/taglib.tld inside
/WEB-INF/yourLibs/sample.jar
<%@ taglib prefix="test"
uri="http://www.manning.com/sampleLib" %>
As mapped in web.xml (See listing 15.1)
/WEB-INF/sampleLib.tld<%@ taglib prefix="test"
uri="http://www.manning.com/myTest" %>
As mapped in web.xml (Read the explanation below)

We have added the following in web.xml file

	<taglib>
		<taglib-uri>http://www.manning.com/myTest</taglib-uri>
		<taglib-location>sampleLib.tld</taglib-location>
	</taglib>
Since, the location sampleLib.tld, is a non-root relative path, the JSP engine will prepend it with /WEB-INF/ and thus use /WEB-INF/sampleLib.tld


Trying the combinations

addressInput.jsp is the JSP page as shown in listing 15.2. It uses the first location:
   <%@ taglib prefix="test" uri="sampleLib.tld" %> .

Try out all the other combinations of the TLD locations and the uri attribute of the taglib directive to get familiar with all types of usages. To do that, just open the addressInput.jsp file and modify the taglib directive as shown in the table above.

Tokens.jsp is a very simple JSP shown in listing 15.3. It uses the Java Standard Tag Library to display a list of Token values. It accesses the JSTL with the following statement:
    <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> .

Remember: In order to use the JSTL, you need to copy jstl.jar and standard.jar from
C:\jakarta-tomcat-5.0.25\webapps\jsp-examples\WEB-INF\lib
and add them to a lib directory within WEB-INF.