

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Include taglib directive before tags are used Syntax <%@ taglib prefix="myprefix" uri=โmyuriโ %> prefix: identifies the tag library ` uri: uniquely identifies the tag library descriptor (TLD) directly or indirectly
You declare that a JSP page will use tags defined in a tag library by including a taglib directive in the page before any custom tag from that tag library is used. If you forget to include the taglib directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as template data, and simply insert the text of the custom tag call into the response.
The syntax of taglib directive is as following:
<%@ taglib prefix="tt" uri=URI %>
The prefix attribute defines the prefix that distinguishes tags defined by a given tag library from those provided by other tag libraries. The tagdir attribute specifies the location of tag files. And uri attribute uniquely identifies the tag library descriptor (TLD) either directly or indirectly.
or
body
prefix: distinguishes tag library tag: identifies a tag (within the tag library) attr1 ... attrN: modify the behavior of the tag
Custom tags have the syntax
or
body
where prefix distinguishes tags for a library, tag is the tag identifier, and attr1 ... attrN are attributes that modify the behavior of the tag.
This picture shows when the JSP page in the previous slide is accessed.
Source Directory Structure
Use the following source directory structure (as in J2EE 1.4 Tutorial's web/iterator directory) /j2eetutorial14/examples/web ` iterator
build.xml (ant build file)
src (dir where *.java files with proper package structure reside)
web (dir)
WEB-INF (dir)
*.tld
tags (dir - needed for JSP 2.0 tag files)
*.jsp
First, let's see what would be a desired source directory structure. Of course, how you structure your development directory structure is dependent on individual taste, a recommended approach is to have separate the source tree from the build/deployment tree. And that is what Java WSDP tutorial code is structured and that is what we will use here as well.
Java WSDP 1.2 has โiteratorโ custom tag and it comes with ant build.xml file. So for our custom tag development, we will have a source directory structure mentioned in the slide. That is, build.xml is in the top directory. And we will have src directory which contains *.java source files. And the web directory has WEBโINF subdirectory which will contain the TLD file and JSP files will be under web directory.
Use the following build directory structure (as in J2EE 1.4 Tutorial's web/iterator directory) /j2eetutorial14/examples/web ` iterator
build (unpacked *.war file)
dist (packed *.war file)
OK, now let's see what would be the build directory structure. We will have โbuildโ directory and โdistโ directory. The build directory will reflect the unpacked form of the custom tag library while the dist directory contains the *.jar file.
The first major enhancement in JSP 2.0 is integration of Expression Language (EL) as a builtโin feature of JSP. Expression Language has been around as part of Java Standard Tag Library (JSTL) 1.0. Now due to its huge success, EL is now part of JSP itself. That is, Servlet 2.4 and JSP 2.0 compatible container should be able to handle EL expressions as native JSP syntax.
Based on โSPELโ from JSTL 1. Simplest Possible Expression Language Let you access the property values of a JavaBean in a simpler syntax Example: ${item.price} Recognized by JSP container in: Template text Attributes of any standard or custom action Support for custom EL functions: Extensible via tag libraries Example: ${fn:allCaps(lastName)} ` JSTL 1.1 provides 16 standard EL functions
Expression language is based on โSimplest Possible Expression Languageโ from JSTL 1.0. It basically let you access the property values of a JavaBean in a simpler syntax. For example, โpriceโ property value of โitemโ object can be displayed by using ${item.price} syntax. It also let you perform arithmetic operations, comparisons. You can also access the implicit objects such as form input parameters from the HTML client.
The JSP expression language allows you to define a function that can be invoked in an expression. If you are familiar with string manipulation code, the functions serve somewhat similar purpose. The difference is that under JSP 2.0, these functions can be invoked using EL syntax.
Functions are defined using the same mechanisms as custom tags. For example, if there is a function, for example, allCaps() method that is defined inside tag handler, you can call it through EL syntax, for example, โfnโ prefix with the method name and parameter.
` Using scriptlets:
<% Map m = (Map)pageContext.getAttribute("states" ); State s = ((State)m.get( "NY" )); if( s != null ) { %> <%= s.getCapitol() %> <% } %>
` Equivalent, using an EL expression:
${states["NY"].capitol} ${states["NY"].capitol.name}
This is another example expression language, this time with a Map collection object in which multiple keyโobject pairs, in this case, state and its capital city pairs, are stored.
Again, if you want to get the capital city of a particular state from states map object, you provide the key value with a bracket and the access the capital property.
<%-- Output Shopping Cart --%> <%@ page import="com.acme.util.*" %> <%@ taglib prefix="util" uri="http://mytaglib" %>
<% for( int i = 0; i < cart.size(); i++ ) { CartItem item=(CartItem)cart.get(i); %>
<%= item.getName() %> <%= item.getPrice() %>
<% } %>
This slide shows a JSP page in which the logic of displaying lineitems of a shopping cart is written using the JSP 1.2 syntax with scriptlets. And as you can see, the page is rather hard to build and read. Now let's see how this page can be simplified using JSP 2.0 syntax in the following slide.
**
${item.name} ${item.price}
**
This slide shows the same JSP 2.0 based page but using XML syntax even for the directives. One reason you might want to build your JSP page as XML document is to be able to use XML schema for validation and to allow XML tools to be used for building and manipulating the JSP page.
Now let's talk about simple tag handlers and extensions, which is one of the significant enhancements in JSP 2.0.
Simpler to use than classic tag handlers Implement SimpleTag interface Usually extend SimpleTagSupport class doTag() of SimpleTag interface gets invoked when the end element of the tag is encountered ` You want to override this method
Why do you want to use simple tag handlers as opposed to classic tag handlers? Simple tag handlers are much simpler to use than the classic tag handlers.
The classes and interfaces used to implement simple tag handlers are contained in the javax.servlet.jsp.tagext package. Simple tag handlers implement the SimpleTag interface. Interfaces can be used to take an existing Java object and make it a tag handler. For most newly created handlers, you would use the SimpleTagSupport classes as a base class.
The heart of a simple tag handler is a single methodโโdoTag() methodโโwhich gets invoked when the end element of the tag is encountered. Note that the default implementation of the doTag method() of SimpleTagSupport does nothing. And you want to override this doTag() method most of the time.
ATag t = new ATag(); t.setJSPContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); ... t.setJspBody(new JspFragment(...)) t.doTag();
The SimpleTag interface defines the basic protocol between a simple tag handler and servlet container. The servlet container invokes the setJspContext(), setParent(), and attribute setting methods() before setting JspFragment and then calling doTag() method
ATag t = new ATag(); t.setJSPContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); ... t.setJspBody(new JspFragment(...))รพ t.doTag();