Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering

Slides:



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

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
JSP and web applications
CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
JavaServerPages Some examples. About JSP Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
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 Servlets and JSP Advanced topics Presented by Bartosz Sakowicz.
DT211/3 Internet Application Development
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
DT228/3 Web Development JSP: Directives and Scripting elements.
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.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
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.
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
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.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Which of the options locate the bean equivalent to the following action? (Select three) a request.getAttribute("address"); b request.getParameter("address");
Deploying CFML on J2EE Servers Vince Bonfanti President New Atlanta Communications, LLC.
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.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
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.
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
Chapter 7 Using Custom Tag Libraries and the JSP Standard Tag Library.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
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.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
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.
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.
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.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Introduction to Java Servlets
CS3220 Web and Internet Programming Custom Tag Library
JSP: Actions elements and JSTL
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Developing JavaServer Pages
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
JSP Directives 1-Jan-19.
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Presentation transcript:

Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering 23/01/2008

Fall 2007cs4202 Agenda Creating Custom JSP Tag Libraries Java-based tags –Components of a tag library –Basic tags –Tags that use attributes –Tags that use body content –Tags that optionally use body content JSP-based tags (tag files) –Components of a tag library –Basic tags –Tags that use attributes –Tags that use body content Source – & coreservlets.com

Fall 2007cs4203 Components That Make Up a Tag Library The Tag Handler Class –Java code that says what to output –Must implement javax.servlet.jsp.tagext.SimpleTag –Usually extends SimpleTagSupport –Goes in same directories as servlet class files and beans The Tag Library Descriptor File –XML file describing tag name, attributes, and implementing tag handler class –Goes under WEB-INF The JSP File –Imports a tag library (referencing URL of descriptor file) –Defines tag prefix –Uses tags

Fall 2007cs4204 Defining a Simple Tag Handler Class Extend the SimpleTagSupport class Import needed packages –import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; Override doTag –Obtain the JspWriter with getJspContext().getOut() –Use the JspWriter to generate output –Code gets called at request time  Tag instances are not reused like servlet instances, so no worry about race conditions, even if you have instance variables

Fall 2007cs4205 Defining a Simple Tag Handler Class: Example package coreservlets.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import java.math.*; import coreservlets.Primes; public class SimplePrimeTag extends SimpleTagSupport { protected int length = 50; public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); BigInteger prime = Primes.nextPrime(Primes.random(length)); out.print(prime); }

Fall 2007cs4206 Defining a Simple Tag Library Descriptor Start with XML header Top-level element is taglib –Just use tlib-version and short-name as in example Each tag defined by tag element with: –description, which gives short info. Optional. –name, which defines the base tag name. –tag-class, which gives the fully qualified class name of the tag handler. –body-content, which specifies if tag is standalone or contains content between start and end tag. You can have multiple tag entries in each TLD file Put TLD file somewhere under WEB-INF

Fall 2007cs4207 TLD File for SimplePrimeTag Don't memorize XML header and standard part; download and modify online version –The important thing is to know how to write tag entries –Place TLD file somewhere under WEB-INF

Fall 2007cs4208 Accessing Custom Tags From JSP Files Import the tag library –Specify location of TLD file –Define a tag prefix (namespace) Use the tags –  Tag name comes from TLD file  Prefix comes from taglib directive –E.g.,

Fall 2007cs4209 Using simplePrime Tag

Fall 2007cs42010 Using simplePrime Tag: Result

Fall 2007cs42011 Assigning Attributes to Tags Allowing tags like – Tags are still standalone –No body between start and end tags

Fall 2007cs42012 Attributes: The Tag Handler Class Use of an attribute called attribute1 simply results in a call to a method called setAttribute1 –Attribute value is supplied to method as a String Example –To support add the following to tag handler class: public void setAttribute1(String value1) { doSomethingWith(value1); }

Fall 2007cs42013 Attributes: PrimeTag.java

Fall 2007cs42014 Attributes: The Tag Library Descriptor File The tag element must contain a nested attribute element The attribute element has three further nested elements –name, a required element that defines the case-sensitive attribute name. –required, a required element that stipulates whether the attribute must always be supplied (true) or is optional (false). –rtexprvalue, an optional attribute that indicates whether the attribute value can be a JSP expression like (true) or whether it must be a fixed string (false). The default value is false.

Fall 2007cs42015 TLD File for PrimeTag

Fall 2007cs42016 Using prime Tag

Fall 2007cs42017 Using prime Tag: Result

Fall 2007cs42018 Including the Tag Body Simplest tags – Tags with attributes – Now – Scriptless JSP Content

Fall 2007cs42019 Including Tag Body: The Tag Handler Class Call getJspBody().invoke(null); public void doTag() throws... { JspWriter out = getJspContext().getOut(); out.print("..."); getJspBody().invoke(null); out.print("..."); }

Fall 2007cs42020 Including Tag Body: HeadingTag.java

Fall 2007cs42021 Including Tag Body: HeadingTag.java (Continued)

Fall 2007cs42022 Using Tag Body: The Tag Library Descriptor File Only difference is body-content element –Should be scriptless instead of empty Legal values for body-content –empty: no body content  Body content is ignored even if supplied –scriptless: body content is included  Can contain plain text, EL elements, and page directives only  No explicit scripting allowed ( ) –tagdependent: body content is processed by tag

Fall 2007cs42023 heading Tag: TLD File

Fall 2007cs42024 Using heading Tag

Fall 2007cs42025 Using heading Tag: Results

Fall 2007cs42026 Optional Tag Bodies First examples had no tag bodies –body-content: empty –doTag does not call invoke(null) Most recent examples always included tag bodies –body-content: scriptless –doTag calls invoke(null) Now: decide at request time whether or not to include tag body –body-content: scriptless –doTag conditionally calls invoke(null)  Depending on run-time information

Fall 2007cs42027 Optional Tag Bodies: DebugTag.java

Fall 2007cs42028 TLD File for DebugTag

Fall 2007cs42029 Using debug Tag

Fall 2007cs42030 Using debug Tag: Results

Fall 2007cs42031 Tag Files: Custom Tags Using JSP Syntax Two Approaches –When there is lots of logic, use Java to create output  Analogous to when you use servlets –When there is lots of formatting, use JSP to create output  Analogous to when you use JSP pages Pros –Very good for complex text formatting –Very concise Cons –Not good for complicated logic –Runs only in JSP 2.0 and later  Java-based versions had "classic" syntax that worked in older servers (e.g., BEA WebLogic 8.1, Oracle 9i AS)

Fall 2007cs42032 Simple Standalone Tags Java-based approach requires three pieces –Java code that overrides doTag to generate output  Strengths and weaknesses generally similar to those of servlets, but more cumbersome –Tag Library Descriptor (TLD) file that maps Java class name to tag name –JSP page that refers to specific location of TLD file JSP-based approach requires two pieces –JSP code (tag file) that shows result  /WEB-INF/tags/someName.tag –No TLD file: tag name taken from tag-file name –JSP page that refers to directory containing tag file  /WEB-INF/tags or a subdirectory thereof

Fall 2007cs42033 Tag Files Look just like regular JSP files, except –Must be located in (or under) WEB-INF/tags –Must be named blah.tag, not blah.jsp –You use instead of –You use predefined variable jspContext instead of pageContext  But you can cast it to PageContext  Other variables (request, response, etc.) are the same Example

Fall 2007cs42034 Tag Files: Tag Code (Simple Standalone Tag) WEB-INF/tags/simplePrime2.tag –Directory name is not arbitrary; must be in WEB-INF/tags or a subdirectory thereof

Fall 2007cs42035 Tag Files: Usage in JSP (Simple Standalone Tag)

Fall 2007cs42036 Tag Files: Result (Simple Standalone Tag)

Fall 2007cs42037 Tags with Attributes Java-based tags –For each attribute, add setAttributeName method to the Java class –Attribute value usually explicitly stored in instance variable (field) of Java class –List each attribute explicitly in TLD file JSP-based tags (tag files) –Each attribute listed in tag file  Can also list required and rtexprvalue (default false) –Attribute value automatically stored in scoped variable (for access from expression language) and in local variable (for access from Java code) –No TLD file

Fall 2007cs42038 Tag Files: Tag Code (Tag with Attributes)

Fall 2007cs42039 Tag Files: Usage in JSP (Tag with Attributes)

Fall 2007cs42040 Tag Files: Results (Tag with Attributes)

Fall 2007cs42041 Tags with Bodies Java-based tags –Change body-content from empty to scriptless (in TLD) –Call getJspBody().invoke(null) –Still need setter method and TLD entry for every attribute JSP-based tags (tag files) –Use to output tag body –No major syntax changes –Access to attributes still much simpler

Fall 2007cs42042 Tag Files: Tag Code (Tag with Body)

Fall 2007cs42043 Tag Files: Usage in JSP (Tag with Body)

Fall 2007cs42044 Tag Files: Results (Tag with Body)

Fall 2007cs42045 Open Source Tag Libraries JSP Standard Tag Library (JSTL) – AjaxTags – Jakarta Taglibs –  Benchmarking  Internationalization (I18N)  Database access  Sending  Populating/validating form fields  Regular expressions  Extracting data from other Web pages  Logging  Etc.

Fall 2007cs42046 Summary & Questions? That’s all for today!