Tag files contain JSP elements that define a custom action. A JSP tag file is similar to a normal JSP file with three exceptions:
- The page directive is not allowed in a tag file.
- There are certain JSP directives that are only allowed in a tag file.
- The file name extension for a tag file is “.tag”.
Everything else is the same as creating a JSP page.
Using custom JSP tag files provide a number of benefits:
- Promotes reusability
- Enhances loose coupling
- Creates single point of maintenance
- Allows a clear division of labor between technical and esthetic skill sets
The following is a JSP page that uses a custom tag. For all intents and purposes, it eliminates coding from the page with the exceptions of the directives and the me prefix on the sayHello element.
1 2 3 4 5 6 7 8 9 10 11 | <%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="me" tagdir="/WEB-INF/tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Hello with Custom Tag</title> </head> <body> <me:sayHello firstName="Roger" lastName="Sakowski"/> </body> </html> |
When this page is viewed in a browser, it will provide the following display:
Here is the custom tag (sayHello.tag) used in the JSP example page above:
1 2 3 4 5 6 7 | <%@ tag body-content="empty" %> <%@ attribute name="firstName" required="true" rtexprvalue="true" %> <%@ attribute name="lastName" required="true" rtexprvalue="true" %> <jsp:useBean id="bean" class="com.webucator.beans.YourName" /> <jsp:setProperty name="bean" property="firstName" value="#{firstName}" /> <jsp:setProperty name="bean" property="lastName" value="#{lastName}" /> <jsp:getProperty name="bean" property="greeting" /> |
Note the directives. They declare the required attributes firstName and lastName. These attributes were used in expressions to set the values on a JavaBean. Here is the JavaBean (YourName.java) used by this tag:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.webucator.beans; public class YourName { private String firstName; private String lastName; public YourName() {} public String getGreeting() { return "<h1>Hello, " + firstName + " " + lastName + "!</h1>"; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } } |
This brings me back to the benefits or custom JSP tags that I listed at the beginning of this article:
- Promotes reusability: The tag can be used in any number of JSP pages that need it.
- Enhances loose coupling: The JSP page is completely decoupled from the underlining code.
- Creates single point of maintenance: Changes made to the tag will also change the JSP documents that use it.
- Allows a clear division of labor between technical and aesthetic skill sets: It frees page designers to deal with design while freeing programmers to focus on programming.
In short, it is a win/win proposition for web application development in general.
In my upcoming articles I will cover Java frameworks in general and explore the most popular.
Custom tags are covered in our Introduction to JSP, Advanced JSP, and Comprehensive JSP classes.





Leave a Reply