Custom Tags in JSP: Features, Examples, and Implementation, Slides of Java Programming

An overview of custom tags in jsp, their features, examples, and implementation. Custom tags are user-defined jsp language elements that encapsulate recurring tasks and provide portable semantics. They help separate presentation from business logic and make page authoring simpler. The architecture of custom tags, their benefits, and how to write, package, and use custom tags.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Agenda
`
PART 01
`
Evolution of web Tier technology
`
What is Custom Tag
`
Why Custom Tag
`
Quick Intro to Tag1.2
`
PART02
`
Custom Tag 2.0
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Custom Tags in JSP: Features, Examples, and Implementation and more Slides Java Programming in PDF only on Docsity!

Agenda

` PART 01

Evolution of web Tier technology What is Custom Tag Why Custom Tag Quick Intro to Tag1.

` PART

` Custom Tag 2.

Evolution of

Web tier technology

OK, let's talk about where JSP fits in from a big picture perspective of J2EE architecture.

Where does custom tags fit in?

Java Beans Custom tag

This picture shows how custom tags fit into the Web application architecture. Typically HTTP requests coming from the client are handled by the centralized controller, which in turn forwards them to JSP pages. The JSP pages then call server side objects for business logic processing. These server side

objects can be either in the form of Java Beans or custom tags.

Standard Action Tags

<jsp:useBean> Instantiate Java Bean class without explicit Java programming language <jsp:getProperty> Allow accessing bean properties <jsp:setProperty> Allow setting bean properties

Now JSP container already provides simple but standard tags that allows JSP page designer to set and get properties of Java Beans instances. For example, the <jsp:useBean> tag instantiates Java Bean class without forcing page designers to use Java programming language. And <jsp:getProperty> and

<jsp:setProperty> allows page designers to access and set properties of Java Bean instances.

What is

Custom Tag?

Now let's talk about what custom tag is.

What is a Custom Tag?

User defined JSP language elements Encapsulates recurring tasks ` Distributed in a “custom made” tag library

Custom tags are user‐defined JSP language elements that encapsulate recurring tasks. Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags.

Custom Tag Examples

Getting/setting Implicit objects Processing forms Accessing database Flow control

Iterations Many more

The standard JSP tags simplify JSP page development and maintenance. JSP technology also provides a mechanism for encapsulating other types of dynamic functionality in custom tags, which are extensions to the JSP language. Some examples of tasks that can be performed by custom tags include operations

on implicit objects, processing forms, accessing databases and other enterprise services such as e‐mail and directories, and flow control.

Ready-to-usable Custom Tag Library

Java Standard Tag Library (JSTL) Tags for setting/getting attributes, iteration, etc Tags for database access Tags for internationalized formatting Tags for XML Jakarta-Taglibs

Now there are ready to usable custom tag libraries. They are Java Standard Tag Library (JSTL), which contains custom tags for setting and getting attributes, handles iterations, and so on. It also contains tags for database access, tags for internationalized formatting and tags for XML manipulation. Also

Jakarta tag library from Apache has been quite popular.

Why Custom Tags?

Separate presentation from business (and other functionality) logic page authors author presentation Business logic developers create custom tags Encapsulate business logic Reusable & Maintainable Help page authors Page authors use simpler syntax Provide ` Portable semantics

Custom tags separate presentation from business logic in that page content is created by page designers while the business logic's are captured in the form of custom tags. Now custom tags is not just for business logic but it can encapsulate other relatively complex functionality such as complex display,

iteration, formatting and so on.

Because custom tags encapsulate business logic, they are reusable and easier to maintain. In fact, in most cases, you will use custom tags that are created by someone else.

Because custom tags hide complexity from page authors, it is easier to author pages. And there are many tools that support custom tags.

Custom tags provide portable semantics.

Custom Tags vs. JavaBeans

Custom tags can manipulate JSP contents while beans cannot Complex operations can be reduced to a significantly simpler form with custom tags than the beans ` Custom tags require quite a bit of more work to set up than do beans

source: more Servlets and JSP[2]

(read the slide)þ

Three things make up custom tag

architecture

Tag handler class Defines tag's behavior Tag library descriptor (TLD) Maps XML elements to tag handler class JSP file Uses tags

To use a custom tag in a JSP page, you must:

  • Declare the tag library containing the tag before the usage of any custom tags from that tag library
  • Use custom tags using custom tag syntax in a JSP page

It is assumed that the tag library has been configured and deployed with individual Web application or

globally for all Web applications running on the server.

Steps for implementing custom tags

` Write tag handlers

Write Tag library descriptor (TLD) file Package tag handlers and TLD file into tag library (either unpacked or packed form)

Write JSP pages that use tags Configure and deploy tag library along with JSP pages

The steps you follow in order to implement and deploy custom tags are relatively straight‐forward.

First, you write tag handlers. Under JSP 1.2 architecture, tag handlers are Java classes.

Second, you write so called tag library descriptor, TLD file, in short.

Third, you package a set of tag handlers and TLD file into what is called tag library in either unpacked or

packed form.

Then, you write JSP pages that use these tags. Then you deploy the tag library along with JSP pages.

Now let's talk about 3 things here by using examples ‐ tag handler, TLD file, and JSP pages.

Example: Tag Handler (page 1)

ExampleTag.java

package moreservlets.tags; import javax.servlet.jsp.; import javax.servlet.jsp.tagext.; import java.io.; /* Very simple JSP tag that just inserts a string ("Custom tag example...") into the output.

  • The actual name of the tag is not defined here; that is given by the **Tag Library
  • Descriptor(TLD)** file that is referenced by the taglib directive in the JSP file.
  • *

    * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * © 2002 Marty Hall; may be freely used or adapted. */

This is the simplest tag handler, ExampleTag.java, from Marty Hall's sample code you can download free. The package structure is ./moreservlets.tags.ExampleTag.class.

Example: Tag Handler (page 2)

ExampleTag.java

public class ExampleTag extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.print("Custom tag example " + "(moreservlets.tags.ExampleTag)"); } catch(IOException ioe) { System.out.println("Error in ExampleTag: " + ioe); } return(SKIP_BODY); } }

The ExampleTag class extends TagSupport class. It does include doStartTag() method in which you get JspWriter object which you get from pageContext object.

Since the ExampleTag does not include nor manipulate body contents, it returns SKIP_BODY. (We will talk about this again later on.)þ