Home arrow Java & J2EE arrow Page 6 - Overview of Java Web Technologies, Part 2

Custom Tags - Java

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).

TABLE OF CONTENTS:
  1. Overview of Java Web Technologies, Part 2
  2. Sun's Solution
  3. JSP and JavaBeans
  4. Calling a Bean from a JSP Page
  5. Accessing Bean Properties
  6. Custom Tags
  7. Developing and Using Custom Tag Libraries
  8. Writing a Tag Handler
  9. Writing and Using Tags
  10. Model 2 Architecture
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 9
March 03, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Using JavaBeans allows you to separate the presentation part of a JSP page from the Java code. However, only three action elements—jsp:useBean, jsp:getProperty, and jsp:setProperty—are available for accessing a bean. Therefore in some situations, we need to resort to using code in a JSP page. In other words, in many cases, JavaBeans do not offer complete separation of presentation and business rule implementation.

Also, JavaBeans are designed with reusability in mind, meaning that using a bean to output HTML tags directly is not recommended. Outputting HTML tags from a bean makes the bean usable only from a certain page.

In recognition of the imperfection of JavaBeans as a solution to separation of presentation and business rule implementation, JSP 1.1 defined a new feature: custom tags that can be used to perform custom actions. For example, the following JSP page uses custom tags:

 
<%@ taglib uri="/myTLD" prefix="c"%> 
<html
<head
<title>Using custom tags</title
</head
<body
<c:myTag/> 
</body
</html

As you can see, the JSP page above is free from Java code. On the first line, you declare that you are using a custom tag in the JSP page, and on the seventh line, you use the custom tag itself. The result output by the <c:myTag/> tag depends on the Java class associated with the custom tag.

Understanding how to use and develop custom tags is important in developing JSF applications, because, in fact, JSF uses custom tags extensively.

To use custom tags, you must develop a custom tag library. We will examine the process of creating custom tag libraries and using them from JSP pages in detail. But first, we’ll take a look at the differences between JavaBeans and custom tags.

Comparing Custom Tags with JavaBeans

Compared to JavaBeans, custom tags offer some advantages:

  • Custom tags have access to all the objects available to JSP pages.
  • Custom tags can be customized using attributes.

However, custom tags have the disadvantage of being slightly harder to build and use than JavaBeans. Sometimes, JavaBeans are preferable because of their reusability. There is no hard-and-fast rule governing whether to use JavaBeans or custom tags. You often need to decide which one to use based on the particular project’s specifications.

Buy this book now!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!



 
 
>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: