JSTL (JSP Standard Tag Library) Tutorial: Core & Custom Tags Examples

JSTL is a standard tag library of the JSP. Here we will see how using different JSTL tags will make JSP coding easier.

In this tutorial, you will learn-

What is JSTL?

JSTL stands for Java server pages standard tag library, and it is a collection of custom JSP tag libraries that provide common web development functionality.

Advantages of JSTL

  1. Standard Tag: It provides a rich layer of the portable functionality of JSP pages. It's easy for a developer to understand the code.
  2. Code Neat and Clean: As scriplets confuse developer, the usage of JSTL makes the code neat and clean.
  3. Automatic JavabeansInterospection Support: It has an advantage of JSTL over JSP scriptlets. JSTL Expression language handles JavaBean code very easily. We don't need to downcast the objects, which has been retrieved as scoped attributes. Using JSP scriptlets code will be complicated, and JSTL has simplified that purpose.
  4. Easier for humans to read: JSTL is based on XML, which is very similar to HTML. Hence, it is easy for the developers to understand.
  5. Easier for computers to understand: Tools such as Dreamweaver and front page are generating more and more HTML code. HTML tools do a great job of formatting HTML code. The HTML code is mixed with the scriplet code. As JSTL is expressed as XML compliant tags, it is easy for HTML generation to parse the JSTL code within the document.

JSTL Core

The core tags are most frequently used tags in JSP. They provide support for

To use core tags we need to define tag library first and below is the syntax to include a tag library.

Syntax :

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

Here,

Let see some of the core tags in detail,

1. Out:

Syntax:

<c:out value="" default="" escapeXML="">

Example:

Coretag_jsp1.jsp

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP1</title>
</head>
<body>

</body>
</html>

Explanation of the code:

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c'. Hence, it can be used as a prefix for all coretags.

Code Line 12: Here we are using coretag out with the prefix "c" and this out will print the value in the expression tag. Hence, output will be name

When you execute the above code, you get the following output:

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

2. Catch

Syntax:

<c:catchvar="">

Here var represents the name of the variable, which will hold throwable exception.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP2</title>
</head>
<body>
<c:catch var="guruException">
   <% int num = 10/0; %>
</c:catch>
The Exception is : ${guruException} 
</body>
</html>

Explanation of the code:

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c' hence it can be used as a prefix for all coretags

Code Line 11-13: Coretag catch is used to catch the exception and print the exception. Here the exception is raised when 10/0 and that exception has name "guruException".

Code Line 14: We are printing the "guruException".

When you execute the code, you will get the following output:

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

3. Import

Syntax:

<c:importvar="" uri="">

Here var is a variable name which is an identifier, which will hold the filename/uri.

uri is relative filename or uriname.

coretag_jsp31.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP 31</title>
</head>
<body>
<c:import var="displayfile" url="coretag_jsp32.jsp">
</c:import>
<c:out value="${displayfile}"/>
</body>
</html>

Coretag_jsp32.jsp

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a>The file is diplayed after importing</a>
</body>
</html>

Explanation of the code:

Coretag_jsp31.jsp

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c' hence it can be used as a prefix for all coretags

Code Line 11-12: Here we are importing coretag_jsp32.jsp file into this file using import tag

Code Line13: Here we are printing the file coretag_jsp32.jsp using out tag.

When you execute the above code, you get the following output.

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

4. forEach

Syntax:

<c:forEach var="" begin=""  end="">

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP4</title>
</head>
<body>
<c:forEach var="gurucount" begin="5" end="10">
 <c:out value="${gurucount}"/>
</c:forEach>
</body>
</html>

Explanation of the code:

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c' hence it can be used as a prefix for all coretags

Code Line 11-13: Here we use "forEach" loop where the variable name is "gurucount", which has begun count as 5 and end count as 10.We are printing the variable gurucount which has numbers starting from 5 to 10.

When you execute the code, you get the following output

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

5. If

Syntax:

<c:if test="${condition}></c:if>

Here if the condition is true then series of statements are executed.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP5</title>
</head>
<body>
<c:set var="count" value="100"/>
<c:if test="${count == 100}">
   <c:out value="The count is 100"/>
</c:if>
</body>
</html>

Explanation of the code:

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c' hence it can be used as a prefix for all coretags

Code Line 11: Here we are setting the variable named as count to 100

Code Line 12-14: Here we are using "if condition" where we are checking whether the count is equal to 100. It is equal to 100 then we get the output as "The count is 100."

When you execute the above code, you get the following output

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

6. redirect:

Syntax:

<c:redirect url="" context=""/>

Here url is relative url to which it has to be redirected and context name of the local web application.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP6</title>
</head>
<body>
<c:redirect url="/"/>
</body>
</html>

Explanation of the code:

Code Line 3: This taglib prefix is required for all tags and prefix added is 'c' hence it can be used as a prefix for all coretags

Code Line 11: Here we use "redirect tag", where we are specifying the urlname, and when we click on that page it redirects to site which has been given for redirect.

When you execute the above code, you get the following output;

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

JSP Custom Tags

Advantages of custom tags in JSP:

Syntax:

Consider we are creating testGuru tag and we can usetaghandlertestTag class, which will override doTag() method.

<ex:testGuru/>
Class testTag extends SimpleTagSupport{ public void doTag()}

Also, we will have to map this testTag class in TLD (Tag Library Descriptor) as JSP container will automatically create a mapping between the class file and uri which has been mentioned in the TLD file.

JSP Tag Interface

We are considering in the example below

Method of Tag Interface

Example:

Customtag_jsp1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Custom Tag</title>
</head>
<body>
<ex:guruTag/>
</body>
</html>

Custom.tld

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Test TLD</short-name>
  <tag>
    <name>guruTag</name>
    <tag-class>demotest.guruTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

guruTag.java(TagHandler)

package demotest;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class guruTag extends SimpleTagSupport{
	public void doTag() throws JspException,IOException
	{
		JspWriter out = getJspContext().getOut();
		out.println("Guru Tag");
	}

}

Explanation of the code:

guruTag.java(TagHandler)

Code Line 6:guruTag class is extending SimpleTagSupport class which is present in javax.servlet.JSP jar

Code Line 7: Here we are overriding doTag() method which throws JspException and IOException.

Code Line 9-10: In this method, the code will be embedded to custom tag which will be called. We are taking an object of JspWriter, and that will print "Guru Tag."

Custom.tld

Code Line 6: Here name of the custom tag is "guruTag."

Code Line 7:Tag class is taghandlerclass,i.e., guruTag.java. It takes full path of the handler file which includes the directory path of the location of the file.

Customtag_jsp1.jsp

Code Line 3:This taglib prefix is required for all tags and prefix added is 'ex' hence it can be used as a prefix for all coretags and uri is custom.tld which maps the tag handler.

Code Line 11: Here we are defining the custom tag "guruTag", which will call the handler class doTag() method and the code within it will be executed.

When you execute the above code, you get the following output

JSP Tag Library - JSTL Core & JSP Custom Tags

Output:

Summary:

 

YOU MIGHT LIKE: