JSTL stand for JavaServer Pages Standard Tag Library. JSTL provides a framework for integrating custom tags with JSTL tags. The goal of JSTL is to minimize or, if possible, eliminate actual Java code introduced through JSP. JSTL is a collection of very useful core tags and functions. These tags and functions will help you write JSP code efficiently. These tags allow developers to use predefined tags instead of writing the java code. This also provides core iteration and control flow features, text inclusion and SQL tags.
Types of JSTL Tags
JSTL provides four types of tag libraries. They are as follows:
- JSTL core tags: These tags are used for manipulating with data in JSP pages. This is used for iterating on data collection, setting parameter etc.
- The XML tag library: This tag is used for manipulating XML documents. These tags are used for parsing, selecting and transforming XML data in JSP page.
- The Format tag library: These tags are used for formatting the data used in a JSP page.
- The SQL tag library: This library is used to access the relational database used in a JSP page.
Advantages of JSTL
- JSTL represents the new method of programming web pages.
- JSTL is used to separate the business logic from the design logic.
- It is used to encapsulate the data
- JSTL is a great tool for performing tasks such as iterating over relatively simple data structures and similar tasks.
Disadvantages of JSTL
- JSP files that make use of JSTL will generate a great deal of overhead code.
- JSTL is not flexible JSTL is a new programming language that’s the reason it is not popular as like java
- May seem burdensome for experienced programmers
Environment to set up for JSTL
Following steps are useful for setup the JSTL Library:
You would need the following jars to use JSTL:
- JSTL1.2.jar: Download this from http://search.maven.org/#browse. Go to jstl/ directory on the this link and download the latest jstl.jar
- standard.jar:Download this from http://search.maven.org/#browse. Go to taglibs/ directory on the this link and download the latest standard.jar/
Copy these two jar files to WEB-INF/lib directory of your web server, in our case it is Tomcat 7.0.50. To start using JSTL in your JSP, it is necessary to declare taglib using proper URI path. Following tags are used in JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
JSTL Version
The latest version of JSTL is JSTL 1.2. We are using the same version of JSTL for demonstrate the use of JSTL in all our examples.
Simple Example of JSTL
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <span style="font-size: 12px; line-height: 18px;">"http://www.w3.org/TR/html4/loose.dtd"></span> <span style="font-size: 12px; line-height: 18px;"><%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %></span> <span style="font-size: 12px; line-height: 18px;"> Simple Example Of JSTL<br><br></span> <span style="font-size: 12px; line-height: 18px;"><c:out value="Wecome To JSTL"/></span>
In the above example first taglib directive points to the JSTL core tag library with prefix “c”. <c:out> tag is used to print or display the result of an expression.
Steps for Execution
- Click on the jsp File.
- Right click on mouse and select run as ->click run on server.
- The simple way for execute the program on eclipse is to use Ctrl+F11 key then click on select your browser option and click on finish.
Output
When the execution process is completed successfully we will get the output as below:
Next Tutorial : JSTL Core Tags