

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Evolution of web Tier technology What is Custom Tag Why Custom Tag Quick Intro to Tag1.
` 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.
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.
<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.
Now let's talk about what custom tag is.
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.
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.
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.
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 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)þ
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:
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.
` 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.
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.
* 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.
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.)þ