Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JSP Standard Tag Library (JSTL)

This article briefs you about Tag Libraries used in JSP, often called as JavaServer Pages Standard Tag Library (JSTL).

In this article I shall brief you about Tag Libraries used in JSP, often called as JavaServer Pages Standard Tag Library (JSTL). The following topics are covered in the following sections:

  • Introduction
  • Types of JSTL
  • Advantages of JSTL
  • Disadvantages of JSTL
  • Setting Up JSTL Library
  • Core Tag Library
  • The Format Tag Library
  • The SQL Tags
  • The XML Tags
  • The Function Tags
  • Examples of JSTL Tag Libraries

Introduction

The JSP Standard Tag Library (JSTL) is a collection of JSP custom tags libraries, which provides core functionality used for JSP documents. JSTL allows developers to use predefined tags instead of writing the Java code. The goal of JSTL is to minimize, eliminate actual java code that was written through JSP. The JSTL tag libraries released by SUN to perform operations on JSP programming.

Types of JSTL

The JSTL provides four types of tag libraries that can be used with JSP pages :

  • The Core JSTL
  • The XML Tag Library
  • The Format Tag Library
  • The SQL Tag Library
  • The Functions Tag Library

JSTL Core Tags

Core Tag provides support for iteration, conditional processing, and expression language for the tags in JSP pages.

Syntax of Core Tag :

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

JSTL XML Tags

It is used to work with XML data such as parsing, selecting and transforming XML data in JSP page.

Syntax of XML Tag :

<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

JSTL Formatting Tags

It is used for formatting the data used in JSP page. The data can be dates, numbers and time specifications for internationalized web sites.

Syntax of Format Tag Library :

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

JSTL SQL Tags

It provides SQL support. It is used to access relational data bases such as Oracle, MySql etc used in a JSP page.

Syntax of SQL Tag Library :

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

JSTL Function Tags

JSTL function tag library contains set of string manipulation functions that can be used with the JSP Expression Language.

Syntax of Function Tag Library :

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Advantages of JSTL

  • Easy to read and maintain.
  • Reusability of the code.
  • JSTL reduces use of scriptlets in a JSP page.
  • JSTL allows using predefined tags instead of writing the java code.
  • We can easily access and manipulate the application data without using the scriptlets.
  • JSTL is easy to learn and understand without knowledge of Java programming.
  • It easier to read because JSTL based on XML which is similar to HTML.

Disadvantages of JSTL

  • Large complex JSP pages make use of JSTL tag libraries which exceeds class size limitation. Because size of java class is limited.
  • JSTL is not so flexible and not as extensive as JSP Scriptlet. The JSTL is not nearly as evolved as Java itself.
  • It is easier to programmer who knows HTML skill set. If JSTL get translated into Java code, experienced programmer may wonder why he does not do that himself and save the time. So it may difficult to carry out for experienced programmers.

Setting Up JSTL Library

JSTL is part of Java API and included in most servlet or web containers. I have usedTomcat 7.0.50server which can support servlet 2.3 and JSP 1.2.

To use JSTL we need to include following JSTL jars for servlet container.

  • jstl.jar
  • standard.jar

Download these two jars from the link:https://jstl.java.net/download.html

These jars sholuld be included into WEB-INF/lib directory of the web application server. Each web application must contain these files. The latest version of JSTL is 1.2

Core Tag Library

The Core tag library is the collection of the JSTL core tags in the JSP pages. These tags are used for manipulating with data in JSP pages. Core Tag provides support for iteration, conditional processing, and expression language for the tags in JSP pages. The core tag library can be used in JSP page by using following tag library :

<% @taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %>

The JSTL Core Tags are as follows :

Tag

Description


It is used to evaluate the result of an expression, like <%=...>expression


It is used to set the value of variable into the given scope.


It is used to remove the variable from the specified scope.


This tag is used to handle the exceptions that can be raised by any of the tag which located inside the body of thetag.


This tag evaluates an expression and displays its body content only if the expression is true.


It is like a java switch statement. It executes the conditional block statements which is embedded with the sub-tagsand.


This tag must appear inside the sub tagwhich executes body content when the condition is true.


It is same astag but is unconditional. It is evaluated if none of thetag conditions in the sub tagtag are evaluated to true.


This tag is used to include the content of another resource in the current JSP.


It is used to iterate over a collection of data such as arrays.


This tag is used to break a string into tokens and iterate through each of the tokens.


It is used to add the parameter and their values to the output ofandtags.


It is used to redirect control to other pages within the web application.


Thetag formats a URL into a string and stores it into a variable.

The Format Tag Library

This tag library is used to formatting the data used in JSP page. The data can be dates, numbers and time specifications for internationalized web sites. It also provides the support for internationalization. . The format tag library can be used in JSP page by using following tag library :

<%@ taglib uri=http://java.sun.com/jsp/jstl/fmt prefix="fmt" %>

The JSTL Format Tags are as follows

Tag

Description


It is used to format numbers, currencies according to specific precision or formatting pattern.



It is used to parse the string representation of numbers, currencies according to specific precision or formatting pattern.


It allows us to format dates and times according to specific precision or formatting pattern.


It is used to parse the string representation of dates and times according to specific precision or formatting pattern.


It creates ResourceBundle object by using locale object and applies to formatting actions in the body content.


This tag stores the given locale in the locale configuration variable of the given scope.


It creates ResourceBundle object by using locale object and stores ResourceBundle object into the given variable and scope.


It specifies the tags that will be controlled by time zone and also specifies time zone for formatting or parsing the actions in the body content.


It is used to store the given time zone in the time zone configuration variable of the given scope.


It is used to display the message and maps key to localized message using resource bundle specified in bundle attribute.


This tag allows to set the character encoding of a request. It invokes setCharacterEncoding () method to set the character encoding of a request

The SQL Tags

It provides SQL support. It is used to access relational data bases such as Oracle, MySql etc used in a JSP page. The SQL tag library can be used in JSP page by using following tag library :

<%@ taglib uri=http://java.sun.com/jsp/jstl/sql prefix="sql" %>

The JSTL SQL Tags are as follows

Tag

Description


It is used for creating data source that is to be accessed by executing a query.


It is used to execute a query specified in a jsp page.


It is used to update SQL statements defined in its body or by the sql attribute.


It sets parameter to the sql statement to the given value.


It is used to add parameter to sql statement to the given java.util.Date value.


It provides connection to all data base operations and executes all statements in one transaction.

The XML Tags

It is used to work with XML data used in the JSP page. The XML tag library helps in parsing, selecting and transforming XML data in JSP page. These operations can be done by using the XPath expressions with XML document. The XML tag library can be used with JSP page by using following tag library :

<%@ taglib uri=http://java.sun.com/jsp/jstl/xml prefix="x" %>

The JSTL XML Tags are as follows

Tag

Description


It evaluates XML XPath expression and writes result to current JspWriter, like <%=...> for XPath expressions.


It parses XML document and stores result of XML document in the specified variable.


It evaluates XPath expression and stores result in scoped variable.


This tag evaluates a XPath expression and if the result is true then the body of the tag is evaluated; otherwise not.


It is used for looping list of elements obtained after evaluating XPath expression.


It is similar to java switch statement which is used for checking conditional statements which encloses one or moreandtags.


This tag must appear inside the sub tagwhich executes body content when the condition is true.


It is same astag but is unconditional. It is evaluated if none of thetag conditions in the sub tagtag are evaluated to true.


it is used to transform XML document using XSL style sheet.


It is used to set transformation parameters. This tag should be used within the body oftag to set the parameter in the XSLT style sheet.

The Function Tags

JSTL function tag library contains set of string manipulation functions that can be used with the JSP Expression Language. The XML tag library can be used with JSP page by using following tag library :

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions "prefix="fn" %>

The JSTL Function Tags are as follows

Function

Description

fn:contains()

It is used to check whether string contains specified sub string or not.

fn:containsIgnoreCase()

It returns true if string contains specified substring ,ignores case and also returns true or false based on evaluation condition.

fn:endsWith()

It returns true if string ends with specified suffix.

fn:escapeXml()

It is used to escape XML markup characters. It takes only one parameter of type string.

fn:indexOf()

It returns index for the first occurrence of the specified sub string.

fn:join()

It is used to join all the elements contained in an array with the specified string.

fn:length()

It returns number of elements from an array or collection, characters from a string.

fn:replace()

It is used to replace a string with another string.

fn:split()

It returns array of string by separating the string with separator.

fn:startsWith()

It returns true if string starts with prefix.

fn:endsWith()

It returns true if string ends with prefix.

fn:substring()

It returns substring of string with begin index till the end index.

fn:substringAfter()

It returns subset of string after the substring.

fn:substring()

It returns subset of string before the specific substring.

fn:toLowerCase()

It returns all the characters of string to lower case.

fn:toUpperCase()

It returns all the characters of string to upper case.

fn:trim()

It is used to remove white spaces from the start and end of the string.

Examples of JSTL Tag Libraries

Example 1- Use of Core, XML and Format tags

The following example shows use of Core, XML and Format tags.

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
   <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
   </head>
   <body>
    
   <!-- Using XML Tags -->
   <h3>Fruits Information:</h3>
   <c:set var="fruit">
     
   <fruits>
       <fruit>
         <name>Grapes</name>
         <price>40/kg</price>
       </fruit>
      
    <fruit>
         <name>Orange</name>
         <price>30/kg</price>
       </fruit>
      
    <fruit>
         <name>Apple</name>
         <price>90/kg</price>
       </fruit>
     
   </fruits>
    
   </c:set>
    
   <x:parse xml="${fruit}" var="output"/>
   <p>The name of the Fruit :</p> 
    
   <x:out select="$output/fruits/fruit[1]/name" /><br>
    
   <p>The price of the Apple :</p>
    
   <x:out select="$output/fruits/fruit[3]/price" />
    
    
   <!--  Using Formatting Tag -->
   <h3>Parse Date</h3>
    
               <c:set var="today"  value="21-02-2014"/>
    
               <fmt:parseDate value="${today}" pattern="dd-MM-yyyy" var="parseDate" />
    
               <c:out value="${parseDate}"></c:out>
               
   </body>
   </html>
   

Steps for Execution

  • Save the file as example.jsp in eclipse IDE.
  • Now select the jsp file, right click on mouse and select Run as -> Run on Server.

Output

After successful execution of the program we will get the following result:

As shown in above example we are usingandtags of Core Tag library,andtags of XML Tag library andtag of Function tag library.

  • Thetag sets result of evaluated expression in the scoped variable.
  • Thetag is used to evaluate the result of an expression.
  • Thetag parses XML document and stores result of XML document in the specified variable and
  • tag evaluates XML XPath expression and writes result to current JspWriter.
  • Thetag is used to parse the string representation of dates and times according to specific precision or formatting pattern.

Example 2- Use of SQL and Function tags

The following example shows use of SQL and Function tags.

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
               pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
   <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
   <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
    
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>sql_function_example</title>
   </head>
   <body>
    
       <!--  Using SQL tags -->
               <!-- Connect to the database -->
               <sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver"
                           url="jdbc:mysql://localhost/test" user="root" password="" />
                           <h3>Using SQL Tags:</h3>
               <p>The data is below:</p>
               <!--  Execute the SQL -->
               <sql:query dataSource="${ds}"
                           sql="select * from employees where id >=10" var="result" />
               <c:out value="The rows returned by SQL : ${result.getRowCount()}" /><br>
               <!-- Using Function Tag -->
               <h3>Using Function Tag</h3>
               <c:set var="str" value="HELLO WORLD!!!"></c:set>
               <p>String in Lower Case:</p>
               <c:out value="${fn:toLowerCase(str)}"></c:out>
    
   </body>
   </html>
   

Steps for Execution

  • Here we have use MySQL database and created a tableemployees.
  • Save the file as sql_function_example.jsp in eclipse IDE.
  • Now select the jsp file, right click on mouse and select Run as -> Run on Server.

Output

After successful execution of the program we will get the following result :

As shown in the above example, we have usedtag andtag of SQL Tag library andtag of Function tag library.

  • Thetag is used for creating data source that is to be accessed by executing a query.
  • Thetag is used to execute a query specified in a jsp page .
  • Thetag returns all the characters of string to lower case.

Conclusion

In this article we saw some basics of JSTL with examples. Hope you liked the article.



I''m a full stack developer with around 10+ yrs of experience. I enjoy writing technical articles on upcoming technical trends.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login