JSTL JSP Standard Tag Library 12-Oct-15. W HAT IS JSTL? JSTL (JSP Standard Tag Libraries) is a collection of JSP custom tags developed by Java Community.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
PHP I.
Introducing JavaScript
Java Script Session1 INTRODUCTION.
Apache Struts Technology
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
DT211/3 Internet Application Development
DT228/3 Web Development JSP: Directives and Scripting elements.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Apache Struts Technology A MVC Framework for Java Web Applications.
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.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Introducing HTML & XHTML:. Goals  Understand hyperlinking  Understand how tags are formed and used.  Understand HTML as a markup language  Understand.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Eclipse is an open source IDE (Integrated Development Environment) for developing applications in Java, C/C++, HTML, Cobol, Perl, etc. The official Eclipse.
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.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
JSP Standard Tag Library
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
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.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
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.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Fall CIS 764 Database Systems Design L8. Web ….
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
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.
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
JSTL The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
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.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
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.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
Apache Struts Technology A MVC Framework for Java Web Applications.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
Web browser Web server HTML request Scripted pagedatabase The following components will be used: Apache Jakarta Tomcat servlet engine Derby JavaServer.
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.
Knowledge Byte In this section, you will learn about:
Intro to PHP & Variables
MSIS 655 Advanced Business Applications Programming
JSP Standard Tag Library
JavaServer Faces: The Fundamentals
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Presentation transcript:

JSTL JSP Standard Tag Library 12-Oct-15

W HAT IS JSTL? JSTL (JSP Standard Tag Libraries) is a collection of JSP custom tags developed by Java Community Process, The reference implementation is developed by the Jakarta project, jakarta.apache.org. Full JSTL – Contains many common and useful JSP custom tags – Particularly useful when you are using MVC, but the data contains a varying number of entries – Based on the Struts looping and logic tags – Not part of the JSP 1.2, 2.0, or 2.1 specs. It is a separate specification that requires a separate download. The JSP Standard Tag Library (JSTL) is a very new component released by Sun for JSP programming. JSTL allows you to program your JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to. JSTL can do nearly everything that regular JSP scriptlet code can do.

I NSTALLING JSTL To use JSTL, you must have a JSP 1.2 (or higher) container installed. One of the most commonly used JSP containers is the Apache Tomcat Web server. You can obtain a copy of Tomcat from Using Tomcat alone will allow you to use regular JSP scriptlet code. To use JSTL, you must install JSTL into Tomcat. JSTL can be obtained from the same source as Tomcat. The main URL for JSTL is You get jstl-api-1.2.jar and jstl-impl-1.2.jar from this site. To use JSTL, you must unzip the distribution files and install them into the correct locations within Tomcat. For Older Versions, we will also need jstl.jar and standard.jar which you can download from In fact choose the binaries : Note : For more details on versions and taglibs, refer to the preface page at the end of this sesssion.

I NSTALLING JSTL To properly install JSTL for use with Tomcat, follow these three steps: 1. Copy the JSTL JAR files to Tomcat's lib directory. If you are using Windows, the likely location of your lib directory is C:\Apache Tomcat 6.0\webapps\ROOT\WEB-INF\lib. There are a number of JAR files included with the JSTL release. You should copy each of these JAR files to the Tomcat JAR directory. In Tomcat 6.0, copy it to C:\Apache Tomcat 6.0\lib and if you are using older version say Tomcat5.5, copy it to C:\Apache Tomcat 5.5\common\lib 2. Copy the JSTL TLD files to Tomcat's web-inf directory. The web-inf directory is likely at this location: C:\Apache Tomcat 6.0\webapps\ROOT\WEB-INF. If you examine the JSTL distribution files, you should notice eight files that end with the TLD extension. All eight files should be copied to your web-inf directory.

I NSTALLING JSTL 3. Modify the web.xml file to include the TLD files. Finally, you must modify your web.xml, and add an entry for all eight of the tag libraries that you added. This consists of adding directives inside the main directive. The entries you should add are listed here. /WEB-INF/fmt.tld /WEB-INF/fmt-rt.tld /WEB-INF/c.tld

I NSTALLING JSTL /WEB-INF/c-rt.tld /WEB-INF/sql.tld /WEB-INF/sql-rt.tld /WEB-INF/x.tld /WEB-INF/x-rt.tld

I NSTALLING JSTL After completing the preceding three steps, you are now ready to test your installation of JSTL. This can be done by creating a JSP page that uses JSTL. A very simple example would be the "count to ten" example. You should place your JSP file inside the Webroot directory (C:\Apache Tomcat 6.0\webapps\ ROOT). Once the Tomcat server is started, you should be able to browse to to see your page. If you do not have JSTL installed correctly, there will likely be no error message. If JSTL is not interpreting your tags, they will be passed through directly to the Web browser. The Web browser will interpret these tags as unknown HTML tags. Most browsers simply ignore the unknown HTML tags.

JSTL was introduced to allow JSP programmers to program using tags rather than Java code. To show why this is preferable, a quick example is in order. We will examine a very simple JSP page that counts to ten. We will examine this page both as regular scriptlet-based JSP, and then as JSTL. When the count to ten example is programmed using scriptlet based JSP, the JSP page appears as follows.` Count to 10 in JSP scriptlet COUNT TO TEN E XAMPLE U SING SCRIPTLET :

As you can see from the preceding example, using scriptlet code produces page source code that contains a mix of HTML tags and Java statements. There are several reasons why this mixing of programming styles is not optimal. The primary reason that it is not optimal to mix scriptlet and tag- based code is readability. This readability applies both to humans and computers. JSTL allows the human programmer to look at a program that consists entirely of HTML and HTML-like tags. The readability of JSP scriptlet code does not just apply to human beings. The mixing of scriptlet and HTML code is also hard for computers to read. This is particularly true of HTML authoring tools such as Someone's Dreamweaver and Microsoft FrontPage. Currently, most HTML authoring tools will segregate JSP scriptlet code as non-editable blocks. The HTML authoring tools usually do not modify the JSP scriptlet code directly.

COUNT TO TEN E XAMPLE U SING JSTL: Count to 10 Example (using JSTL) The following code shows how the count to ten example would be written using JSTL. As you can see, this code listing is much more constant, as only tags are used. HTML and JSTL tags are mixed to produce the example.

When you examine the preceding source code, you can see that the JSP page consists entirely of tags. The above code makes use of HTML tags such as and. The use of tags is not confined just to HTML tags. We can modify that code to print numbers from 1 to n. The code is given in the next slide. This code also makes use of JSTL tags such as and. In this article you will be introduced to some of the basics of JSTL.

P RINT 1 TO N E XAMPLE U SING JSTL: Hello...Printing numbers till N= The following code shows how the count to n example would be written using JSTL. As you can see, this code listing is much more constant, as only tags are used. HTML and JSTL tags are mixed to produce the example. You can just use to see the result.

T HE JSTL T AG L IBRARIES The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. JSTL is often spoken of as a single-tag library. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. It also provides a framework for integrating existing custom tags with JSTL tags. The JSTL tags can be classified, according to their functions, into following JSTL tag library groups that can be used when creating a JSP page:

T HE JSTL T AG L IBRARIES The JSTL tags can be classified, according to their functions, into following JSTL tag library groups that can be used when creating a JSP page: Core Tags Formatting tags SQL tags XML tags JSTL Functions

T HE JSTL T AG L IBRARIES Core Tag Library —Contains tags that are essential to nearly any Web application. Examples of core tag libraries include looping, expression evaluation, and basic input and output. Formatting/Internationalization Tag Library —Contains tags that are used to parse data. Some of these tags will parse data, such as dates, differently based on the current locale. Database Tag Library —Contains tags that can be used to access SQL databases. These tags are normally used only to create prototype programs. This is because most programs will not handle database access directly from JSP pages. Database access should be embedded in EJBs that are accessed by the JSP pages. XML Tag Library —Contains tags that can be used to access XML elements. Because XML is used in many Web applications, XML processing is an important feature of JSTL. JSTL Functions -- JSTL includes a number of standard functions, most of which are common string manipulation functions.

C ORE T AGS : The core group of tags are the most frequently used JSTL tags. Following is the syntax to include JSTL Core library in your JSP: There are following Core JSTL Tags: TagDescription Like, but for expressions. Sets the result of an expression evaluation in a 'scope' Removes a scoped variable (from a particular scope, if specified). Catches any Throwable that occurs in its body and optionally exposes it. Simple conditional tag which evalutes its body if the supplied condition is true. Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and Subtag of that includes its body if its condition evalutes to 'true'. Subtag of that follows tags and runs only if all of the prior conditions evaluated to 'false'. Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality. Iterates over tokens, separated by the supplied delimeters. Adds a parameter to a containing 'import' tag's URL. Redirects to a new URL. Creates a URL with optional query parameters

F ORMATTING TAGS : The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP: Following is the list of Formatting JSTL Tags: TagDescription To render numerical value with specific precision or format. Parses the string representation of a number, currency, or percentage. Formats a date and/or time using the supplied styles and pattern Parses the string representation of a date and/or time Loads a resource bundle to be used by its tag body. Stores the given locale in the locale configuration variable. Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable. Specifies the time zone for any time formatting or parsing actions nested in its body. Stores the given time zone in the time zone configuration variable To display an internationalized message. Sets the request character encoding

SQL TAGS : The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server. Following is the syntax to include JSTL SQL library in your JSP: Following is the list of SQL JSTL Tags: TagDescription Creates a simple DataSource suitable only for prototyping Executes the SQL query defined in its body or through the sql attribute. Executes the SQL update defined in its body or through the sql attribute. Sets a parameter in an SQL statement to the specified value. Sets a parameter in an SQL statement to the specified java.util.Date value. Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML TAGS : The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JSTL XML library in your JSP: The JSTL XML tag library has custom tags for interacting with XML data. This includes parsing XML, transforming XML data, and flow control based on XPath expressions. Before you proceed with the examples, you would need to copy following two XML and XPath related libraries into your \lib: XercesImpl.jar: Download it from xalan.jar: Download it from

TagDescription Like, but for XPath expressions. Use to parse XML data specified either via an attribute or in the tag body. Sets a variable to the value of an XPath expression. Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored. To loop over nodes in an XML document. Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and Subtag of that includes its body if its expression evalutes to 'true' Subtag of that follows tags and runs only if all of the prior conditions evaluated to 'false' Applies an XSL transformation on a XML document Use along with the transform tag to set a parameter in the XSLT stylesheet Following is the list of XML JSTL Tags:

JSTL F UNCTIONS : JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions library in your JSP: Following is the list of JSTL Functions: FunctionDescription fn:contains()Tests if an input string contains the specified substring. fn:containsIgnoreCase( ) Tests if an input string contains the specified substring in a case insensitive way. fn:endsWith()Tests if an input string ends with the specified suffix. fn:escapeXml()Escapes characters that could be interpreted as XML markup. fn:indexOf()Returns the index within a string of the first occurrence of a specified substring. fn:join()Joins all elements of an array into a string. fn:length()Returns the number of items in a collection, or the number of characters in a string. fn:replace()Returns a string resulting from replacing in an input string all occurrences with a given string. fn:split()Splits a string into an array of substrings.

In this session, we will only take a brief look at a few of the core tags. We will examine a simple example that shows how to process data that a user enters into a form. Before we examine this program, we must first see how JSTL handles expressions. Expression handling in JSTL is accomplished by using the EL expression language, just as it is done in JSP 2.0. In the subsequent sessions, we will examine the EL expression language.

T HE EL E XPRESSION L ANGUAGE One major component of JSP 2.0 is the new expression language named EL. EL is used extensively in JSTL. However, it is important to remember that EL is a feature of JSP and not of JSTL. JSP scriptlet code used with JSP 2.0 can contain EL expressions. The following lines of code demonstrate using EL inside of JSP scriptlet code: Your total, including shipping is ${total+shipping} As you can see from the preceding code, the values "total" and "shipping" are added and displayed as the HTML is generated. These expressions can be used inside of JSTL tags as well. One important requirement of JSTL 1.0 was that JSTL could be used with JSP 1.2. Because JSP 1.2 does not support EL, it is necessary to provide a few additional JSTL tags that facilitate the use of EL. For example, if you wanted to use JSTL to display the above expression, you would use the following code. Your total, including shipping is One of the requirements of JSTL was that it not require JSP 2.0 to run. By providing a tag that is capable of displaying EL expressions, this requirement is met.

JSTL E XAMPLE We will now examine a simple example that uses JSTL. For this example, we will examine a common procedure that is done by many Web applications. We will see how to POST a form and process the results from that POST. A simple program that is capable of doing this is shown below: If with Body You guessed it!

JSTL E XAMPLE … CONTD You are wrong Guess what computer language I am thinking of?

This simple Web page will display a form and ask the user to guess what computer language the program is thinking of. Of course, the computer is thinking of "Java." This page begins by checking to see if a POST was done. This allows both the form, and the code that handles the form, to be placed on one single page. This is done with the following JSTL if statement. Here you can see that the tag uses an EL expression to evaluate whether the request method is POST. If data was posted to the page, the value that the user entered for their guess is stored in a parameter named "guess". This is because "guess" was specified as the name of the form input item. We must now check to see whether this parameter is equal to the word "Java". This is done with the following tag. You guessed it! As you can see, the body of the tag is executed if the statement evaluates to true. In this article, we began to examine the basics of how JSTL is installed and how it works. There is much more to JSTL than this small example. The core tags of JSTL also include tags for looping, iteration, and variable handling. By using these tags, you can iterate through collections, access user session data, and perform other core tasks that all Web applications perform. You can get more examples from the site.

Conclusion This tutorial showed you some of the differences between the JSTL and standard JSP scriptlet programming. As you can see, JSTL allows a more consistent programming environment by allowing both HTML and procedural code to be expressed as tags. JSTL and tag libraries represent a new method of programming Web pages. JSTL does not provide everything that a programmer would need to create a full-featured Web application. Further, some procedures that could be programmed in JSTL is often best not programmed in JSTL. One perfect example of this is the database JSTL tags. Except for very small Web applications, it is generally considered bad programming practice to embed actual database commands into a JSP page. The proper location for such program code is in Java beans and EJBs that will be used by your Web application. For such routines, you may consider creating your own tag libraries. This way, your JSP pages can use JSTL to perform basic programming procedures that are not unique to your business. You should implement your own tag libraries to implement components, which are unique to your business, which will be used by your Web application. JSTL allows you to create a very consistent programming JSP-based application. This is done not just through JSTL, but through a combination of JSTL, your own custom tag libraries, and an underlying database. Understanding each of these components allows you to deploy an effective Web application

PREFACE

JSTL T AGLIB D EFINITIONS JSTL 1.2 (J2EE 5/JSP 2.1) Download: JSTL 1.2 consists of two files (jstl-impl-1.2.jar and jstl-api-1.2.jar) and is available for download at the above link. It should be used for JSP 2.1 containers such as Tomcat 6. Older Versions Be aware that there are two jar files to the JSTL; generally named jstl.jar and standard.jar. You need both. Be sure to use the appropriate version of the JSTL for your servlet container. JSTL 1.1 should used for JSP 2.0 containers such as Tomcat 5 and Resin 3, while JSTL 1.0 should be used for containers supporting JSP 1.n. JSTL 1.1 (J2EE 1.4/JSP 2.0) Download:

JSTL T AGLIB D EFINITIONS JSTL 1.0 (J2EE 1.3/JSP 1.2) Download: Notes on Dynamic Attributes for JSTL Tags Under JSTL1.1 and JSP2.0 you can use either scriplet expressions such as or EL expressions like ${expr} for JSTL tag attributes that accept run-time expressions. Most attributes in JSTL tags are enabled for run-time expressions, the most prevalent exceptions being the var and scope attributes of tags that accept them. This is because under JSP 2.0 the responsibility for evaluating the expressions lies with the container. Under JSTL1.0 and JSP 1.2, the JSTL is responsible for EL expression evaulation and you need to choose whether you want to use ${expr} or notations. This is why two version of each tag set are provided; for example the c library for EL expressions, and the c-rt library for use with scriptlet expressions.