Custom Tags Tag Handlers XML Tag Libraries Web

  • Slides: 127
Download presentation
Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor

Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor chapter 11 © copyright Janson Industries 2011 1

Why Custom Tags? ▮ Bean tags are limited: use. Bean, get. Property, and set.

Why Custom Tags? ▮ Bean tags are limited: use. Bean, get. Property, and set. Property ▮ 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 chapter 11 © copyright Janson Industries 2011 2

How do tags work? ▮ Tags can be tied to a java class called

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) ▮ do. Start. Tag ▮ do. Init. Body ▮ do. After. Body ▮ do. End. Tag ▮ release chapter 11 © copyright Janson Industries 2011 3

Generic Tag Classes ▮ A class is defined as a tag handler by extending

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 chapter 11 © copyright Janson Industries 2011 4

Generic Tag Classes ▮ Generic tag classes ▮ Tag. Support ▮ Body. Tag. Support

Generic Tag Classes ▮ Generic tag classes ▮ Tag. Support ▮ Body. Tag. Support (extends Tag. Support) ▮ Tag interfaces ▮ Tag ▮ Iteration. Tag (extends Tag) ▮ Body. Tag (extends Iteration. Tag) chapter 11 © copyright Janson Industries 2011 5

Generic Tag Classes ▮ Tag. Support (implements Iteration. Tag) subclasses inherit the methods: ▮

Generic Tag Classes ▮ Tag. Support (implements Iteration. Tag) subclasses inherit the methods: ▮ do. Start. Tag ▮ do. After. Body ▮ do. End. Tag ▮ release ▮ Body. Tag. Support (extends Tag. Support and implements Body. Tag) subclasses inherit the additional method: ▮ do. Init. Body chapter 11 © copyright Janson Industries 2011 6

chapter 11 © copyright Janson Industries 2011 7

chapter 11 © copyright Janson Industries 2011 7

Creating a tag handler ▮ Generates the following code: package c 11; import javax.

Creating a tag handler ▮ Generates the following code: package c 11; import javax. servlet. jsp. tagext. Body. Tag. Support; public class Test. Tag extends Body. Tag. Support { } ▮ Not too exciting ▮ Need to add tag methods chapter 11 © copyright Janson Industries 2011 8

Click Source, Override/Implement Methods, select these methods, then click OK chapter 11 © copyright

Click Source, Override/Implement Methods, select these methods, then click OK chapter 11 © copyright Janson Industries 2011 9

New methods simply call the superclass’ overridden methods chapter 11 © copyright Janson Industries

New methods simply call the superclass’ overridden methods chapter 11 © copyright Janson Industries 2011 10

We will add this code to the do. Start. Tag method A tag handler

We will add this code to the do. Start. Tag method A tag handler has been defined. . . chapter 11 © copyright Janson Industries 2011 11

Creating a Tag ▮. . however, creating a tag, tying a tag to a

Creating a Tag ▮. . 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) chapter 11 © copyright Janson Industries 2011 12

Creating a Tag Library ▮ We will create a folder to hold tag libraries

Creating a Tag Library ▮ We will create a folder to hold tag libraries in My. Web called Tag. Libs ▮ Within Tag. Libs we will create a tld file called My. Tag. Lib ▮ Within My. Tag. Lib, we will define a tag called m. FT (My. First. Tag) and associate it with Test. Tag chapter 11 © copyright Janson Industries 2011 13

Tag Libraries must reside in Web-INF chapter 11 © copyright Janson Industries 2011 14

Tag Libraries must reside in Web-INF chapter 11 © copyright Janson Industries 2011 14

Create a new file in Tag. Libs chapter 11 © copyright Janson Industries 2011

Create a new file in Tag. Libs chapter 11 © copyright Janson Industries 2011 15

chapter 11 © copyright Janson Industries 2011 16

chapter 11 © copyright Janson Industries 2011 16

Just need to add the XML to tie the tag to the tag handler

Just need to add the XML to tie the tag to the tag handler chapter 11 © copyright Janson Industries 2011 17

XML ▮ e. Xtensible Markup Language ▮ The "Duct Tape" of the Internet ▮

XML ▮ e. Xtensible 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 chapter 11 © copyright Janson Industries 2011 18

XML <taglib> <tag> <name> m. FT </name> <tagclass> c 11. Test. Tag </tagclass> </taglib>

XML <taglib> <tag> <name> m. FT </name> <tagclass> c 11. Test. Tag </tagclass> </taglib> ▮ XML syntax very flexible. This works too: chapter 11 <taglib> <tag> <name>m. FT</name> <tagclass>c 11. Test. Tag</tagclass> </taglib> © copyright Janson Industries 2011 19

Using a Tag ▮ A little more complicated ▮ Simply coding the tag in

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) chapter 11 © copyright Janson Industries 2011 20

For instance, when a for. Each tag was inserted from the palette. . chapter

For instance, when a for. Each tag was inserted from the palette. . chapter 11 © copyright Janson Industries 2011 21

. . . RAD inserted the tag (notice the prefix c). . . chapter

. . . RAD inserted the tag (notice the prefix c). . . chapter 11 © copyright Janson Industries 2011 22

. . . as well as taglib directives The taglib directive indicates the URI

. . . as well as taglib directives The taglib directive indicates the URI for the correct taglib chapter 11 © copyright Janson Industries 2011 23

taglib directive ▮ Multiple tag libraries can be accessed in a JSP ▮ Therefore,

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 chapter 11 © copyright Janson Industries 2011 24

URI ▮ Lastly, the URI is tied to an actual path/file (in the Web

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 My. Tag. Lib. tld to the URI http: //www. mytags. com/ ▮ TT. jsp (Tag. Test). In TT: ▮ A taglib directive for the URI and prefix ▮ The m. FT tag with a prefix chapter 11 © copyright Janson Industries 2011 25

Quick Review <z: m. FT></z: m. FT> Tag prefix identifies correct taglib directive <%@taglib

Quick Review <z: m. FT></z: m. FT> Tag prefix identifies correct taglib directive <%@taglib uri="http: //www. mytags. com/" prefix="z"%> Taglib directive tells server to go to deployment descriptor URI: http: //www. mytags. com/ Location: /WEB-INF/Tag. Libs/My. Tag. Lib. tld Deployment descriptor identifies the correct tag library <z: m. FT></z: m. FT> <name>m. FT</name> <tagclass>c 11. Test. Tag</tagclass> Tag definition identifies tag handler class to run chapter 11 © copyright Janson Industries 2011 26

Why define a URI? ▮ You could specify the taglib file in the taglib

Why define a URI? ▮ You could specify the taglib file in the taglib directive <%@taglib uri= "/WEB-INF/Tag. Libs/My. Tag. Lib. tld" prefix="z"%> ▮ 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 chapter 11 © copyright Janson Industries 2011 27

TT has some static text chapter 11 © copyright Janson Industries 2011 28

TT has some static text chapter 11 © copyright Janson Industries 2011 28

We insert the taglib directive and tag in the source code RAD not happy

We insert the taglib directive and tag in the source code RAD not happy with taglib directive because the URI isn’t in the deployment descriptor In fact, there is no deployment descriptor! chapter 11 © copyright Janson Industries 2011 29

To create a deployment descriptor, right click the project and select Java EE and

To create a deployment descriptor, right click the project and select Java EE and Generate Deployment Descriptor Stub chapter 11 © copyright Janson Industries 2011 30

A file called web. xml is created in WEB-INF Open by right clicking, select

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 chapter 11 © copyright Janson Industries 2011 31

Click Add… …from Add Item, select JSP Configuration and then click OK chapter 11

Click Add… …from Add Item, select JSP Configuration and then click OK chapter 11 © copyright Janson Industries 2011 32

Click Add again … …select Taglib and then click OK chapter 11 © copyright

Click Add again … …select Taglib and then click OK chapter 11 © copyright Janson Industries 2011 33

Specify the tag library file and the URI chapter 11 © copyright Janson Industries

Specify the tag library file and the URI chapter 11 © copyright Janson Industries 2011 34

Save the deployment descriptor. Reference added. chapter 11 © copyright Janson Industries 2011 35

Save the deployment descriptor. Reference added. chapter 11 © copyright Janson Industries 2011 35

When My. Tag. Lib file created, didn’t specify the file extension as. tld chapter

When My. Tag. Lib file created, didn’t specify the file extension as. tld chapter 11 © copyright Janson Industries 2011 36

Rename and add. tld chapter 11 © copyright Janson Industries 2011 37

Rename and add. tld chapter 11 © copyright Janson Industries 2011 37

Run TT. jsp on server chapter 11 © copyright Janson Industries 2011 38

Run TT. jsp on server chapter 11 © copyright Janson Industries 2011 38

Custom Tag ▮ In the future defining a tag will be faster because you

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 chapter 11 © copyright Janson Industries 2011 39

Tag Handler ▮ Tag handler methods are executed in a particular order ▮ For

Tag Handler ▮ Tag handler methods are executed in a particular order ▮ For instance, do. End. Tag executed after do. Start. Tag ▮ However, which tag handler methods are executed is based on whether the tag body is empty and what do. Start. Tag returns ▮ Let's prove. . . chapter 11 © copyright Janson Industries 2011 40

chapter 11 © copyright Janson Industries 2011 41

chapter 11 © copyright Janson Industries 2011 41

Refresh the browser chapter 11 © copyright Janson Industries 2011 42

Refresh the browser chapter 11 © copyright Janson Industries 2011 42

Tag Handler ▮ Notice, not all tag handler methods were run ▮ This is

Tag Handler ▮ Notice, not all tag handler methods were run ▮ This is because (by default) do. Start. Tag returns the inherited value SKIP_BODY ▮ If we: ▮ Put text in the tag body ▮ Change the returned value to EVAL_BODY_INCLUDE chapter 11 © copyright Janson Industries 2011 43

Put some text in the tag body chapter 11 © copyright Janson Industries 2011

Put some text in the tag body chapter 11 © copyright Janson Industries 2011 44

Change do. Start. Tag to return EVAL_BODY_INCLUDE chapter 11 © copyright Janson Industries 2011

Change do. Start. Tag to return EVAL_BODY_INCLUDE chapter 11 © copyright Janson Industries 2011 45

Tag body content (hello) passed to the JSP Writer (and displayed) and do. After.

Tag body content (hello) passed to the JSP Writer (and displayed) and do. After. Body run chapter 11 © copyright Janson Industries 2011 46

Change do. Start. Tag to EVAL_BODY_BUFFERED chapter 11 © copyright Janson Industries 2011 47

Change do. Start. Tag to EVAL_BODY_BUFFERED chapter 11 © copyright Janson Industries 2011 47

Content not passed to JSP writer and no other methods called However body content

Content not passed to JSP writer and no other methods called However body content is available in a server supplied Body. Content object called body. Content chapter 11 © copyright Janson Industries 2011 48

To retrieve body text use body. Content. get. String() chapter 11 © copyright Janson

To retrieve body text use body. Content. get. String() chapter 11 © copyright Janson Industries 2011 49

When body content is accessed: 1. do. Init. Body run 2. content is returned

When body content is accessed: 1. do. Init. Body run 2. content is returned (and displayed) 3. do. After. Body run Then do. End. Tag finishes (skips a line) and release is invoked chapter 11 © copyright Janson Industries 2011 50

Can prove that do. Init. Body and do. After. Body are called by body.

Can prove that do. Init. Body and do. After. Body are called by body. Content. get. String() After body. Content. get. String(), printed a tag Notice that it is printed after the do. After. Body msg then the release method invoked chapter 11 © copyright Janson Industries 2011 51

Tag Attributes ▮ Even though the tag handler has full access to the body

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 chapter 11 © copyright Janson Industries 2011 52

Test Tag ▮ Change the tag so that the tag body content can be

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 chapter 11 © copyright Janson Industries 2011 53

Tag Attributes ▮ Defined with XML in the tag definition <attribute> <name>link</name> <required>true</required> </attribute>

Tag Attributes ▮ Defined with XML in the tag definition <attribute> <name>link</name> <required>true</required> </attribute> <name>addr</name> </attribute> ▮ An attribute defined as required true, must be specified in the tag chapter 11 © copyright Janson Industries 2011 54

Current Tag Definition chapter 11 © copyright Janson Industries 2011 55

Current Tag Definition chapter 11 © copyright Janson Industries 2011 55

Tag Attributes ▮ For each attribute, must have a private String of the same

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 chapter 11 © copyright Janson Industries 2011 56

Tag Attributes private String link; private String addr; public String get. Link() { return

Tag Attributes private String link; private String addr; public String get. Link() { return link; } public void set. Link(String l) { link = l; } public String get. Addr() { return addr; } public void set. Addr(String a) { addr = a; } chapter 11 © copyright Janson Industries 2011 57

Link Attribute ▮ When Link is “t”, tag body text must be defined as

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] com will be used ▮ This is HTML to define an email link <A href="mailto: emailaddress"> chapter 11 © copyright Janson Industries 2011 58

do. End. Tag ▮ " forces a " to be written onto page if

do. End. Tag ▮ " forces a " to be written onto page if (link. equals("t")){ page. Context. get. Out(). write("<A href="mailto: "); if (addr == null){ page. Context. get. Out(). write("[email protected] com">"); } else{ page. Context. get. Out(). write(addr + "">"); } } ▮ Will comment out the text printouts in all the methods chapter 11 © copyright Janson Industries 2011 59

New tag handler code chapter 11 Commented out text printouts © copyright Janson Industries

New tag handler code chapter 11 Commented out text printouts © copyright Janson Industries 2011 60

Must specify the required attribute link chapter 11 © copyright Janson Industries 2011 61

Must specify the required attribute link chapter 11 © copyright Janson Industries 2011 61

Move mouse over link to show mail to address 62

Move mouse over link to show mail to address 62

This time specify a address chapter 11 © copyright Janson Industries 2011 63

This time specify a address chapter 11 © copyright Janson Industries 2011 63

Save TT, refresh browser, move mouse over link to show new "mail to" address

Save TT, refresh browser, move mouse over link to show new "mail to" address chapter 11 © copyright Janson Industries 2011 64

Business Change ▮ Want to be able to insert and pay POs online ▮

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 chapter 11 © copyright Janson Industries 2011 65

Business Change ▮ Need to insert PO info on the Web ▮ Could create

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 chapter 11 © copyright Janson Industries 2011 66

Inserting a PO ▮ Instead, display all schools (in a DDM using a custom

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 chapter 11 © copyright Janson Industries 2011 67

So when user starts the “create PO process” we display a dropdown menu with

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. . . chapter 11 © copyright Janson Industries 2011 68

The school name, buyer and previous purchase info is displayed Also set the purchase

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 chapter 11 © copyright Janson Industries 2011 69

And if they specify a new school. . . chapter 11 © copyright Janson

And if they specify a new school. . . chapter 11 © copyright Janson Industries 2011 70

. . . we can at least set the purchase date chapter 11 ©

. . . we can at least set the purchase date chapter 11 © copyright Janson Industries 2011 71

Inserting a PO View Sales DB Insert. PO School. jsp User DDM with Schools

Inserting a PO View Sales DB Insert. PO School. jsp User DDM with Schools School. Tag Handler Model All POs Trans. Table (POBean) 72 Chapter 8 © copyright Janson Industries 2011

Inserting a PO ▮ Notice that Trans. Table is used in the Web-base app

Inserting a PO ▮ Notice that Trans. Table is used in the Web-base app ▮ Because we did not embed the business logic/Model (Trans. Table) 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!!! chapter 11 © copyright Janson Industries 2011 73

Inserting a PO ▮ Need to copy Trans. Table class into My. Web/Java Resources/src/c

Inserting a PO ▮ Need to copy Trans. Table class into My. Web/Java Resources/src/c 11 and ▮ DBFile ▮ Access. File or DB 2 File or Oracle. File ▮ Invalid. Length. Exception ▮ These are the classes that comprise the client apps Model chapter 11 © copyright Janson Industries 2011 74

After Specifying School Inserting View School Controller Insert. PO School. jsp Redirects Creates User

After Specifying School Inserting View School Controller Insert. PO School. jsp Redirects Creates User Add. PO. jsp PO # Chapter 8 Process. PO School (servlet) School & purch info & PO # Trans. Table (POBean) Sales DB School & purch info All PO info Model © copyright Janson Industries 2011 75

Inserting a PO ▮ Trans. Table needs a new business function (get. School. Info)

Inserting a PO ▮ Trans. Table needs a new business function (get. School. Info) that ▮ Reads Tx. Table for a particular school ▮ Populates the Trans. Table fields associated with that school and the last PO ▮ This is how new business functions for classes come about – changes in business chapter 11 © copyright Janson Industries 2011 76

New Trans. Table Methods public void get. School. Info(String school) { read(" WHERE School

New Trans. Table Methods public void get. School. Info(String school) { read(" WHERE School = '" + school + "'"); this. get. School. Data. From. RS(); } private void get. School. Data. From. RS() { try { school = rs. get. String(1); customer = rs. get. String(2); item. Name = rs. get. String(5); qty = rs. get. Int(4); price = rs. get. Double(6); } catch (SQLException sqlex) { sqlex. print. Stack. Trace(); System. out. println("n. SQL exception on rs getn"); } © copyright Janson Industries 2011 77 } chapter 11

Insert custom tag (schools. DDM) to display schools . . . and tie Insert.

Insert custom tag (schools. DDM) to display schools . . . and tie Insert. POSchool's form to the servlet Process. POSchool chapter 11 © copyright Janson Industries 2011 78

schools. DDM Tag ▮ Invokes School. Tag. Handler ▮ School. Tag. Handler will ▮

schools. DDM Tag ▮ Invokes School. Tag. Handler ▮ School. Tag. Handler will ▮ Create a Trans. Table object ▮ Invoke get. All. POs ▮ Generates the HTML that defines a DDM with the schools names chapter 11 © copyright Janson Industries 2011 79

package c 11; import java. io. IOException; import java. sql. Result. Set; import java.

package c 11; import java. io. IOException; import java. sql. Result. Set; import java. sql. SQLException; import javax. servlet. jsp. Jsp. Exception; import javax. servlet. jsp. tagext. Tag. Support; public class School. Tag. Handler extends Tag. Support { Trans. Table tt = new Trans. Table(); Result. Set all. POs. RS; String schools; public int do. Start. Tag() throws Jsp. Exception { try { page. Context. get. Out(). write("<select name="School">"); all. POs. RS = tt. get. All. POs(); schools = all. POs. RS. get. String(1); page. Context. get. Out(). write( "<option selected value="" + schools + "">" + schools + "</option>"); while (all. POs. RS. next()) { schools = all. POs. RS. get. String(1); page. Context. get. Out(). write("<option selected value="" + schools + "">" + schools + "</option>"); } page. Context. get. Out(). write("</select>"); } catch (IOException e 1) {e 1. print. Stack. Trace(); } catch (SQLException e) {e. print. Stack. Trace(); } chapter 11 © copyright Janson Industries 2011 return super. do. Start. Tag(); } } 80

Add XML to My. Tag. Lib that defines schools. DDM and ties it to

Add XML to My. Tag. Lib that defines schools. DDM and ties it to c 11. School. Tag. Handler chapter 11 © copyright Janson Industries 2011 81

? is now resolved As an alternative to typing in the taglib directive and

? is now resolved As an alternative to typing in the taglib directive and tag… chapter 11 © copyright Janson Industries 2011 82

…click on Custom, then the location on the JSP to put the tag Need

…click on Custom, then the location on the JSP to put the tag Need to specify the URI, click Add chapter 11 © copyright Janson Industries 2011 83

Scroll down list and click www. mytags. com checkbox Must also specify a prefix

Scroll down list and click www. mytags. com checkbox Must also specify a prefix chapter 11 © copyright Janson Industries 2011 84

Select the tag, click Insert, then Close chapter 11 © copyright Janson Industries 2011

Select the tag, click Insert, then Close chapter 11 © copyright Janson Industries 2011 85

Run to test chapter 11 © copyright Janson Industries 2011 86

Run to test chapter 11 © copyright Janson Industries 2011 86

Inserting a PO ▮ New to add a button (when clicked, Process. POSchool, a

Inserting a PO ▮ New to add a button (when clicked, Process. POSchool, a servlet, is invoked) ▮ Process. POSchool ▮ Creates a Trans. Table object ▮ Defines it as a bean (POBean) ▮ If user entered a new school, sets bean school property to new school ▮ If not, invokes get. School. Info (passing the selected school) ▮ Redirects to Add. PO. jsp chapter 11 © copyright Janson Industries 2011 87

Add. PO ▮ Retrieves data (from POBean) and displays: ▮ School info ▮ Last

Add. PO ▮ Retrieves data (from POBean) and displays: ▮ School info ▮ Last purchase info ▮ Purchase date chapter 11 © copyright Janson Industries 2011 88

So if user chose Hard Knocks U… … last PO’s info displayed chapter 11

So if user chose Hard Knocks U… … last PO’s info displayed chapter 11 © copyright Janson Industries 2011 89

<!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 01 Transitional//EN" "http: //www. w 3.

<!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 01 Transitional//EN" "http: //www. w 3. org/TR/html 4/loose. dtd" > <%@page language="java" content. Type="text/html; charset=ISO-8859 -1" page. Encoding="ISO-8859 -1"%> <%@taglib uri="http: //java. sun. com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Add. PO</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="Add. PO. 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="school. TF" size="25" value="${POBean. school}"></td> chapter 11 © copyright Janson Industries 2011 </tr> Will invoke itself when button clicked Uses EL to retrieve info from POBean 90

<tr> <td align="right"><font size="4" color="blue" face="Verdana">Customer: </font></td> <td align="left"><input type="text" name="cust. TF" size="30" value="${POBean.

<tr> <td align="right"><font size="4" color="blue" face="Verdana">Customer: </font></td> <td align="left"><input type="text" name="cust. TF" size="30" value="${POBean. customer}"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">Purchase Date: </font></td> <td align="left"><input type="text" name="purch. Data. TF" size="10" value="${POBean. purch. Date}"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">Item: </font></td> <td align="left"><input type="text" name="item. TF" size="30" value="${POBean. item. Name}"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">Quantity: </font></td> <td align="left"><input type="text" name="qty. TF" size="5" value="${POBean. qty}"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">Price: </font></td> <td align="left"><input type="text" name="price. TF" size="7" value="${POBean. price}"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">PO Number: </font></td> <td align="left"><input type="text" name="p. ONum. TF" size="10"></td> </tr> <td align="right"><font size="4" color="blue" face="Verdana">Comments: </font></td> <td align="left"><input type="text" name="comments. TF" size="30" maxlength="100"></td> </tr> </tbody> </table> Gets rest of data from POBean and puts into textfields </div> <p align="center"><input type="submit" name="Submit. Btn" value="Add PO"></p> </form> </body> </html> chapter 11 © copyright Janson Industries 2011 91

Add. PO ▮ When button clicked want to: ▮ Move data from text fields

Add. PO ▮ When button clicked want to: ▮ Move data from text fields to PO Bean ▮ Write the PO to the DB chapter 11 © copyright Janson Industries 2011 92

<c: if test='${page. Context. request. method=="POST"}'> <jsp: set. Property name="POBean" property="school" param="school. TF" />

<c: if test='${page. Context. request. method=="POST"}'> <jsp: set. Property name="POBean" property="school" param="school. TF" /> <jsp: set. Property name="POBean" property="customer" param="cust. TF" /> <jsp: set. Property name="POBean" property="purch. Date" param="purch. Date. TF" /> There is no set. Purch. Date that accepts a string <jsp: set. Property name="POBean" /> <jsp: set. Property name="POBean" param="comments. TF" /> </c: if> chapter 11 property="item. Name" param="item. TF" property="qty" param="qty. TF"/> property="price" param="price. TF" property="PONum" param="p. ONum. TF" property="comments" © copyright Janson Industries 2011 93

Need to create one chapter 11 © copyright Janson Industries 2011 94

Need to create one chapter 11 © copyright Janson Industries 2011 94

insert. PO Tag ▮ Need another tag (insert. PO) to invoke the write method

insert. PO Tag ▮ Need another tag (insert. PO) to invoke the write method ▮ Create Insert. POHandler ▮ Add XML to tag library ▮ Put tag in Add. PO. jsp chapter 11 © copyright Janson Industries 2011 95

Retrieves POBean and invokes the write method package c 11; import javax. servlet. jsp.

Retrieves POBean and invokes the write method package c 11; import javax. servlet. jsp. Jsp. Exception; import javax. servlet. jsp. tagext. Tag. Support; public class Insert. POHandler extends Tag. Support { Trans. Table tt; public int do. Start. Tag() throws Jsp. Exception { tt = (Trans. Table)page. Context. get. Session(). get. Attribute("POBean"); tt. write(); return super. do. Start. Tag(); } } chapter 11 © copyright Janson Industries 2011 96

chapter 11 Add the XML to My. Tag. Lib © copyright Janson Industries 2011

chapter 11 Add the XML to My. Tag. Lib © copyright Janson Industries 2011 97

In Add. PO, insert the custom tag at the end of the if tag

In Add. PO, insert the custom tag at the end of the if tag and add the page directive… chapter 11 © copyright Janson Industries 2011 98

and define the prefix chapter 11 © copyright Janson Industries 2011 99

and define the prefix chapter 11 © copyright Janson Industries 2011 99

Add the tag chapter 11 © copyright Janson Industries 2011 100

Add the tag chapter 11 © copyright Janson Industries 2011 100

chapter 11 © copyright Janson Industries 2011 101

chapter 11 © copyright Janson Industries 2011 101

Add a submit button to Insert. POSchool chapter 11 Must create Process. POSchool servlet

Add a submit button to Insert. POSchool chapter 11 Must create Process. POSchool servlet © copyright Janson Industries 2011 102

package c 11; import import java. io. IOException; javax. servlet. Servlet. Exception; javax. servlet.

package c 11; import import java. io. IOException; javax. servlet. Servlet. Exception; javax. servlet. annotation. Web. Servlet; javax. servlet. http. Http. Servlet. Request; javax. servlet. http. Http. Servlet. Response; javax. servlet. http. Http. Session; @Web. Servlet("/Process. POSchool") public class Process. POSchool extends Http. Servlet { private static final long serial. Version. UID = 1 L; private String school = null; private Trans. Table new. PO; public Process. POSchool() { super(); } protected void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { chapter © copyright Janson Industries 2011 103 } 11

protected void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception,

protected void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { new. PO = new Trans. Table(); new. PO. set. Purch. Date(); school = request. get. Parameter("school. TF"); if (school. equals("")){ school = request. get. Parameter("School"); new. PO. get. School. Info(school); }else{ try { new. PO. set. School(school); } catch (Invalid. Length. Exception e) { e. print. Stack. Trace(); } } Http. Session session = request. get. Session(); session. set. Attribute("POBean", new. PO); response. send. Redirect("Add. PO. jsp"); } } chapter 11 © copyright Janson Industries 2011 104

Run Insert. POSchool chapter 11 © copyright Janson Industries 2011 105

Run Insert. POSchool chapter 11 © copyright Janson Industries 2011 105

Test by entering new school and click Submit chapter 11 © copyright Janson Industries

Test by entering new school and click Submit chapter 11 © copyright Janson Industries 2011 106

chapter 11 © copyright Janson Industries 2011 107

chapter 11 © copyright Janson Industries 2011 107

Add PO info and click Add. PO chapter 11 © copyright Janson Industries 2011

Add PO info and click Add. PO chapter 11 © copyright Janson Industries 2011 108

Prove it worked by going back to Insert. POSchool. New school will be in

Prove it worked by going back to Insert. POSchool. New school will be in DDM. chapter 11 © copyright Janson Industries 2011 109

Alternatives ▮ Custom tags can invoke JSP code instead of a Tag. Handler ▮

Alternatives ▮ Custom tags can invoke JSP code instead of a Tag. Handler ▮ 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 chapter 11 © copyright Janson Industries 2011 110

Folder and file created, JSP code entered in tag file chapter 11 © copyright

Folder and file created, JSP code entered in tag file chapter 11 © copyright Janson Industries 2011 111

Create JSP to use tag Taglib directive ids prefix and location of the tag

Create JSP to use tag Taglib directive ids prefix and location of the tag file chapter 11 © copyright Janson Industries 2011 112

Run Using. Dude. JSP on server chapter 11 © copyright Janson Industries 2011 113

Run Using. Dude. JSP on server chapter 11 © copyright Janson Industries 2011 113

Alternative ▮ Use this technique if JSP code will be used in many JSPs

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 chapter 11 © copyright Janson Industries 2011 114

Attributes ▮ To define, need an attribute directive in the tag file ▮ JSP

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 chapter 11 © copyright Janson Industries 2011 115

In tag file, attribute directive defines dudecolor Dudecolor value accessed using EL then assigned

In tag file, attribute directive defines dudecolor Dudecolor value accessed using EL then assigned to font's color attribute chapter 11 © copyright Janson Industries 2011 116

Using. Dude. JSP sets a value for attribute dudecolor chapter 11 © copyright Janson

Using. Dude. JSP sets a value for attribute dudecolor chapter 11 © copyright Janson Industries 2011 117

Run Using. Dude. JSP on server (may have to remove app from server to

Run Using. Dude. JSP on server (may have to remove app from server to force reload of dude. tag, then refresh browser) chapter 11 © copyright Janson Industries 2011 118

JSP creates request variable custname and sets value to John. (Also, need taglib directive

JSP creates request variable custname and sets value to John. (Also, need taglib directive to core) Changed color to green. chapter 11 © copyright Janson Industries 2011 119

Tag gets the custname value using EL and displays chapter 11 © copyright Janson

Tag gets the custname value using EL and displays chapter 11 © copyright Janson Industries 2011 120

The tag accessed the variable chapter 11 © copyright Janson Industries 2011 121

The tag accessed the variable chapter 11 © copyright Janson Industries 2011 121

To access the body of a tag: <jsp: do. Body/> Also, tag will display

To access the body of a tag: <jsp: do. Body/> Also, tag will display in brown to prove that tag retrieves and displays the content (not the JSP) chapter 11 © copyright Janson Industries 2011 122

Content in the tag body (changed dudecolor too) chapter 11 © copyright Janson Industries

Content in the tag body (changed dudecolor too) chapter 11 © copyright Janson Industries 2011 123

chapter 11 © copyright Janson Industries 2011 124

chapter 11 © copyright Janson Industries 2011 124

Can define a variable in the tag file with a variable directive (Must have

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 chapter 11 © copyright Janson Industries 2011 125

May have to remove app from server to force reload of dude. tag, then

May have to remove app from server to force reload of dude. tag, then refresh browser (or create a new server to run on) chapter 11 © copyright Janson Industries 2011 126

Points to Remember ▮ Custom tags used to simplify JSP coding ▮ If custom

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 chapter 11 © copyright Janson Industries 2011 127