3.2 Why tags?
JSP already makes it possible to embed scriptlets (bits of Java code) and JavaBeans in
line with HTML content, so why do we need JSP tags? We need them because tags
were never intended to offer more functionality than scriptlets, just better packag-ing.
JSP tags were created to improve the separation of program logic and presenta-tion
logic; specifically, to abstract Java syntax from HTML.
Scriptlets are not a suitable solution for all web development because most con-tent
developers (art designers, HTML developers, and the like) don't know Java
and, perhaps, don't care to. Though much Java code can be encapsulated in beans,
their usage in a JSP still requires the presentation developer to have a basic knowl-edge
of Java syntax and datatypes in order to be productive. JSP tags form a new
"scriptlet-free" and even a completely "Java-free" component model that is adapted
perfectly to the JSP environment with its different developer types. If custom tags
are properly constructed, they can be of enormous use to HTML developers, even
those who have no working knowledge of Java— they won't even have to know
they're using it. Tags can reduce or eliminate the number of scriptlets in a JSP appli-cation
in four ways:
- A tag is nothing more than a Java component that takes its arguments from attribute and body. Since tags can have attributes and body, any necessary param-eters to the tag can be passed within the tag's body or as one of its attributes. No Java code is needed to initialize or set properties on the component. .
- JSP requires a considerable quantity of scriptlets for tasks such as iteration, setting of initial values, and performing conditional HTML. All of these tasks can be cleanly abstracted in a few simple tags.
- In many cases, a JavaBean component is configured and activated using scriptlets. One can develop a set of JSP tags to perform this configuration and activation without any Java.
- Tags can implement many utility operations, such as sending email and con-necting to a database, and in this way reduce the number of utility scriptlets needed inside JSP.
The benefits of custom tags also include the creation of a neat abstraction layer
between logic and presentation. This abstraction creates an interface that allows
Java developers to fix bugs, add features, and change implementation without
requiring any changes to the JSPs that include those tags. In short, JSP tags help
bring you one step closer to the Holy Grail of web development— true abstraction
of presentation and control. For more on the benefits of custom tags, see
chapter 15.
3. 2. 1 Comparisons of scriptlets and custom tags
- The differences between scriptlets and custom tags are fairly concrete:
Custom tags have simpler syntax. Scriptlets are written in Java and require
the author to be familiar with Java syntax, whereas tags are HTML-like in syn-tax
and require no Java knowledge.
- Custom tags are easier to debug and are less error prone than scriptlets, since
omitting a curly bracket, a semicolon, or some other minute character in a
scriptlet can produce errors that are not easy to understand. Custom tag syn-tax
is extraordinarily simple and, with most JSP runtime products, even the
occasional typo in custom tag usage will produce meaningful error messages.
- Custom tags are easy to integrate in development environments. Since tags
are a common component of many web technologies, HTML editors have
support for adding tags into the development environment. This allows JSP
authors to continue using their favorite integrated development environ-ment
(IDE) to build tag-based JSPs. Support for JSP scriptlets syntax in
development environments exists, but is only useful to JSP authors well
versed in Java.
- Custom tags can eliminate the need for Java in your JSPs. By containing
most of your logic within objects in your scriptlets, you can vastly reduce
the amount of Java code in a JSP; however, custom tags still carry the
advantage of imposing absolutely no Java syntax, something scriptlets can-not
achieve.
For small projects in which all your JSPs will be authored by developers knowledge-able
in Java, scriptlets are a fine solution. For larger projects, where content devel-opers
unfamiliar with Java will be handling most of the presentation, JSP custom
tags provide a real advantage and are a logical choice.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|