Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).
Developing a custom tag library consists of three main steps:
Write a tag handler. A tag handler is a Java class that contains the logic for your custom tag. Every time a JSP page encounters a custom tag, the JSP container finds the associated tag handler of the custom tag and executes it. Listing 10 shows an example of a tag handler called MyTagHandler.
Write a tag library descriptor (TLD). A TLD is an XML file that defines a tag library and its tags. Listing 11 shows an example of a TLD file called myTaglib.tld.
Edit your Web application’s deployment descriptor (web.xml file). To use custom tags, you must specify a taglib element in the deployment descriptor. Listing 12 shows a deployment descriptor with this element.
Write a JSP page that includes the taglib directive. To use a custom tag in a JSP page, you need to use the taglib directive to identify the TLD and tag. Listing 13 shows an example of a .jsp that uses a custom tag.
Before we go into the details of each of these steps, let’s use the examples in Listings 10 through 13 to demonstrate how custom tags work. First, write the tag handler in Listing 10 and compile it to obtain the MySimpleTag.class file. Next, write the TLD file in Listing 11. Then edit the application’s deployment descriptor as shown in Listing 12. Finally, create the customTagExample.jsp file in Listing 13 to use the custom tag.
Listing 10The Tag Handler: MyTagHandler.java
package ch01
; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; public class MyTagHandler extends TagSupport { public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.print("This is the output from the custom tag."); } catch (Exception e) { System.out.println(e.toString()); } return super.doEndTag(); } }
Listing 11The Tag Library Descriptor: myTaglib.tld
NOTE: Tomcat reports an error if your tag handler belongs to the default package.
To compile the MyTagHandler.java, you need the servlet-api.jar file in your class path. In Tomcat 5, the servlet-api.jar can be found in the common/lib directory under %CATALINA_HOME%. If you copy the servlet-api.jar file to the directory where subdirectory ch01 resides, here is the command to compile the source code:
The directory structure for deployment is shown in Figure 2. The application name is JSFCh01. Now, start Tomcat and direct your browser to the following URL:
http
://localhost:8080/JSFCh01/customTagExample.jsp
You will see the result shown in Figure 3.
When the JSP container encounters the taglib element in the JSP page, it looks up the deployment descriptor for the location of the taglib where the URI is /myTLD and gets the path to the TLD file. This path is stored in memory for later use. When the custom tag
Figure 2The directory structure for the application that uses the custom tag
Figure 3Output from the custom tag
<myTag> is encountered, the JSP container reads the TLD file and finds the fully qualified name of the tag handler. The JSP container then loads the tag handler and processes it.
Figure 4 illustrates this process.
Figure 4The relationship between the deployment descriptor, the JSP page, and the TLD file.
The following sections discuss the steps for developing and using a custom tag library in detail.
Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne. Buy this book!