JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

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.
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.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
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.
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
1 Build a Web Application on J2EE. 2 J2EE Scenario Client – Web Server – EIS Resources Client – Web Server – EIS Resources Client – Application Server.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
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.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
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.
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.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
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.
Jordan Anastasiade. All rights reserved.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
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.
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.
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.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
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 ►
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
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.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
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 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
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.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
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.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
Apache Struts Technology A MVC Framework for Java Web Applications.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Custom Tag Library
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.
Scripted Page Web App Development (Java Server Pages)
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
MSIS 655 Advanced Business Applications Programming
JSP Directives 1-Jan-19.
Presentation transcript:

JSP Custom Tags

Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML

What is a Custom Tag? User defined JSP language elements (as opposed to standard tags) Encapsulates recurring tasks Distributed in a “custom made” tag library, which defines a set of related custom tags and contains the objects that implement the tags.

Why Use Custom Tags? Improved separation of presentation and implementation Reduce/eliminate scripting tags Encapsulate common or application-specific page idioms Provide an HTML-friendly layer of abstraction Only three data-oriented JSP actions:

Why Use Custom Tags? Custom tags separate presentation from business logic in that page content is created by page designers while the business logic's are captured in the form of custom tags. Now custom tags is not just for business logic but it can encapsulate other relatively complex functionality such as complex display, iteration, formatting and so on. Because custom tags encapsulate business logic, they are reusable and easier to maintain. In fact, in most cases, you will use custom tags that are created by someone else. Because custom tags hide complexity from page authors, it is easier to author pages. And there are many tools that support custom tags. Custom tags provide portable semantics.

Where does custom tags fit in? This picture shows how custom tags fit into the Web application architecture. Typically HTTP requests coming from the client are handled by the centralized controller, which in turn forwards them to JSP pages. The JSP pages then call server side objects for business logic processing. These server side objects can be either in the form of Java Beans or custom tags

Custom Tags against JavaBeans Pros – Custom tags can manipulate JSP contents while beans cannot – Complex operations can be reduced to a significantly simpler form with custom tags than beans Cons – Custom tags require quite a bit of more work to set up than do beans

What Can They Do? Generate content for a JSP page Access a page’s JavaBeans Introduce new JavaBeans Introduce new scripting variables Any combination of the above Plus anything else you can do with Java

What Can They Do? Custom tags have a rich set of features. They can Be customized via attributes passed from the calling page. Pass variables back to the calling page. Access all the objects available to JSP pages. Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag. Be nested within one another and communicate via private variables.

Anatomy of a Custom Tag Like the standard actions, custom tags follow XML syntax conventions or body content

Start and End Tags In XML and JSP is equivalent to Given this equivalence, the model used by the JSP custom tag API acts as if there is always a start tag and an end tag.

How Do They Work? Like most J2EE technologies, there are two aspects to the development of a custom tag library. Individual tags are implemented as Java classes called Tag Handlers. An XML file called the Tag Library Descriptor (TLD) maps a set of tag handlers into a library. The Java classes and TLD can be deployed individually or via a JAR file.

How Do They Work? When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed. JSP tag extensions let you create new tags that you can insert directly into a JavaServer Page just as you would the built-in tags you learned about in earlier chapters. The JSP 2.0 specification introduced Simple Tag Handlers for writing these custom tags.

Three things make up custom tag architecture Tag handler class – Defines tag's behaviour Tag library descriptor (TLD) – Maps XML elements to tag handler class JSP file (user of custom tags) – Uses tags To use a custom tag in a JSP page, you must: * Declare the tag library containing the tag before the usage of any custom tags from that tag library * Use custom tags using custom tag syntax in a JSP page It is assumed that the tag library has been configured and deployed with individual Web application or globally for all Web applications running on the server.

Tag Handlers A tag handler class must implement one of the following interfaces: javax.servlet.jsp.tagext.Tag javax.servlet.jsp.tagext.IterationTag javax.servlet.jsp.tagext.BodyTag Usually extends utility class javax.servlet.jsp.tagext.TagSupport or javax.servlet.jsp.tagext.BodyTagSupport class Located in the same directory as servlet class files /WEB-INF/classes/ A tag handler can optionally implement javax.servlet.jsp.tagext.TryCatchFinally Tag attributes are managed as JavaBeans properties (i.e., via getters and setters)

Tag Library Descriptor TLD defines tag syntax TLD maps tag names to handler classes TLD constrains tag body content TLD specifies tag attributes Attribute names and optional types Required vs. optional Compile-time vs. run-time values TLD specifies tag variables (name, scope, etc.) TLD declares tag library validator and lifecycle event handlers, if any

Tag Library Descriptor XML file that describes – tag name – bodycontent – attributes – tag handler class Container knows which tag is associated with which tag handler class via this file Located – Usually under WEB-INF directory Custom location can specified in JSP file – Via uri attribute of taglib directive

What is Tag Library? Is a collection of related tags – Tag(s) can be packaged in a Tag Library Typically packaged as a jar file containing – A tag library descriptor (TLD) e.g. META-INF/taglib.tld – *.class files for the tag handler class(es) – Any additional associated resource(s)

What is Tag Library? We already talked about tag library. A tag library is a collection of related tags. It is a customary that you package related tags into a single tag library. A tag library is typically packaged as a jar file which contains TLD file along with tag handler classes and any related classes. When a tag library is packaged as a jar file, the TLD file should reside under META-INF directory and the tag handler classes should reside under classes directory.

Declaring a tag library Include taglib directive before tags are used Syntax – – prefix: identifies the tag library – uri: uniquely identifies the tag library descriptor (TLD) directly or indirectly

Using Custom Tags Tags are made available within a JSP page via the taglib directive: Directive’s uri attribute references the TLD (established via WAR file’s web.xml ) Directive’s prefix attribute provides a local namespace for the TLD’s tags

Steps for implementing, using & deploying custom tags The steps you follow in order to implement and deploy custom tags are relatively straight-forward. First, you write tag handlers. Under JSP 2.0 architecture, tag handlers are Java classes. Second, you write so called tag library descriptor, TLD file, in short. Third, you package a set of tag handlers and TLD file into what is called tag library in either unpacked or packed form. Then, you write JSP pages that use these tags. Then you deploy the tag library along with JSP pages. Now let's talk about 3 things here by using examples - tag handler, TLD file, and JSP pages.

Example 1- Create "Hello" Tag Consider you want to define a custom tag named and you want to use it in the following fashion without a body: To create a custom JSP tag, you must first create a Java class that acts as a tag handler. So let us create HelloTag class as follows: package myTags; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } JSP tag extensions let you create new tags that you can insert directly into a JavaServer Page just as you would the built-in tags you learned about earlier. The JSP 2.0 specification introduced Simple Tag Handlers for writing these custom tags. To write a custom tag you can simply extend SimpleTagSupport class and override the doTag() method, where you can place your code to generate content for the tag.

Example 1- Create "Hello" Tag Above code has simple coding where doTag() method takes the current JspContext object using getJspContext() method and uses it to send "Hello Custom Tag!" to the current JspWriter object. Let us compile above class and copy it in a directory available in environment variable CLASSPATH. Here we have copied it to ROOT\WEB-INF\classes\myTags Note :You need to include the following to compile: TOMCAT_HOME/lib/jsp-api.jar to your CLASSPATH Finally create following tag library file: webapps\ROOT\WEB- INF\custom.tld Example TLD Hello myTags.HelloTag empty

Example 1- Create "Hello" Tag Now it's time to use above defined custom tag Hello in our JSP program(Hello.jsp) as follows: Note : Hello.jsp should also be saved in WEB-INF folder A sample custom tag Start Tomcat server and Try to call above JSP in a browser using: This should produce following result: Hello Custom Tag!

Accessing the Tag Body: You can include a message in the body of the tag as you have seen with standard tags. Consider you want to define a custom tag named and you want to use it in the following fashion with a body: This is message body Let us make following changes in above our tag code to process the body of the tag: package myTags; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException{ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); }}

Accessing the Tag Body: In this case, the output resulting from the invocation is first captured into a StringWriter before being written to the JspWriter associated with the tag. Now accordingly we need to change TLD file as follows: Now let us call above tag with proper body as follows: Example TLD with Body Hello myTags.HelloTag scriptless

Accessing the Tag Body: This will produce following result: A sample custom tag This is message body This is message body

Custom Tag Attributes: You can use various attributes along with your custom tags. To accept an attribute value, a custom tag class needs to implement setter methods, identical to JavaBean setter methods as shown below: package myTags; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { private String message; public void setMessage(String msg) { this.message = msg; } StringWriter sw = new StringWriter(); public void doTag()throws JspException, IOException { if (message != null) { /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); } else { /* use message from the body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); }

Custom Tag Attributes: The attribute's name is "message", so the setter method is setMessage(). Now let us add this attribute in TLD file using element as follows: Example TLD with Body Hello myTags.HelloTag scriptless message

Custom Tag Attributes: Now let us try following JSP with message attribute as follows: A sample custom tag This will produce following result: This is custom tag Hope above example makes sense for you. It would be worth to note that you can include following properties for an attribute:

Custom Tag Attributes: PropertyPurpose Name The name element defines the name of an attribute. Each attribute name must be unique for a particular tag. Required This specifies if this attribute is required or optional. It would be false for optional. Rtexprvalue Declares if a runtime expression value for a tag attribute is valid Type Defines the Java class-type of this attribute. By default it is assumed as String DescriptionInformational description can be provided. FragmentDeclares if this attribute value should be treated as a JspFragment.

Custom Tag Attributes:..... attribute_name false java.util.Date false..... attribute_name1 false java.util.Boolean false attribute_name2 true java.util.Date..... If you are using two attributes then you can modify your TLD as follows: Following is the example to specify properties related to an attribute:

THANK YOU