Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor.

Slides:



Advertisements
Similar presentations
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Advertisements

JSP and web applications
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
The Web Warrior Guide to Web Design Technologies
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
Advanced Java Server Pages An more detailed look at JSPs.
JSP Tag Extensions And Java Bean. JSP Tag Extensions Tag extensions look like HTML (or rather, XML) tags embedded in a JSP page. They have a special meaning.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
UNIT-V The MVC architecture and Struts Framework.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JSP Java Server Pages Softsmith Infotech.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
Introduction to Java Server Pages (JSPs) Robert Thornton.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
Spell Checker web service (you build a web client that interacts with the service) The client uses a servlet class and a JSP page. The user passes information.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Pre-assessment Questions
In Class Assg 3 - Solution
In Class Assg 2 - solution
Presentation transcript:

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

chapter 112© 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

chapter 113© 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

chapter 114© 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

chapter 115© copyright Janson Industries 2011 ▮ Generic tag classes ▮ TagSupport ▮ BodyTagSupport (extends TagSupport) ▮ Tag interfaces ▮ Tag ▮ IterationTag (extends Tag) ▮ BodyTag (extends IterationTag) Generic Tag Classes

chapter 116© 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

chapter 117© copyright Janson Industries 2011

chapter 118© 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 { }

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

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

chapter 1111© copyright Janson Industries 2011 We will add this code to the doStartTag method A tag handler has been defined...

chapter 1112© 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

chapter 1113© 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

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

chapter 1115© copyright Janson Industries 2011 Create a new file in TagLibs

chapter 1116© copyright Janson Industries 2011

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

chapter 1118© 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

chapter 1119© copyright Janson Industries 2011 mFT c11.TestTag XML ▮ XML syntax very flexible. This works too: mFT c11.TestTag

chapter 1120© 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)

chapter 1121© copyright Janson Industries 2011 For instance, when a forEach tag was inserted from the palette..

chapter 1122© copyright Janson Industries RAD inserted the tag (notice the prefix c)...

chapter 1123© copyright Janson Industries as well as taglib directives The taglib directive indicates the URI for the correct taglib

chapter 1124© 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

chapter 1125© 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 ▮ TT.jsp (TagTest). In TT: ▮ A taglib directive for the URI and prefix ▮ The mFT tag with a prefix

chapter 1126© copyright Janson Industries 2011 Quick Review URI: 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 mFT c11.TestTag Tag definition identifies tag handler class to run

chapter 1127© 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

chapter 1128© copyright Janson Industries 2011 TT has some static text

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

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

chapter 1131© 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

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

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

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

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

chapter 1136© copyright Janson Industries 2011 When MyTagLib file created, didn’t specify the file extension as.tld

chapter 1137© copyright Janson Industries 2011 Rename and add.tld

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

chapter 1139© 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

chapter 1140© 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...

chapter 1141© copyright Janson Industries 2011

chapter 1142© copyright Janson Industries 2011 Refresh the browser

chapter 1143© 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

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

chapter 1145© copyright Janson Industries 2011 Change doStartTag to return EVAL_BODY_INCLUDE

chapter 1146© copyright Janson Industries 2011 Tag body content (hello) passed to the JSP Writer (and displayed) and doAfterBody run

chapter 1147© copyright Janson Industries 2011 Change doStartTag to EVAL_BODY_BUFFERED

chapter 1148© 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

chapter 1149© copyright Janson Industries 2011 To retrieve body text use bodyContent.getString()

chapter 1150© copyright Janson Industries 2011 When body content is accessed: 1. doInitBody run 2. content is returned (and displayed) 3. doAfterBody run Then doEndTag finishes (skips a line) and release is invoked

chapter 1151© copyright Janson Industries 2011 Can prove that doInitBody and doAfterBody are called by bodyContent.getString() After bodyContent.getString(), printed a tag Notice that it is printed after the doAfterBody msg then the release method invoked

chapter 1152© 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

chapter 1153© copyright Janson Industries 2011 Test Tag ▮ Change the tag so that the tag body content can be turned into a hyperlink addr ▮ With option to specify the addr ▮ Two tag attributes: ▮ One to specify whether to change to link ▮ One to specify the addr

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

chapter 1155© copyright Janson Industries 2011 Current Tag Definition

chapter 1156© 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

chapter 1157© 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

chapter 1158© copyright Janson Industries 2011 Link Attribute ▮ When Link is “t”, tag body text must be defined as an link ▮ If an addr parameter is specified that addr is used else a default address of will be used ▮ This is HTML to define an link

chapter 1159© copyright Janson Industries 2011 if (link.equals("t")){ pageContext.getOut().write("<A href=\"mailto:"); if (addr == null){ } else{ pageContext.getOut().write(addr + "\">"); } } doEndTag ▮ \" forces a " to be written onto page ▮ Will comment out the text printouts in all the methods

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

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

62 Move mouse over link to show mail to address

chapter 1163© copyright Janson Industries 2011 This time specify a address

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

chapter 1165© 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

chapter 1166© 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

chapter 1167© 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

chapter 1168© 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...

chapter 1169© 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

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

chapter 1171© copyright Janson Industries we can at least set the purchase date

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

chapter 1173© 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!!!

chapter 1174© 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

Chapter 8 75© copyright Janson Industries 2011 After Specifying School Inserting ViewController School & purch info InsertPO School.jsp User ProcessPO School (servlet) Sales DB AddPO.jsp Redirects School & purch info & PO # Model PO # TransTable (POBean) All PO info Creates School

chapter 1176© 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

chapter 1177© 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

chapter 1178© copyright Janson Industries and tie InsertPOSchool's form to the servlet ProcessPOSchool Insert custom tag (schoolsDDM) to display schools

chapter 1179© 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

chapter 1180© 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(" "); allPOsRS = tt.getAllPOs(); schools = allPOsRS.getString(1); pageContext.getOut().write( " " + schools + " "); while (allPOsRS.next()) { schools = allPOsRS.getString(1); pageContext.getOut().write("<option selected value=\"" + schools + "\">" + schools + " ");} pageContext.getOut().write(" "); } catch (IOException e1) {e1.printStackTrace(); } catch (SQLException e) {e.printStackTrace();} return super.doStartTag(); } }

chapter 1181© copyright Janson Industries 2011 Add XML to MyTagLib that defines schoolsDDM and ties it to c11.SchoolTagHandler

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

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

chapter 1184© copyright Janson Industries 2011 Scroll down list and click checkbox Must also specify a prefix

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

chapter 1186© copyright Janson Industries 2011 Run to test

chapter 1187© 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

chapter 1188© copyright Janson Industries 2011 AddPO ▮ Retrieves data (from POBean) and displays: ▮ School info ▮ Last purchase info ▮ Purchase date

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

chapter 1190© copyright Janson Industries 2011 language="java" contentType="text/html; charset=ISO " pageEncoding="ISO "%> AddPO Please enter the Purchase Order Information School: <input type="text" name="schoolTF" size="25" value="${POBean.school}"> Will invoke itself when button clicked Uses EL to retrieve info from POBean

chapter 1191© copyright Janson Industries 2011 Customer: <input type="text" name="custTF" size="30" value="${POBean.customer}"> Purchase Date: <input type="text" name="purchDataTF" size="10" value="${POBean.purchDate}"> Item: <input type="text" name="itemTF" size="30" value="${POBean.itemName}"> Quantity: <input type="text" name="qtyTF" size="5" value="${POBean.qty}"> Price: <input type="text" name="priceTF" size="7" value="${POBean.price}"> PO Number: Comments: <input type="text" name="commentsTF" size="30" maxlength="100"> <input type="submit" name="SubmitBtn" value="Add PO"> Gets rest of data from POBean and puts into textfields

chapter 1192© copyright Janson Industries 2011 AddPO ▮ When button clicked want to: ▮ Move data from text fields to PO Bean ▮ Write the PO to the DB

chapter 1193© copyright Janson Industries 2011 There is no setPurchDate that accepts a string

chapter 1194© copyright Janson Industries 2011 Need to create one

chapter 1195© 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

chapter 1196© 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

chapter 1197© copyright Janson Industries 2011 Add the XML to MyTagLib

chapter 1198© copyright Janson Industries 2011 In AddPO, insert the custom tag at the end of the if tag and add the page directive…

chapter 1199© copyright Janson Industries 2011 and define the prefix

chapter 11100© copyright Janson Industries 2011 Add the tag

chapter 11101© copyright Janson Industries 2011

chapter 11102© copyright Janson Industries 2011 Add a submit button to InsertPOSchool Must create ProcessPOSchool servlet

chapter 11103© 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 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 { }

chapter 11104© 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"); }

chapter 11105© copyright Janson Industries 2011 Run InsertPOSchool

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

chapter 11107© copyright Janson Industries 2011

chapter 11108© copyright Janson Industries 2011 Add PO info and click AddPO

chapter 11109© copyright Janson Industries 2011 Prove it worked by going back to InsertPOSchool. New school will be in DDM.

chapter 11110© 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

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

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

chapter 11113© copyright Janson Industries 2011 Run UsingDudeJSP on server

chapter 11114© 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

chapter 11115© 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

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

chapter 11117© copyright Janson Industries 2011 UsingDudeJSP sets a value for attribute dudecolor

chapter 11118© copyright Janson Industries 2011 Run UsingDudeJSP on server (may have to remove app from server to force reload of dude.tag, then refresh browser)

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

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

chapter 11121© copyright Janson Industries 2011 The tag accessed the variable

chapter 11122© copyright Janson Industries 2011 To access the body of a tag: Also, tag will display in brown to prove that tag retrieves and displays the content (not the JSP)

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

chapter 11124© copyright Janson Industries 2011

chapter 11125© 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

chapter 11126© 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)

chapter 11127© 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