CS3220 Web and Internet Programming Custom Tag Library

Slides:



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

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.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
JSP Java Server Pages Reference:
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.
JSP – Java Server Pages Part 2 Representation and Management of Data on the Internet.
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.
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.
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. 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,
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
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
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.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
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 ►
Chapter 11 Invoking Java Code with JSP Scripting Elements.
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.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
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.
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.
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 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,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
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.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Introduction to Java Servlets
JSP: Actions elements and JSTL
Handling Errors in Web Applications
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.
CS3220 Web and Internet Programming JSP Standard Tag Library (JSTL)
CS320 Web and Internet Programming JSP Standard Tag Library (JSTL)
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
Chengyu Sun California State University, Los Angeles
CS320 Web and Internet Programming Expression Language (EL)
CS320 Web and Internet Programming MVC Architecture
JSP Directives 1-Jan-19.
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
CS3220 Web and Internet Programming Expression Language (EL)
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Expression Language (EL)
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS3220 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles

JSTL Tags <ul> <c:forEach items="${a.numbers}" var="number"> <li>${number}</li> </c:forEach> </ul>

ASP.NET Tags <p>Enter a number from 1 to 100: <asp:TextBox id="tbox1" runat="server" /> <asp:Button Text="Submit" runat="server" /> </p>

ColdFusion Tags <cfform action = “cftextinput.cfm” method=“post”> <cftextinput name = “myInput” message = “This field must not be blank” required = “Yes” /> <input type = “Submit” value =“submit” /> </cfform>

What These Tags Have in Common Not standard HTML tags  custom tags Interpreted by application server, not browser Generate code and/or HTML text Simpler than code Easier to learn, especially by non-programmers Easier to parse, validate, and interpret by IDEs

Create JSP Custom Tags Implement a tag Using Java code Using a tag file (basically a JSP file) Declare the tag in a Tag Library Descriptor (TLD) file

Example: Add Tag cs3220.tld AddTag.java TaglibTest.jsp <cs3220:add op1=“10” op2=“20” /> 30 cs3220.tld AddTag.java TaglibTest.jsp

Tag Library Descriptor (TLD) A JSP custom tag must belong to a tag library TLD is an XML document that contains information about a tag library and the tags in the library TLDs are used by application servers and development tools to validate the tags

About TLD A TLD file must have the .tld suffix Locations for TLD WAR or unpackaged: WEB-INF or its subdirectories, except WEB-INF/classes and WEB-INF/lib JAR: META-INF or its subdirectories More information about TLD Chapter 7 and 8 in JSP 2.2 Specification Chapter 8 in Java EE 5 Tutorial

An Empty TLD <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <display-name>CS3220 Custom Tag Examples</display-name> <tlib-version>1.0</tlib-version> <short-name>cs3220</short-name> <uri>http://cs.calstatela.edu/cs3220/stu31</uri> </taglib>

Elements of TLD description display-name icon tlib-version short-name uri validator listener tag tag-file function Make sure the uri is unique. Since JSP 2.1, the order of elements in an TLD must follow the order of the elements in the TLD schema specification.

Declaration for AddTag <name>add</name> <tag-class>cs3220.tag.AddTag</tag-class> <body-content>empty</body-content> <attribute> <name>op1</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <name>op2</name> </tag>

Elements of <tag> name tag-class body-content JSP, scriptless, empty, tagdependent attribute required rtexprvalue – whether the attribute value can be a request-time expression (i.e. EL expression)

AddTag.java ... package cs320.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; public class AddTag extends SimpleTagSupport { int op1, op2; public AddTag() { op1 = op2 = 0; }

... AddTag.java public void setOp1( int i ) { op1 = i; } public void doTag() throws JspException, IOException JspWriter out = getJspContext().getOut(); out.print( op1+op2 );

TaglibTest.jsp <%@ taglib prefix="cs3220" uri="http://cs.calstatela.edu/cs3220/stu31" %> <!DOCTYPE html> <html> <head><title>TaglibTest</title></head> <body> The sum of ${param.a} and ${param.b} is <cs3220:add op1="${param.a}" op2="${param.b}" />. </body> </html>

How Does Server Find a Tag Implementation <cs3220:add> Custom tag uri="http://cs.calstatela.edu/cs3220/stu31" Tag Libraries cs3220.tld Tag Library Tag Library cs3220.tag.AddTag Tag Implementation

A Closer Look at SimpleTagSupport http://docs.oracle.com/cd/E17802_01/products/products/jsp/2.1/docs/jsp-2_1-pfd2/javax/servlet/jsp/tagext/SimpleTagSupport.html doTag() getJspContext() getJspBody()

Example: RequestInfo Tag <cs3220:requestInfo type=“method” /> GET Display request information JspContext  PageContext  Request

Example: Cap Tag Capitalize the body content of a tag <cs3220:cap>Hello World</cap> HELLO WORLD Capitalize the body content of a tag getJspBody()  JspFragment  invoke(java.io.Writer) ??

Tag Files A way to create a custom tag without writing Java code Tag files – must have .tag suffix WAR or unpackaged: WEB-INF/tags or its subdirectories JAR: META-INF/tags or its subdirectories Tag file in TLD <tag-file> <name>sometag</name> <path>/WEB-INF/tags/sometag.tag</path>

Example: Greeting Tag greeting.tag A JSP file <cs3220:greeting name=“cysun”>Hello</cs320:greeting> Hello, cysun! greeting.tag A JSP file tag and attribute directives

EL Functions Just like the function library in JSTL Any static methods can be used as EL functions EL function in TLD <function> <name> <function-class> <function-signature>

Example: Leet Talk cs3220.tag.Functions String leetTalk( String ) ${cs320:leetTalk(“fear my mad programming skills”)} ph34r my m4d pr0gr4mming zki11z! cs3220.tag.Functions String leetTalk( String )

When to Create Custom Tags Model Class: data representation Servlet: processing Custom Tag: reusable presentation logic For example Display file size in KB or MB Display Roman numerals

More Custom Tag Examples Tomcat JSP Examples - http://cs3.calstatela.edu:8080/examples/jsp/ JSTL Source code from http://tomcat.apache.org/taglibs/