Java Reference
In-Depth Information
JSP Customs Tags
JSP custom tags encapsulate functional and/or business logic that can be reused inside of a
JavaServer Page. This gives you the ability to insert complex logic into a JSP without having
the JSP code itself be overly complicated. This is a true use of object-oriented encapsulation.
Some custom tag examples that we have already discussed are the JSP standard actions. They
have the same functionality as a custom tag. A code snippet with a custom tag follows:
<%@ taglib uri=”/djs” prefix=”djs” %>
<djs:hello />
This JSP contains two lines that you need to examine. The first is the line:
<%@ taglib uri=”/djs” prefix=”djs” %>
This is the taglib directive. It describes the tag library and must be included to reference any
custom tag library. We will cover the taglib directive in more detail in the next section.
The second thing you should notice is the tag itself. Our tag has a prefix of djs , which was
defined by the taglib directive, and the tag itself is hello . When the JSP container encounters
this line of JSP text, it will execute the code associated with this tag. The code that will be exe-
cuted is called a tag handler , which will also be covered in further detail in a later section.
Deploying Tag Libraries
Before you can start using tag libraries, you must understand how they are deployed. JSP tag
libraries are groups of JSP custom tags that have been packaged together for deployment. As
sample grouping might be a set of tags that encapsulate database functionality. Four steps are
involved in deploying a tag library:
Create a tag library descriptor.
Deploy the tag handlers to your Web application.
Add a taglib entry to your Web application's web.xml file.
Add a taglib directive to your JSP that will be using the custom tag library.
Creating a Taglib Descriptor
A tag library descriptor ( tld ) is just what it sounds like. It is an XML file that describes the tag
library that you are trying to deploy.
Search WWH ::

Custom Search