JSTL Tutorial, JSTL Tags Example

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc. We will look into JSTL Tags in detail in this JSTL tutorial.

Earlier we saw how we can use JSP EL and JSP Action Tags to write JSP code like HTML but their functionality is very limited. For example, we can’t loop through a collection using EL or action elements and we can’t escape HTML tags to show them like text in client side. This is where JSTL tags comes handy because we can do much more from JSTL tags.

JSTL

JSTL is part of the Java EE API and included in most servlet containers. But to use JSTL in our JSP pages, we need to download the JSTL jars for your servlet container. Most of the times, you can find them in the example projects of server download and you can use them. You need to include these libraries in your web application project WEB-INF/lib directory.

JSTL jars

JSTL jars are container specific, for example in Tomcat, we need to include jstl.jar and standard.jar jar files in project build path. If they are not present in the container lib directory, you should include them into your application. If you have maven project, below dependencies should be added in pom.xml file or else you will get following error in JSP pages – eclipse Can not find the tag library descriptor for "https://java.sun.com/jsp/jstl/ core"

JSTL Tags

Based on the JSTL functions, they are categorized into five types.

  1. JSTL Core Tags: JSTL Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. To use JSTL core tags, we should include it in the JSP page like below.

    In this article, we will look into important JSTL core tags.
  2. JSTL Formatting and Localisation Tags: JSTL Formatting tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles. We can include these jstl tags in JSP with below syntax:
  3. JSTL SQL Tags: JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries, we include these JSTL tags in JSP with below syntax:
  4. JSTL XML Tags: JSTL XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation. Syntax to include JSTL XML tags in JSP page is:
  5. JSTL Functions Tags: JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc. Syntax to include JSTL functions in JSP page is:

Note that all the JSTL standard tags URI starts with https://java.sun.com/jsp/jstl/ and we can use any prefix we want but it’s best practice to use the prefix defined above because everybody uses them, so it will not create any confusion.

JSTL Core Tags

JSTL Core Tags are listed in the below table.

JSTL Core Tag Description
<c:out> To write something in JSP page, we can use EL also with this tag
<c:import> Same as <jsp:include> or include directive
<c:redirect> redirect request to another resource
<c:set> To set the variable value in given scope.
<c:remove> To remove the variable from given scope
<c:catch> To catch the exception and wrap it into an object.
<c:if> Simple conditional logic, used with EL and we can use it to process the exception from <c:catch>
<c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <c:when> and <c:otherwise>
<c:when> Subtag of <c:choose> that includes its body if its condition evalutes to ‘true’.
<c:otherwise> Subtag of <c:choose> that includes its body if its condition evalutes to ‘false’.
<c:forEach> for iteration over a collection
<c:forTokens> for iteration over tokens separated by a delimiter.
<c:param> used with <c:import> to pass parameters
<c:url> to create a URL with optional query string parameters

JSTL Tutorial

Let’s see some of the JSTL core tags usages in a simple web application. Our project will include a Java Bean and we will create a list of objects and set some attributes that will be used in the JSP. JSP page will show how to iterate over a collection, using conditional logic with EL and some other common usage.

JSTL Tutorial – Java Bean Class

JSTL Tutorial – Servlet Class

JSTL Tutorial – JSP Page

home.jsp code:

Now when we run application with URL https://localhost:8080/JSTLExample/HomeServlet, we get response as in below image.

JSTL-Tags-Example

In above JSTL example, we are using c:catch to catch the exception within the JSP service method, it’s different from the JSP Exception Handling with error pages configurations.

That’s all for a quick roundup of JSTL tags and example of JSTL core tags usage.

Reference: JSTL Wikipedia Page

By admin

Leave a Reply