• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

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

  • Iteration
  • Conditional logic
  • Catch exception
  • url forward
  • Redirect, etc.

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,

  • prefix can be used to define all the core tags and
  • uri is the library of taglib from which it is imported

Let see some of the core tags in detail,

1. Out:

  • Result of expression is displayed in the out tag
  • It can directly escape the XML tags. Hence, they are not evaluated as actual tags

Syntax:

<c:out value="" default="" escapeXML="">
  • Here value represents information to the output, and it is mandatory
  • Default is failure to output information, and it is not mandatory
  • escapeXML – It is true if it escapes XML characters.

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:

  • We are getting the value as a name from the core tag "out" which will print in the output stream.

2. Catch

  • It catches any throwable exception which occurs in the body and shows as output.
  • It is used for handling the errors and to catch them.

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:

  • We are getting Arithmetic Exception as /by zero, and it is printed in the output using variable "guruException"

3. Import

  • We can import another file contents into a JSP page like we did in JSP include action.
  • Here we can also include URL and contents will be displayed on that page.

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:

  • Coretag_jsp32 is printed in the output as this file was imported in coretag_jsp31.jsp.

4. forEach

  • It is used to iterate the number of elements in series of statements.
  • It is same as a Java forloop.

Syntax:

<c:forEach var="" begin=""  end="">
  • Here var represents variable name which will hold counter name
  • Begin represents counter begin value
  • End will represent its end value

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:

  • The output we are getting is starting from 5 to 10.

5. If

  • It is used for Testing conditions.
  • If the tag is used to test a condition whether it is true or not based on this, the block of code would be executed.

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:

  • As the "if" condition is true, we get the output as "The count is 100".

6. redirect:

  • It is used for redirecting the current page to another URL by providing the relative URL of this tag.
  • It supports context relative URLs

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:

  • We get the output guru99.com url which is redirected by coretag_jsp6.jsp

JSP Custom Tags

  • It is a user-defined JSP language element.
  • When JSP is translated into a servlet, custom tag is converted into a class which takes action on an object and is called as a tag handler.
  • Those actions when the servlet is executed are invoked by the web container.
  • To create the user-defined custom tag, we need to create the tag handler which will be extending the SimpleTagSupport and have to override doTag() method.
  • We need to create TLD where we need to map the class file in TLD.

Advantages of custom tags in JSP:

  • Portable - An action described in a tag library must be usable in any JSP container.
  • Simple - Unsophisticated users must be able to understand and use this mechanism.Vendors of JSP functionality must find it easy to make it available tousers as actions.
  • Expressive - The mechanism must support a wide range of actions, includingnested actions, scripting elements inside action bodies, creation, use andupdating of scripting variables.
  • Usable from different scripting languages - Although the JSP specificationcurrently only defines the semantics for scripts in the Java programming language, we want to leave open the possibility of other scripting languages.
  • Built upon existing concepts and machinery- We do not want to reinvent whatexists elsewhere. Also, we want to avoid future conflicts whenever we canpredict them

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

  • This class will have to extend SimpleTagSupport class.
  • This class will have to override doTag() method which is part of SimpleTagSupport class (overriding is a method which is inherited from parent class).
  • This interface is a sub interface of JSPTag interface.
  • It provides methods to perform at the start and end of the tag.
  • Also, we need to map this class in TLD i.e. Tag Library descriptor

We are considering in the example below

Method of Tag Interface

  • doTag() is a method which we need to override which will have the contents for the tag.
  • It takes the current JSP Context using getJSPContext()

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:

  • We get the output as "GuruTag" from guruTag.java i.e. TagHandler, which overrides doTag() method and which print "Guru Tag" as an output.

Summary:

  • In this section, we learnt about JSP standard tag library in which we did core tags and custom tags.
  • Core tags include for, if, redirect, import, catch tags which were tags used for basic purposes in JSP.
  • Also, we did custom tags wherein we can define the tags and use it in JSP

 

YOU MIGHT LIKE:
JSP

Debugging in JSP

Debugging is the process to trace the error in the application. It is not very easy to trace bugs...