Learn Java for Web Development

(Tina Meador) #1

78 CHAPTER 2: Building Web Applications Using Servlets and JSP


Taglib Directive


The Java Server Pages API provides standard actions, covered in the next section, that encapsulate
functionality. The JSP API also allows you to define custom actions that implement custom behavior.
Several such custom actions, also called custom tags, are assembled in a library called a tag library.
The taglib directive is used to define a prefix and location for a tag library in the current JSP page.
The taglib directive uses the following syntax:


<%@ taglib uri="uri" prefix="tagPrefix" >


The uri attribute value is an absolute or relative path that specifies the location of the tag library, and
the prefix attribute specifies the custom action to be used in the current JSP. Listing 2-18 illustrates
the usage of the taglib directive for an example tag library called helloTagLib, which comprises a
tag called hello that prints a “Hello World” message.


Listing 2-18. Usage of the Taglib Directive


1.<%@ taglib uri="/ helloTagLib" prefix="helloTag" %>
2.
3.
4.
5.
6.


   Line1: The URI to the library helloTagLib and prefix helloTag
 Line 4: Using the hello tag via the prefix

Declarations

Using declarations, JSP allows you to declare methods and variables in JSP pages. Once they are in
a JSP page, they are available to scriptlets and expressions throughout the page. JSP declarations
are placed between <%! and %> declaration delimiters. Since declarations are used with expressions
and scriptlets, I will introduce expressions and scriptlets in the following sections, and then I will
show you how declarations, scriptlets, and expressions are used in a JSP page.


Figure 2-26. Using the include directive

Free download pdf