1 1. 1 2 J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.

Slides:



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

JSP and web applications
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
© 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.
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.
CS 201 Functions Debzani Deb.
Object-Oriented Enterprise Application Development JavaServer Pages Tag Libraries.
UNIT-V The MVC architecture and Struts Framework.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
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.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Java for the WWW November 2012Slide Tag Libraries What are they? –Custom libraries Custom tags – see examples in following slides.
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.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
Introduction to Java Server Pages (JSPs) Robert Thornton.
Custom Tags1 Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions Slides can be modified.
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.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
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,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
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.
Definition CSS “Short for Cascading Style Sheets, a new feature being added to HTML that gives both Web site developers and users more control over how.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
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.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
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.
DT228/3 Web Development JSP: Actions elements and JSTL.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Chapter 14 Using JavaBeans Components in JSP Documents.
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.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
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.
Arrays and files BIS1523 – Lecture 15.
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
Intro to PHP & Variables
MSIS 655 Advanced Business Applications Programming
JSP Directives 1-Jan-19.
Presentation transcript:

1 1

1 2 J2EE JSP Custom Tag Libraries

1 3 JSP: Custom Tag Libraries

1 4 Since JSP 1.1 there has been a very powerful tool called custom tag libraries. The idea is to make complex server-side behavior available for use on a JSP page using a very basic syntax. We have glimpsed this power in the jsp:useBean combination we explored in the previous lecture. One major advantage of custom taglibs is their ability to manipulate JSP content. JSP: Custom Tag Libraries

1 5 To use custom JSP tags, you need to define three separate components: 1.) The tag handler class—this defines the tag’s behavior. 2.) The tag library descriptor file—that maps the XML element names to the tag implementation. 3.) The JSP file that uses the tag library. JSP: Custom Tag Libraries

1 6 JSP: Tag Handler Class

1 7 First of all, this is just another Java class. When you want to define a new tag, you must first write a Java class that does the work you expect your tag to do. This class must implement the javax.servlet.jsp.tagext.Tag interface. Usually, this is accomplished by extending the TagSupport or BodyTagSupport classes. JSP: Tag Handler Class

1 8 So, without further adieu, let’s look at an example: JSP: Tag Handler Class package mypackage; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; public class FirstTag extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.print( “My First Tag” ); } catch( IOException io ) { System.out.println( “FirstTag threw an IOException io” ); } return SKIP_BODY; } If you think about XML, it always has a Start Tag and an End Tag. Usually—but not always—there is a body between the tags. In this case, we’re just giving a start tag. When we return this constant int, “Skip any text you find between the body of the tag.” Notice, I’ve put the tag handler class in a package, so that will be a directory inside the classes directory.

1 9 After we have written our tag handler class, we need to compile it and place it in a directory below the WEB-INF/classes directory. JSP: Tag Handler Class Since we placed our FirstTag class in a package, we have to account for that package in the directory below classes.

1 10 JSP: Tag Library Descriptor File

1 11 So, we’ve finished with step 1, creating the class. Next, we have to announce the tag to the server. JSP: Tag Library Descriptor File JavaclassTaglib.tld The “shortname” is the library name, which will appear to the left of the colon. The tag’s “name” is the part that will appear to the right of the colon. This is the Java class that implements the tag. The class is usually placed in a package.

1 12 JSP: A JSP That Uses The Tag

1 13 Now, we have:  the Tag Handler class  a Tag Library Descriptor File and we still need  the JSP. JSP: A JSP That Uses The Tag

1 14 Here is the JSP that we will use to execute our first tag. Just like any other JSP, we place it in the javaclass directory. JSP: A JSP That Uses The Tag TestFirstTag.jsp The.tld file we just created must be referenced here at the top in a taglib Directive. The prefix parameter tells our JSP which “shortname” prefix to look for inside the.tld file. Since the URI for our.tld file does not specify a path, then we are saying it will be found in the same directory as the JSP.

1 15 Let’s review the process. 1.) Write the tag handler class. Compile. Put in WEB-INF/classes directory in the correct package. 2.) Write the Tag Library Definition file (.tld ), place in javaclass directory. 3.) Write the JSP, place in javaclass directory. The JSP must have the taglib directive before the tag is used. JSP: A JSP That Uses The Tag

1 16 JSP: A More Ambitious Tag

1 17 As the previous tag did not seem worth the trouble, we will look at one that begins to show you how valuable these taglibs are. JSP: A More Ambitious Tag

1 18 For the price of a tiny tag, this will insert a table into our JSP. Let’s remember our three steps: 1.) Tag Handler class 2.) Tag Library Descriptor File 3.) JSP that uses the taglib. JSP: A More Ambitious Tag

1 19 JSP: A More Ambitious Tag Please note that I had to “escape” the double quotes so that the class was able to compile.

1 20 JSP: A More Ambitious Tag I next add the tag to the table definition library. JavaclassTaglib.tld

1 21 Finally, here is the JSP that uses this new tag. JSP: A More Ambitious Tag I don’t know about you, but this part looks pretty easy. And, in a normal environment, all of these tags would have already been written. TestTableTag.jsp

1 22 This is the resulting page. JSP: A More Ambitious Tag

1 23 Recall that our doStartTag() method ended with the return value SKIP_BODY, which tells the tag to skip anything it found between the start and end tags. Let’s see if that really does what we expect. JSP: A More Ambitious Tag

1 24 We change our JSP so that it includes an end tag. JSP: A More Ambitious Tag from TableTag.java

1 25 JSP: More on the TLD

1 26 For simplicity, I omitted one of the tags that you should include if your body content is supposed to be empty. JSP: More on the TLD If the tag is expecting normal JSP content in the body, you use this: If the tag takes care of its own body:

1 27 JSP: Assigning Attributes to Tags

1 28 Although our examples so far have not used them, it is common to pass attributes to tags so the values of the attributes can be incorporated into the HTML that is generated in the tag. This is where our knowledge of the ways of JavaBeans comes in handy, because that’s exactly the way it works. JSP: Assigning Attributes to Tags

1 29 The tag handler that corresponds to the tag below would have to have methods named: public void setAttribute1( String value1 ) and public void setAttribute2( String value2 ) When the tag gets processed, it will automatically call these methods in the tag handler class. JSP: Assigning Attributes to Tags

1 30 Here we see how the method getMyAttribute() is inserted into the tag. JSP: Assigning Attributes to Tags Also, notice how nowhere does this class call “ setMyAttribute() ”

1 31 We’re not done yet—we need to add our ParameterTag class to the tld file. JSP: Assigning Attributes to Tags The name here is case sensitive. Remembering the rules of JavaBeans, it must match the getters and setters in your class. This optional attribute rtexprvalue shows whether it is okay for the value to be the result of a JSP Expression. When you have chosen to include an attribute in your tag, you need to add the attribute tag, which itself has three possible sub elements.

1 32 Last but not least we build a JSP to take advantage of our tag. JSP: Assigning Attributes to Tags See how this is going to work? Whatever we insert in myAttribute gets inserted into the tag when it’s made.

1 33 Granted, these have been pretty simple examples. But, hopefully, you can see how we could pass in multiple attributes and get some pretty complex structures. The tags give us canned code. Everybody shares the same code base and nothing is hard- coded in our JSPs. JSP: Assigning Attributes to Tags

1 34 JSP: Including the Tag Body

1 35 Up until now, all of our Custom Tags have omitted the body. Let’s see how the body can be included. Our doStartTag() methods have always returned the constant SKIP_BODY. JSP: Including the Tag Body return SKIP_BODY; } If, instead, we would have had our doStartTag() method return the constant EVAL_BODY_INCLUDE, then we can have the body contain JSP scripting elements, directives and actions. return EVAL_BODY_INCLUDE; }

1 36 To make sure you understand what I’m saying, here’s an example: body stuff can go in here including any legal JSP stuff. JSP: Including the Tag Body

1 37 Often, when you’re bothering to include the body, you also want to do something special with the end tag. Naturally, there is a method you can override called: doEndTag() For its return value, you can have the doEndTag() method either: return EVAL_PAGE; or return SKIP_PAGE; JSP: Including the Tag Body This means, after you’re done with the end tag, continue to evaluate the rest of the JSP page. This means, after you’re done with the end tag, abandon the rest of the JSP page.

1 38 Quickly, let’s look at the Tag Handler class: JSP: Including the Tag Body

1 39 JSP: Including the Tag Body Here’s the Start Tag This means the body should be evaluated.

1 40 And, finally, here’s the End Tag. JSP: Including the Tag Body

1 41 This is the.tld file. As you can see, all of the attributes need to be listed. JSP: Including the Tag Body

1 42 This example, when we see how it renders, will start to show how powerful this technique is. JSP: Including the Tag Body HeadingExample.jsp

1 43 JSP: Including the Tag Body HeadingExample.jsp

1 44 Package com.hp.bco.pl.wpa.taglib.hpweb WPA HPWeb Layout Plug-in Version 1.1.7