chapter 111© copyright janson industries 2011 custom tags ▮ tag handlers ▮ xml ▮ tag...
TRANSCRIPT
![Page 1: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/1.jpg)
chapter 11 1© copyright Janson Industries 2011
Custom Tags
▮ Tag Handlers
▮ XML
▮ Tag Libraries
▮ Web Deployment Descriptor
![Page 2: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/2.jpg)
chapter 11 2© copyright Janson Industries 2011
Why Custom Tags?
▮ Bean tags are limited: useBean, getProperty, and setProperty
▮ Often need functions in JSPs that require scripts or extensive JSTL tags
▮ Using custom tags instead:▮ Simplifies the coding in the JSPs▮ Makes functions accessible to all JSPs▮ Easier to change the function
![Page 3: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/3.jpg)
chapter 11 3© copyright Janson Industries 2011
How do tags work?
▮ Tags can be tied to a java class called a tag handler
▮ As the tag is read, various tag handler methods will be invoked (by the server) ▮ doStartTag▮ doInitBody▮ doAfterBody▮ doEndTag▮ release
![Page 4: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/4.jpg)
chapter 11 4© copyright Janson Industries 2011
Generic Tag Classes
▮ A class is defined as a tag handler by extending a generic tag class or implementing a tag interface
▮ By implementing an interface or extending a generic tag class, the tag handler gets useful methods and variables
![Page 5: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/5.jpg)
chapter 11 5© copyright Janson Industries 2011
▮ Generic tag classes▮ TagSupport▮ BodyTagSupport (extends TagSupport)
▮ Tag interfaces▮ Tag▮ IterationTag (extends Tag)▮ BodyTag (extends IterationTag)
Generic Tag Classes
![Page 6: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/6.jpg)
chapter 11 6© copyright Janson Industries 2011
Generic Tag Classes
▮ TagSupport (implements IterationTag) subclasses inherit the methods:▮ doStartTag▮ doAfterBody▮ doEndTag▮ release
▮ BodyTagSupport (extends TagSupport and implements BodyTag) subclasses inherit the additional method:▮ doInitBody
![Page 7: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/7.jpg)
chapter 11 7© copyright Janson Industries 2011
![Page 8: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/8.jpg)
chapter 11 8© copyright Janson Industries 2011
Creating a tag handler
▮ Generates the following code:
▮ Not too exciting
▮ Need to add tag methods
package c11;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class TestTag extends BodyTagSupport {
}
![Page 9: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/9.jpg)
chapter 11 9© copyright Janson Industries 2011
Click Source, Override/Implement Methods, select these methods, then
click OK
![Page 10: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/10.jpg)
chapter 11 10© copyright Janson Industries 2011
New methods simply call the superclass’ overridden
methods
![Page 11: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/11.jpg)
chapter 11 11© copyright Janson Industries 2011
We will add this code to the doStartTag method
A tag handler has been defined...
![Page 12: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/12.jpg)
chapter 11 12© copyright Janson Industries 2011
▮ ..however, creating a tag, tying a tag to a tag handler, and using the tag is a little more complicated
▮ A tag is defined and associated with a tag handler class in a tag library
▮ A tag library is a TLD (Tag Library Description) file that contains XML to associate tag(s) and tag handler(s) class(es)
Creating a Tag
![Page 13: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/13.jpg)
chapter 11 13© copyright Janson Industries 2011
▮ We will create a folder to hold tag libraries in MyWeb called TagLibs
▮ Within TagLibs we will create a tld file called MyTagLib
▮ Within MyTagLib, we will define a tag called mFT (MyFirstTag) and associate it with TestTag
Creating a Tag Library
![Page 14: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/14.jpg)
chapter 11 14© copyright Janson Industries 2011Tag Libraries must reside in Web-INF
![Page 15: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/15.jpg)
chapter 11 15© copyright Janson Industries 2011
Create a new file in TagLibs
![Page 16: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/16.jpg)
chapter 11 16© copyright Janson Industries 2011
![Page 17: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/17.jpg)
chapter 11 17© copyright Janson Industries 2011
Just need to add the XML to tie the tag to the tag
handler
![Page 18: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/18.jpg)
chapter 11 18© copyright Janson Industries 2011
XML
▮ eXtensible Markup Language
▮ The "Duct Tape" of the Internet
▮ XML similar in syntax to all MLs▮ Start Tags▮ End Tags
▮ To define a tag, use the tag, name, and tagclass tags
![Page 19: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/19.jpg)
chapter 11 19© copyright Janson Industries 2011
<taglib>
<tag>
<name>mFT</name>
<tagclass>c11.TestTag</tagclass>
</tag>
</taglib>
XML
▮ XML syntax very flexible. This works too:
<taglib>
<tag>
<name>
mFT
</name>
<tagclass>
c11.TestTag
</tagclass>
</tag>
</taglib>
![Page 20: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/20.jpg)
chapter 11 20© copyright Janson Industries 2011
Using a Tag
▮ A little more complicated
▮ Simply coding the tag in a JSP is easy
▮ However, must tell the server where the tag library is ▮ So the server can find/run the tag handler
▮ A taglib directive (in the JSP) tells the server where to find the tag library with a URI (uniform resource identifier)
![Page 21: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/21.jpg)
chapter 11 21© copyright Janson Industries 2011
For instance, when a forEach tag was inserted from the palette..
![Page 22: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/22.jpg)
chapter 11 22© copyright Janson Industries 2011
...RAD inserted the tag (notice the prefix
c)...
![Page 23: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/23.jpg)
chapter 11 23© copyright Janson Industries 2011
... as well as taglib directives
The taglib directive indicates the URI for the
correct taglib
![Page 24: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/24.jpg)
chapter 11 24© copyright Janson Industries 2011
taglib directive
▮ Multiple tag libraries can be accessed in a JSP
▮ Therefore, taglib directives must assign a prefix (nickname) for each taglib
▮ And when the custom tag is specified, the prefix must be included
![Page 25: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/25.jpg)
chapter 11 25© copyright Janson Industries 2011
URI
▮ Lastly, the URI is tied to an actual path/file (in the Web deployment descriptor)
▮ For this example, we will create:
▮ A Tag Lib Reference in the Web deployment descriptor that ties MyTagLib.tld to the URI http://www.mytags.com/
▮ TT.jsp (TagTest). In TT: ▮ A taglib directive for the URI and prefix▮ The mFT tag with a prefix
![Page 26: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/26.jpg)
chapter 11 26© copyright Janson Industries 2011
Quick Review
<z:mFT></z:mFT>
<%@taglib uri="http://www.mytags.com/" prefix="z"%>
URI: http://www.mytags.com/
Location: /WEB-INF/TagLibs/MyTagLib.tld
Tag prefix identifies correct taglib directive
Taglib directive tells server to go to deployment descriptor
Deployment descriptor identifies the correct tag library
<name>mFT</name>
<tagclass>c11.TestTag</tagclass>
Tag definition identifies tag handler class to run
<z:mFT></z:mFT>
![Page 27: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/27.jpg)
chapter 11 27© copyright Janson Industries 2011
Why define a URI?
▮ You could specify the taglib file in the taglib directive
▮ However, if file location or name changes, all JSPs that use the tag library must be updated
▮ If URI defined in deployment descriptor:▮ A change in the taglib location or name
means only updating the tag library reference in the Web deployment descriptor
<%@taglib uri= "/WEB-INF/TagLibs/MyTagLib.tld" prefix="z"%>
![Page 28: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/28.jpg)
chapter 11 28© copyright Janson Industries 2011
TT has some static text
![Page 29: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/29.jpg)
chapter 11 29© copyright Janson Industries 2011
RAD not happy with taglib directive because the URI isn’t in the deployment
descriptorIn fact, there is no deployment descriptor!
We insert the taglib directive and tag in the
source code
![Page 30: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/30.jpg)
chapter 11 30© copyright Janson Industries 2011
To create a deployment descriptor, right click the project and select Java EE and Generate Deployment Descriptor Stub
![Page 31: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/31.jpg)
chapter 11 31© copyright Janson Industries 2011
A file called web.xml is created in WEB-INF
Open by right clicking, select Open With, then Web Application 3.0 Deployment Descriptor
Editor
![Page 32: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/32.jpg)
chapter 11 32© copyright Janson Industries 2011
Click Add…
…from Add Item, select JSP Configuration and then click OK
![Page 33: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/33.jpg)
chapter 11 33© copyright Janson Industries 2011
Click Add again …
…select Taglib and then click OK
![Page 34: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/34.jpg)
chapter 11 34© copyright Janson Industries 2011
Specify the tag library file and the URI
![Page 35: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/35.jpg)
chapter 11 35© copyright Janson Industries 2011
Save the deployment descriptor. Reference added.
![Page 36: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/36.jpg)
chapter 11 36© copyright Janson Industries 2011
When MyTagLib file created, didn’t specify the file extension as .tld
![Page 37: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/37.jpg)
chapter 11 37© copyright Janson Industries 2011
Rename and add .tld
![Page 38: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/38.jpg)
chapter 11 38© copyright Janson Industries 2011
Run TT.jsp on server
![Page 39: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/39.jpg)
chapter 11 39© copyright Janson Industries 2011
Custom Tag
▮ In the future defining a tag will be faster because you won't have to:
▮ Create a tag library and deployment descriptor
▮ Define a URI in the deployment descriptor
▮ You still have to:
▮ Define a tag handler
▮ Use XML to define the tag in the tag library
![Page 40: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/40.jpg)
chapter 11 40© copyright Janson Industries 2011
Tag Handler
▮ Tag handler methods are executed in a particular order
▮ For instance, doEndTag executed after doStartTag
▮ However, which tag handler methods are executed is based on whether the tag body is empty and what doStartTag returns
▮ Let's prove...
![Page 41: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/41.jpg)
chapter 11 41© copyright Janson Industries 2011
![Page 42: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/42.jpg)
chapter 11 42© copyright Janson Industries 2011
Refresh the browser
![Page 43: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/43.jpg)
chapter 11 43© copyright Janson Industries 2011
Tag Handler
▮ Notice, not all tag handler methods were run
▮ This is because (by default) doStartTag returns the inherited value SKIP_BODY
▮ If we:▮ Put text in the tag body▮ Change the returned value to
EVAL_BODY_INCLUDE
![Page 44: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/44.jpg)
chapter 11 44© copyright Janson Industries 2011
Put some text in the tag body
![Page 45: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/45.jpg)
chapter 11 45© copyright Janson Industries 2011Change doStartTag to return
EVAL_BODY_INCLUDE
![Page 46: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/46.jpg)
chapter 11 46© copyright Janson Industries 2011
Tag body content (hello) passed to the JSP Writer (and displayed) and
doAfterBody run
![Page 47: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/47.jpg)
chapter 11 47© copyright Janson Industries 2011
Change doStartTag to EVAL_BODY_BUFFERED
![Page 48: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/48.jpg)
chapter 11 48© copyright Janson Industries 2011
Content not passed to JSP writer and no other methods called
However body content is available in a server supplied BodyContent object called
bodyContent
![Page 49: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/49.jpg)
chapter 11 49© copyright Janson Industries 2011
To retrieve body text use bodyContent.getString()
![Page 50: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/50.jpg)
chapter 11 50© copyright Janson Industries 2011
When body content is accessed:1. doInitBody run2. content is returned (and displayed)
3. doAfterBody runThen doEndTag finishes (skips a line) and release is invoked
![Page 51: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/51.jpg)
chapter 11 51© copyright Janson Industries 2011
Can prove that doInitBody and doAfterBody are called by bodyContent.getString()
After bodyContent.getString(), printed a <br> tag Notice that it is printed after the
doAfterBody msgthen the release method invoked
![Page 52: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/52.jpg)
chapter 11 52© copyright Janson Industries 2011
Tag Attributes
▮ Even though the tag handler has full access to the body content...
▮ ...body content not the way to control tag functions or pass info to tag
▮ Tag attributes used to specify tag parameters
![Page 53: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/53.jpg)
chapter 11 53© copyright Janson Industries 2011
Test Tag
▮ Change the tag so that the tag body content can be turned into a hyperlink email addr
▮ With option to specify the email addr
▮ Two tag attributes:
▮ One to specify whether to change to link
▮ One to specify the email addr
![Page 54: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/54.jpg)
chapter 11 54© copyright Janson Industries 2011
<attribute>
<name>link</name>
<required>true</required>
</attribute>
<attribute>
<name>addr</name>
</attribute>
Tag Attributes
▮ Defined with XML in the tag definition
▮ An attribute defined as required true, must be specified in the tag
![Page 55: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/55.jpg)
chapter 11 55© copyright Janson Industries 2011
Current Tag Definition
![Page 56: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/56.jpg)
chapter 11 56© copyright Janson Industries 2011
Tag Attributes
▮ For each attribute, must have a private String of the same name in the tag handler
▮ Tag handler needs setters and getters for each attribute variable
▮ If the attribute is specified in the tag, server will invoke setter in tag handler
▮ Tag handler must check variable to see if function should be performed
![Page 57: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/57.jpg)
chapter 11 57© copyright Janson Industries 2011
private String link;
private String addr;
public String getLink() {
return link; }
public void setLink(String l) {
link = l;}
public String getAddr() {
return addr;}
public void setAddr(String a) {
addr = a;}
Tag Attributes
![Page 58: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/58.jpg)
chapter 11 58© copyright Janson Industries 2011
Link Attribute
▮ When Link is “t”, tag body text must be defined as an email link
▮ If an addr parameter is specified that email addr is used else a default address of [email protected] will be used
▮ This is HTML to define an email link<A href="mailto:emailaddress">
![Page 59: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/59.jpg)
chapter 11 59© copyright Janson Industries 2011
if (link.equals("t")){
pageContext.getOut().write("<A href=\"mailto:");
if (addr == null){
pageContext.getOut().write("[email protected]\">");
}
else{
pageContext.getOut().write(addr + "\">"); }
}
doEndTag
▮ \" forces a " to be written onto page
▮ Will comment out the text printouts in all the methods
![Page 60: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/60.jpg)
chapter 11 60© copyright Janson Industries 2011
New tag handler code
Commented out text printouts
![Page 61: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/61.jpg)
chapter 11 61© copyright Janson Industries 2011
Must specify the required attribute link
![Page 62: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/62.jpg)
62
Move mouse over link to show mail to address
![Page 63: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/63.jpg)
chapter 11 63© copyright Janson Industries 2011
This time specify a address
![Page 64: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/64.jpg)
chapter 11 64© copyright Janson Industries 2011
Save TT, refresh browser, move mouse over link to show new "mail to" address
![Page 65: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/65.jpg)
chapter 11 65© copyright Janson Industries 2011
Business Change
▮ Want to be able to insert and pay POs online
▮ Create a Web based application (using custom tags) to do this
▮ Easier to do this because of MVC▮ Because Model classes are separate
from the View (i.e. Frame) classes, can easily incorporate Client App Model into Web App
![Page 66: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/66.jpg)
chapter 11 66© copyright Janson Industries 2011
Business Change
▮ Need to insert PO info on the Web
▮ Could create a static Web page requiring user to input all the PO info
▮ That would be a lot of repetitive work for the user
▮ Must enter Customer name, item, date for each PO
![Page 67: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/67.jpg)
chapter 11 67© copyright Janson Industries 2011
Inserting a PO
▮ Instead, display all schools (in a DDM using a custom tag) and if the school name exists, populate the JSP with
▮ Customer name
▮ Current date
▮ Last purchase info
▮ Must provide for a new school also
![Page 68: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/68.jpg)
chapter 11 68© copyright Janson Industries 2011
So when user starts the “create PO process” we display a dropdown menu with existing
schools
And if they select an already existing school like Death Valley Univ...
![Page 69: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/69.jpg)
chapter 11 69© copyright Janson Industries 2011
The school name, buyer and previous purchase info is displayed
Also set the purchase date to the current date and have a button to actually insert the PO
![Page 70: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/70.jpg)
chapter 11 70© copyright Janson Industries 2011
And if they specify a new school...
![Page 71: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/71.jpg)
chapter 11 71© copyright Janson Industries 2011
...we can at least set the purchase date
![Page 72: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/72.jpg)
Chapter 872
© copyright Janson Industries 2011
Inserting a PO
View
InsertPOSchool.jsp
User
SalesDB
All POs
Model
TransTable(POBean)
SchoolTagHandler
DDM with
Schools
![Page 73: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/73.jpg)
chapter 11 73© copyright Janson Industries 2011
Inserting a PO
▮ Notice that TransTable is used in the Web-base app
▮ Because we did not embed the business logic/Model (TransTable) in a Frame class, it can be used on the Web
▮ Imagine if you had embedded it:▮ You'd have to recode it for the Web▮ Every change would be done twice, once
for client app and once for Web app!!!
![Page 74: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/74.jpg)
chapter 11 74© copyright Janson Industries 2011
Inserting a PO
▮ Need to copy TransTable class into MyWeb/Java Resources/src/c11 and▮ DBFile▮ AccessFile or DB2File or OracleFile▮ InvalidLengthException
▮ These are the classes that comprise the client apps Model
![Page 75: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/75.jpg)
Chapter 8
75© copyright Janson Industries 2011
After Specifying School Inserting
View Controller
School& purch
info
InsertPOSchool.jsp
User
ProcessPOSchool
(servlet)SalesDB
AddPO.jsp
Redirects
School& purchinfo &PO #
ModelPO #
TransTable(POBean) All PO info
Creates
School
![Page 76: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/76.jpg)
chapter 11 76© copyright Janson Industries 2011
Inserting a PO
▮ TransTable needs a new business function (getSchoolInfo) that
▮ Reads TxTable for a particular school
▮ Populates the TransTable fields associated with that school and the last PO
▮ This is how new business functions for classes come about – changes in business
![Page 77: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/77.jpg)
chapter 11 77© copyright Janson Industries 2011
public void getSchoolInfo(String school) {
read(" WHERE School = '" + school + "'");
this.getSchoolDataFromRS();
}
private void getSchoolDataFromRS() {
try { school = rs.getString(1);
customer = rs.getString(2);
itemName = rs.getString(5);
qty = rs.getInt(4);
price = rs.getDouble(6);
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
System.out.println("\nSQL exception on rs get\n");
}
}
New TransTable Methods
![Page 78: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/78.jpg)
chapter 11 78© copyright Janson Industries 2011
...and tie InsertPOSchool's form to the servlet ProcessPOSchool
Insert custom tag (schoolsDDM) to display schools
![Page 79: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/79.jpg)
chapter 11 79© copyright Janson Industries 2011
schoolsDDM Tag
▮ Invokes SchoolTagHandler
▮ SchoolTagHandler will
▮ Create a TransTable object
▮ Invoke getAllPOs
▮ Generates the HTML that defines a DDM with the schools names
![Page 80: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/80.jpg)
chapter 11 80© copyright Janson Industries 2011
package c11;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class SchoolTagHandler extends TagSupport {
TransTable tt = new TransTable();
ResultSet allPOsRS;
String schools;
public int doStartTag() throws JspException {
try { pageContext.getOut().write("<select name=\"School\">");
allPOsRS = tt.getAllPOs();
schools = allPOsRS.getString(1);
pageContext.getOut().write(
"<option selected value=\"" + schools + "\">" +
schools + "</option>");
while (allPOsRS.next()) {
schools = allPOsRS.getString(1);
pageContext.getOut().write("<option selected value=\"" +
schools + "\">" + schools + "</option>");}
pageContext.getOut().write("</select>");
} catch (IOException e1) {e1.printStackTrace();
} catch (SQLException e) {e.printStackTrace();}
return super.doStartTag(); } }
![Page 81: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/81.jpg)
chapter 11 81© copyright Janson Industries 2011
Add XML to MyTagLib that defines schoolsDDM and ties it to
c11.SchoolTagHandler
![Page 82: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/82.jpg)
chapter 11 82© copyright Janson Industries 2011
? is now resolved
As an alternative to typing in the taglib directive and tag…
![Page 83: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/83.jpg)
chapter 11 83© copyright Janson Industries 2011
…click on Custom, then the location on the JSP to put the tag
Need to specify the URI, click Add
![Page 84: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/84.jpg)
chapter 11 84© copyright Janson Industries 2011
Scroll down list and click www.mytags.com checkbox
Must also specify a prefix
![Page 85: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/85.jpg)
chapter 11 85© copyright Janson Industries 2011
Select the tag, click Insert, then Close
![Page 86: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/86.jpg)
chapter 11 86© copyright Janson Industries 2011
Run to test
![Page 87: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/87.jpg)
chapter 11 87© copyright Janson Industries 2011
Inserting a PO
▮ New to add a button (when clicked, ProcessPOSchool, a servlet, is invoked)
▮ ProcessPOSchool ▮ Creates a TransTable object▮ Defines it as a bean (POBean)▮ If user entered a new school, sets bean
school property to new school▮ If not, invokes getSchoolInfo (passing the
selected school)▮ Redirects to AddPO.jsp
![Page 88: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/88.jpg)
chapter 11 88© copyright Janson Industries 2011
AddPO
▮ Retrieves data (from POBean) and displays:
▮ School info
▮ Last purchase info
▮ Purchase date
![Page 89: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/89.jpg)
chapter 11 89© copyright Janson Industries 2011
So if user chose Hard Knocks U…
… last PO’s info displayed
![Page 90: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/90.jpg)
chapter 11 90© copyright Janson Industries 2011
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>AddPO</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<p align="center">
<font size="5" color="black" face="Tahoma">Please enter the
Purchase Order Information </font>
</p>
<form action="AddPO.jsp" method="post">
<div align="center">
<table border="0">
<tbody>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">School:</font></td>
<td align="left"><input type="text" name="schoolTF" size="25"
value="${POBean.school}"></td>
</tr>
Will invoke itself when button
clickedUses EL to
retrieve info from POBean
![Page 91: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/91.jpg)
chapter 11 91© copyright Janson Industries 2011
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Customer:</font></td>
<td align="left"><input type="text" name="custTF" size="30"
value="${POBean.customer}"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Purchase
Date:</font></td>
<td align="left"><input type="text" name="purchDataTF" size="10"
value="${POBean.purchDate}"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Item:</font></td>
<td align="left"><input type="text" name="itemTF" size="30"
value="${POBean.itemName}"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Quantity:</font></td>
<td align="left"><input type="text" name="qtyTF" size="5"
value="${POBean.qty}"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Price:</font></td>
<td align="left"><input type="text" name="priceTF" size="7"
value="${POBean.price}"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">PO
Number:</font></td>
<td align="left"><input type="text" name="pONumTF" size="10"></td>
</tr>
<tr>
<td align="right"><font size="4" color="blue" face="Verdana">Comments:</font></td>
<td align="left"><input type="text" name="commentsTF"
size="30" maxlength="100"></td>
</tr>
</tbody>
</table>
</div>
<p align="center"><input type="submit" name="SubmitBtn"
value="Add PO"></p>
</form>
</body>
</html>
Gets rest of data from POBean and puts into
textfields
![Page 92: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/92.jpg)
chapter 11 92© copyright Janson Industries 2011
AddPO
▮ When button clicked want to:
▮ Move data from text fields to PO Bean
▮ Write the PO to the DB
![Page 93: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/93.jpg)
chapter 11 93© copyright Janson Industries 2011
<c:if test='${pageContext.request.method=="POST"}'>
<jsp:setProperty name="POBean" property="school" param="schoolTF" />
<jsp:setProperty name="POBean" property="customer" param="custTF" />
<jsp:setProperty name="POBean" property="purchDate" param="purchDateTF" />
<jsp:setProperty name="POBean" property="itemName" param="itemTF" />
<jsp:setProperty name="POBean" property="qty" param="qtyTF"/>
<jsp:setProperty name="POBean" property="price" param="priceTF" />
<jsp:setProperty name="POBean" property="PONum" param="pONumTF" />
<jsp:setProperty name="POBean" property="comments" param="commentsTF" />
</c:if>
There is no setPurchDate that accepts a string
![Page 94: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/94.jpg)
chapter 11 94© copyright Janson Industries 2011
Need to create one
![Page 95: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/95.jpg)
chapter 11 95© copyright Janson Industries 2011
insertPO Tag
▮ Need another tag (insertPO) to invoke the write method
▮ Create InsertPOHandler
▮ Add XML to tag library
▮ Put tag in AddPO.jsp
![Page 96: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/96.jpg)
chapter 11 96© copyright Janson Industries 2011
package c11;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class InsertPOHandler extends TagSupport {
TransTable tt;
public int doStartTag() throws JspException {
tt = (TransTable)pageContext.getSession().getAttribute("POBean");
tt.write();
return super.doStartTag();
}
}
Retrieves POBean and invokes the write method
![Page 97: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/97.jpg)
chapter 11 97© copyright Janson Industries 2011Add the XML to MyTagLib
![Page 98: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/98.jpg)
chapter 11 98© copyright Janson Industries 2011
In AddPO, insert the custom tag at the end of the if tag and add the page directive…
![Page 99: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/99.jpg)
chapter 11 99© copyright Janson Industries 2011
and define the prefix
![Page 100: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/100.jpg)
chapter 11 100© copyright Janson Industries 2011
Add the tag
![Page 101: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/101.jpg)
chapter 11 101© copyright Janson Industries 2011
![Page 102: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/102.jpg)
chapter 11 102© copyright Janson Industries 2011
Add a submit button to InsertPOSchool
Must create ProcessPOSchool servlet
![Page 103: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/103.jpg)
chapter 11 103© copyright Janson Industries 2011
package c11;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/ProcessPOSchool")
public class ProcessPOSchool extends HttpServlet {
private static final long serialVersionUID = 1L;
private String school = null;
private TransTable newPO;
public ProcessPOSchool() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
![Page 104: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/104.jpg)
chapter 11 104© copyright Janson Industries 2011
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
newPO = new TransTable();
newPO.setPurchDate();
school = request.getParameter("schoolTF");
if (school.equals("")){
school = request.getParameter("School");
newPO.getSchoolInfo(school);
}else{
try {
newPO.setSchool(school);
} catch (InvalidLengthException e) {
e.printStackTrace();
}
}
HttpSession session = request.getSession();
session.setAttribute("POBean", newPO);
response.sendRedirect("AddPO.jsp");
}
}
![Page 105: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/105.jpg)
chapter 11 105© copyright Janson Industries 2011
Run InsertPOSchool
![Page 106: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/106.jpg)
chapter 11 106© copyright Janson Industries 2011
Test by entering new school and click Submit
![Page 107: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/107.jpg)
chapter 11 107© copyright Janson Industries 2011
![Page 108: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/108.jpg)
chapter 11 108© copyright Janson Industries 2011
Add PO info and click AddPO
![Page 109: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/109.jpg)
chapter 11 109© copyright Janson Industries 2011
Prove it worked by going back to InsertPOSchool. New school will be in DDM.
![Page 110: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/110.jpg)
chapter 11 110© copyright Janson Industries 2011
Alternatives
▮ Custom tags can invoke JSP code instead of a TagHandler
▮ Need:▮ A new folder called Tags in WEB-INF
▮ A tag file in tags folder with:▮ the tag name as prefix ▮ .tag as suffix
▮ Put JSP code in the tag file
▮ Insert taglib directive and tag in a JSP
![Page 111: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/111.jpg)
chapter 11 111© copyright Janson Industries 2011
Folder and file created, JSP code entered in tag file
![Page 112: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/112.jpg)
chapter 11 112© copyright Janson Industries 2011
Taglib directive ids prefix and location of the tag file
Create JSP to use tag
![Page 113: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/113.jpg)
chapter 11 113© copyright Janson Industries 2011
Run UsingDudeJSP on server
![Page 114: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/114.jpg)
chapter 11 114© copyright Janson Industries 2011
Alternative
▮ Use this technique if JSP code will be used in many JSPs
▮ What actually happens is that the web container creates a tag handler from the JSP in the tag file
▮ Custom tag can receive attributes, pass variables back, & access all objects available to the JSP
![Page 115: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/115.jpg)
chapter 11 115© copyright Janson Industries 2011
Attributes
▮ To define, need an attribute directive in the tag file
▮ JSP using the tag specifies a value for attribute
▮ Tag file uses the attribute value
![Page 116: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/116.jpg)
chapter 11 116© copyright Janson Industries 2011
Dudecolor value accessed using EL then assigned to font's color attribute
In tag file, attribute directive defines dudecolor
![Page 117: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/117.jpg)
chapter 11 117© copyright Janson Industries 2011
UsingDudeJSP sets a value for attribute dudecolor
![Page 118: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/118.jpg)
chapter 11 118© copyright Janson Industries 2011
Run UsingDudeJSP on server (may have to remove app from server to force reload of
dude.tag, then refresh browser)
![Page 119: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/119.jpg)
chapter 11 119© copyright Janson Industries 2011
JSP creates request variable custname and sets value to John. (Also, need taglib directive
to core)
Changed color to green.
![Page 120: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/120.jpg)
chapter 11 120© copyright Janson Industries 2011
Tag gets the custname value using EL and displays
![Page 121: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/121.jpg)
chapter 11 121© copyright Janson Industries 2011
The tag accessed the variable
![Page 122: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/122.jpg)
chapter 11 122© copyright Janson Industries 2011
To access the body of a tag: <jsp:doBody/>Also, tag will display in brown to prove that
tag retrieves and displays the content (not the JSP)
![Page 123: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/123.jpg)
chapter 11 123© copyright Janson Industries 2011
Content in the tag body (changed dudecolor too)
![Page 124: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/124.jpg)
chapter 11 124© copyright Janson Industries 2011
![Page 125: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/125.jpg)
chapter 11 125© copyright Janson Industries 2011
Can define a variable in the tag file with a variable directive (Must have taglib directive
to core)Then use/access via tags and EL
![Page 126: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/126.jpg)
chapter 11 126© copyright Janson Industries 2011
May have to remove app from server to force reload of dude.tag, then refresh browser (or
create a new server to run on)
![Page 127: Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649e685503460f94b63ae0/html5/thumbnails/127.jpg)
chapter 11 127© copyright Janson Industries 2011
Points to Remember
▮ Custom tags used to simplify JSP coding
▮ If custom tag tied to tag handler:▮ Tag prefix identifies a taglib directive
▮ Taglib directive identifies the URI
▮ Deployment descriptor ties URI to tag library
▮ Tab library contains the tag definition
▮ Tag definition identifies tag handler to be run