Object-Oriented Enterprise Application Development JavaServer Pages Tag Libraries.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Advertisements

Enterprise Java: Developing JSP Custom Tag Libraries Developing JSP Custom Tag Libraries O'Reilly Conference on Enterprise Java, March 2001.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Creating Custom JSP Tag Libraries Taken from Core Servlets and JavaServer Pages from Prentice.
Object-Oriented Enterprise Application Development JavaServer Pages.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
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.
J2EE Java Server Pages (JSP) Presented by Bartosz Sakowicz.
DT211/3 Internet Application Development
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
J2EE Java Server Pages (JSP) Presented by Bartosz Sakowicz.
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.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
JSP Standard Tag Library
1 Web Based Programming Section 6 James King 12 August 2003.
Object Oriented Programming
OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy.
Basic Elements JSP For a Tutorial, see:
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
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.
Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
Introduction to Java Server Pages (JSPs) Robert Thornton.
1 JSP 1.2 Custom Tags. 2 Agenda ● What is and why custom tags? ● Components that make up custom tag architecture ● How to create custom tags? ● How does.
Custom Tags1 Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions Slides can be modified.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
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.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
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.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
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.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
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 Programming: Advanced Topics 1 Building Web Applications Chapter 13.
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
JSP Java Server Pages. Hello, !
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
Developing JavaServer Pages
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
Generate Dynamic Content On Cache Server
JSP Custom Tags Welcome.
Presentation transcript:

Object-Oriented Enterprise Application Development JavaServer Pages Tag Libraries

Topics During this class we will examine: Benefits of custom tag libraries Tag handlers Tag library descriptors Tags with attributes Complex tags

Introduction

Custom tags are JSP tags defined by a programmer and used by the content provider. Custom tags are only available under the 1.1 version of the JSP specification.

Benefits Custom tags can manipulate the JSP content. Complex JSP operations can be simplified. Allows the separation of roles between Java developers and content developers.

Elements To use a custom tag, you must provide three (3) components: Define a tag handler class Define a tag library descriptor Use the tag within a JSP

Tag Handlers

Definition For the JSP to use a new tag, it must have a tag handler class to interpret and respond to the tag. Each handler must implement the interface javax.servlet.jsp.tagext.Tag. This can be accomplished by extending the classes TagSupport or BodyTagSupport.

Definition (cont.) If the tag doesn't have a body or includes the body content as written, then the tag should inherit from the TagSupport class. The tag on the next slide demonstrates a basic tag handler that provides a simple text insertion.

Sample Code – ExampleTag (1 of 2) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class ExampleTag extends TagSupport { 6. public int doStartTag() { 7. try { 8. JspWriter out = pageContext.getOut(); 9. out.print("Custom Tag Example"); 10. }

Sample Code – ExampleTag (2 of 2) 11. catch (IOException ioe) { 12. ioe.printStackTrace(); 13. } 14. return (SKIP_BODY); 15.} 16.}

doStartTag If the tag doesn't have a body or simply includes the body content, then the tag only needs to override the doStartTag() method. Since the tag doesn't have a body, this method should return the value SKIP_BODY.

PageContext The pageContext variable is an object of type PageContext. The methods you'll most likely use are: getRequest getResponse getServletContext getSession getOut

JspWriter The JspWriter variable is a specific type of PrintWriter object. It is available to the JSP via the implicit variable, out. Because it can throw an IOException, the use of the JspWriter should be enclosed within a try…catch block.

Tag Library Descriptor

Definition Once the tag handler has been defined, you need to inform the server as to the existence of the tag and the handler to be used to execute that tag. Such information is stored within a tag library descriptor file.

Sample Code – SE452.tld (1 of 2) 1. 2.<!DOCTYPE taglib 3. PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library1.1//EN" " jsptaglibrary_1_1.dtd">

Sample Code – SE452.tld (2 of 2) SE A tag library for SE exampleSE se452.tags.ExampleTag A simple line of output

Tag Library Descriptors For tags without attributes, there are only four (4) elements of importance within the tag library descriptor file: name : defines the tag name to which the prefix of the taglib directive will be attached. tagclass : the fully qualified class name of the tag handler. info : a brief description of the tag. bodycontent : for tags without bodies, this value should be EMPTY.

JavaServer Pages

Tag Library Descriptors Once the tag handler and descriptor have been written, we can use the tag within a JSP. We need to do two (2) things to make this happen: Make the JSP aware of the tag library. Use the specific tag from the library within the JSP itself.

Sample Code – SE452Tag.jsp (1 of 1) SE452 Custom Tag JSP

Tags With Attributes

Justification It is often useful to add attributes to tags. For instance, the previous tag could be modified to display a message that is provided as an attribute to the tag itself. This is what we'll build for our next example.

Sample Code – MessageTag.jsp (1 of 1) Attribute Tag JSP

Tag Handler Class If you want a tag to support an attribute, then you need to provide a mutator method on the tag handler to manipulate that attribute. For instance, I want to have an attribute called text on my tag. So I need to provide a method of the following form: public void setText(String arg) { }

Sample Code – MessageTag (1 of 2) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class MessageTag 6.extends TagSupport { 7.private String text = "no.message.defined"; 8.public String getText() { 9. return (this.text); 10.}

Sample Code – MessageTag (2 of 2) 11.public void setText(String text) { 12. this.text = text; 13.} 14. public int doStartTag() { 15. try { 16. JspWriter out = pageContext.getOut(); 17. out.print( getText() ); 18. } 19. catch (IOException ioe) { 20. ioe.printStackTrace(); 21. } 22. return (SKIP_BODY); 23.} 24.}

Tag Library Descriptor We need to define an attribute tag for each attribute: name : the name of the attribute. required : true if the attribute must be provided and false (default) otherwise. rtexprvalue : true if the attribute can be a JSP expression or false (default) if it must be a fixed string.

Sample Code – SE452.tld (1 of 3) 1. 2.<!DOCTYPE taglib 3. PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library1.1//EN" " jsptaglibrary_1_1.dtd">

Sample Code – SE452.tld (2 of 3) SE A tag library for SE exampleSE se452.tags.ExampleTag A simple line of output 18.

Sample Code – SE452.tld (3 of 3) message se452.tags.MessageTag A simple line of output text 27. false

Tags With Bodies

Justification We often want to include body content within a tag. The tags we've done so far have ignored any such content.

Technique (1 of 2) To keep the body of the tag, the doStartTag() method should return EVAL_BODY_INCLUDE instead of SKIP_BODY. Technically, if the tag makes use of its body, then you should set the bodycontent attribute in the tag descriptor file to JSP instead of EMPTY.

Technique (2 of 2) We might want to perform additional work after the body of the tag has been processed. The doEndTag() method is fired when the closing tag of our custom tag is encountered. This method returns the value EVAL_PAGE if processing should continue or SKIP_PAGE if JSP processing should stop.

Sample Code – MessageTagBody (1 of 1) Attribute Tag JSP Just a heading…

Sample Code – MessageBodyTag (1 of 2) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class MessageBodyTag 6.extends TagSupport { 7.private String text = "no.message.defined"; 8.public String getText() { 9.return (this.text); 10.}

Sample Code – MessageBodyTag (2 of 2) 11.public void setText(String text) { 12. this.text = text; 13.} 14. public int doStartTag() { 15. try { 16. JspWriter out = pageContext.getOut(); 17. out.print( getText() ); 18. } 19. catch (IOException ioe) { 20. ioe.printStackTrace(); 21. } 22. return (EVAL_BODY_INCLUDE); 23.} 24.}

Sample Code – SE452.tld (1 of 1) messageBody se452.tags.MessageBodyTag A simple line of output text 9. false

Optional Tag Body It's inefficient to construct a new tag handler to perform essentially the same work as the original. We might want to conditionally include the body based on some criteria. We can use a single tag handler to either process or skip the body by simply changing the return value at run-time.

Manipulating Tag Bodies

Justification We might want to manipulate the contents of the tag body itself perhaps to add or modify the content.

Technique In this case, the TagSupport base class is insufficient. Instead, we must inherit from the BodyTagSupport class which itself extends the TagSupport class.

BodyTagSupport Class The major methods provided by the BodyTagSupport class are: doAfterBody : a method we override to handle the manipulation of the tag's body. getBodyContent : returns an object of type BodyContent that encapsulates information about the content of the tag body.

BodyContent Class The key methods provided by the BodyContent class are: getEnclosingWriter : returns the JspWriter object used by the doStartTag() and doEndTag() methods. getReader : returns a Reader that can read the tag's body. getString : returns a String containing the entire tag body.

Looping

Custom Loop Tag (1 of 2) In the 0.92 version of the JSP specification, there was a LOOP tag that could be used to iterate a specific number of times. This tag was removed as of version 1.0 of the JSP specification since Java could be embedded within the JSP anyway. It's such a useful construct, that I want to resurrect it by creating a custom LoopTag tag with a reps attribute.

Sample Code – LoopTag (1 of 3) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class LoopTag extends BodyTagSupport { 6. private int reps = 1; 7. public int getReps() { 8. return (this.reps); 9. }

Sample Code – LoopTag (2 of 3) 10. public void setReps(String reps) { 11. try { 12. this.reps = Integer.parseInt(reps); 13. } 14. catch (NumberFormatException nfe) { 15. } 16. }

Sample Code – LoopTag (3 of 3) 17. public int doAfterBody() { 18. if (reps-- >= 1) { 19. BodyContent body =getBodyContent(); 20. try { 21. JspWriter out = body.getEnclosingWriter(); 22. out.println(body.getString()); 23. body.clearBody(); 24. } 25. catch (IOException ioe) { 26. ioe.printStackTrace(); 27. } 28. return ( EVAL_BODY_TAG ); 29. } 30. return ( SKIP_BODY ); 31.} 32.}

Custom Loop Tag (2 of 2) Notice that since there isn't any custom content to be generated when the tag is first encountered, I don't provide a doStartTag() method.

Sample Code – LoopTag (rev.) (1 of 1) 1.public int doStartTag() { 2. try { 3. JspWriter out = pageContext.getOut(); 4. out.print( "Looping " + getReps() + " times\n" ); 5. } 6. catch (IOException ioe) { 7. ioe.printStackTrace(); 8. } 9. return ( EVAL_BODY_TAG ); 10.}

Sample Code – SE452.tld (1 of 1) loop se452.tags.LoopTag Loops reps 9. false 10. true

Sample Code – LoopTag.jsp (1 of 1) SE452 Custom Loop Tag

Nested Tags

Nesting Tags There are two (2) key things to know about nested tags: Use the findAncestorWithClass() method to locate the immediate ancestor of a nested tag. If an inner tag needs data from its enclosing tag, then it's the enclosing tag's responsibility to provide a mechanism for the nested tag to use to store and retrieve that data,

Custom If…Then…Else Tag (1 of 2) In the 0.92 version of the JSP specification, there were INCLUDEIF and EXCLUDEIF tags that were used for conditional content. These tag was removed as of version 1.0 of the JSP specification since Java could be embedded within the JSP anyway. It's such a useful construct, that I want to resurrect it by creating custom IF…THEN…ELSE tags.

Custom If…Then…Else Tag (2 of 2) This example is taken from your book and hasn't been modified very much. We will discuss the various tag handler classes and their associated tag library definitions needed to implement a conditional tag.

Tag Handlers For this custom tag, we'll need the following tag handlers: IfTag IfConditionTag IfThenTag IfElseTag

Sample Code – IfTag (1 of 3) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class IfTag extends TagSupport { 6. private boolean condition; 7. private boolean hasCondition = false;

Sample Code – IfTag (2 of 3) 8. public void setCondition(boolean condition) { 9. this.condition = condition; 10. this.hasCondition = true; 11. } 12. public boolean getCondition() { 13. return ( this.condition ); 14. }

Sample Code – IfTag (3 of 3) 15. public void setHasCondition(boolean flag) { 16. this.hasCondition = flag; 17. } 18. public boolean hasCondition() { 19. return ( hasCondition ); 20. } 21. public int doStartTag() { 22. return ( EVAL_BODY_INCLUDE ); 23. } 24.}

Sample Code – IfConditionTag (1 of 2) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class IfConditionTag extends BodyTagSupport { 6. public int doStartTag() throws JspTagException { 7. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class);

Sample Code – IfConditionTag (2 of 2) 8. if (parent == null) { 9. throw new JspTagException( "condition not inside if"); 10. } 11. return ( EVAL_BODY_TAG ); 12. } 13. public int doAfterBody() { 14. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class); 15. String bodyString = getBodyContent().getString(); 16. parent.setCondition(bodyString. trim().equals("true")); 17. return ( SKIP_BODY ); 18.} 19.}

Sample Code – IfThenTag (1 of 4) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class IfThenTag extends BodyTagSupport { 6. public int doStartTag() throws JspTagException { 7. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class);

Sample Code – IfThenTag (2 of 4) 8. if (parent == null) { 9. throw new JspTagException( "then not inside if"); 10. } 11. else if ( !parent.hasCondition() ) { 12. throw new JspTagException( "no condition before then"); 13. } 14. return ( EVAL_BODY_TAG ); 15. }

Sample Code – IfThenTag (3 of 4) 16. public int doAfterBody() { 17. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class); 18. if ( parent.getCondition() ) { 19. try { 20. BodyContent body = getBodyContent(); 21. JspWriter out = body.getEnclosingWriter(); 22. out.print(body.getString()); 23. }

Sample Code – IfThenTag (4 of 4) 24. catch (IOException ioe) { 25. ioe.printStackTrace(); 26. } 27. } 28. return ( SKIP_BODY ); 29.} 30.}

Sample Code – IfElseTag (1 of 4) 1.package se452.tags; 2.import javax.servlet.jsp.*; 3.import javax.servlet.jsp.tagext.*; 4.import java.io.*; 5.public class IfElseTag extends BodyTagSupport { 6. public int doStartTag() throws JspTagException { 7. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class);

Sample Code – IfElseTag (2 of 4) 8. if (parent == null) { 9. throw new JspTagException( "then not inside if"); 10. } 11. else if ( !parent.hasCondition() ) { 12. throw new JspTagException( "no condition before else"); 13. } 14. return ( EVAL_BODY_TAG ); 15. }

Sample Code – IfElseTag (3 of 4) 16. public int doAfterBody() { 17. IfTag parent = (IfTag) findAncestorWithClass(this, IfTag.class); 18. if ( !parent.getCondition() ) { 19. try { 20. BodyContent body = getBodyContent(); 21. JspWriter out = body.getEnclosingWriter(); 22. out.print(body.getString()); 23. }

Sample Code – IfElseTag (4 of 4) 24. catch (IOException ioe) { 25. ioe.printStackTrace(); 26. } 27. } 28. return ( SKIP_BODY ); 29.} 30.}

Sample Code – SE452.tld (1 of 2) if 3. se452.tags.IfTag 4. if tag condition se452.tags.IfConditionTag condition tag 12.

Sample Code – SE452.tld (2 of 2) then se452.tags.IfThenTag then tag else se452.tags.IfElseTag else tag 26.

Sample Code – IfTag.jsp (1 of 2) If...Then...Else Tag If Tag Samples

Sample Code – IfTag.jsp (2 of 2) true condition is true condition is false

Sample Code – NestedIfTag.jsp (1 of 3) If...Then...Else JSP If Tag Samples false 12.

Sample Code – NestedIfTag.jsp (2 of 3) false inner true:false inner true:true

Sample Code – NestedIfTag.jsp (3 of 3) outer false

Review During this class we have discussed: Benefits of custom tag libraries Tag handlers Tag library descriptors Tags with attributes Complex tags

Resources Core Servlets and JavaServer Pages Marty Hall, Prentice-Hall, Inc., ISBN:

Coming Attractions Next week we'll discuss JavaBeans and how they can be integrated into the presentation layer. Please read Chapter 13 in your text.