In this tutorial, you will learn-

What is JSTL? JSTL Core Tags JSTL Custom Tags

Advantages of JSTL

Below are the advantages of JSTL:

Standard Tag: It provides a rich layer of the portable functionality of JSP pages. It’s easy for a developer to understand the code. Code Neat and Clean: As scriplets confuse developer, the usage of JSTL makes the code neat and clean. 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. 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. 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 Tags

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 : 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:

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 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:

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: Here var represents the name of the variable, which will hold throwable exception. Example: 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:

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: 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 Coretag_jsp32.jsp 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.

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:

Here var represents variable name which will hold counter name Begin represents counter begin value End will represent its end value

Example: 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

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: Here if the condition is true then series of statements are executed. Example: 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

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: Here url is relative url to which it has to be redirected and context name of the local web application. Example: 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;

Output:

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

JSTL 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:

Here are the 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. 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 Custom.tld guruTag.java(TagHandler) 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

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