JSP JSTL Core Tags

As you know there are five collections of tags in JSTL, in this tutorial, we are going to discuss some of the core tags in the JSTL library. Now in JSTL Core Library, there are approximately 15 tags. Let’s quickly go through them one by one.

  • Catch: this tag is used to handle any exception thrown.
  • Choose: It’s like a switch statement, only it’s conditions are marked by <when> and <otherwise>.
  • When: a sub-tag of the choose tag which includes its body if its condition evaluates to true.
  • Otherwise: a sub-tag of the choose tag, which follows the <when> tag. It runs only when all the prior conditions amount to false.
  • If: It is a simple conditional tag.
  • Import: It is used for retrieving absolute or relative URLs, and exposing the content within to the page or to a variable.
  • forEach: Iteration tag, used to iterate over different collection types.
  • forTokens: Iteration tag, used to iterate over tokens which are separated by delimiters.
  • Out: it is used to display output, just like <%= … > but for expressions.
  • Param: adds a parameter to a containing ‘import’ tag’s URL.
  • Redirect: it redirects to a new URL.
  • Remove: remove a scoped variable (from a particular scope, if specified).
  • Set: sets the result of an expression evaluation in a scope.
  • URL: creates a URL with the option for query parameters.

Let’s take a look at some of the most commonly used tags.


Alright, so every page which makes use of these JSP core tags needs to refer to the JSTL core library. You can add the reference at the top of the page by adding this line:

<%@ tagliburi=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>

The URI is simply a unique identifier; you don’t need to have internet connectivity to refer to the core libs. The “prefix=c” specifies that you are simply going to use the core tags.

forEach tag

To display the usage of forEach tag, I am going to have a list of data and have the forEach tag loop through it to display the data.

Let’s code:


String[] cities = {"Islamabad", "Delhi", "Heathrow"};

pageContext.setAttribute("mycities", cities);



<c:foreachvar="tempCity" items="${myCities}">





So here we are using the scriptlet to set up the data. Using the String array, we have set up a list of cities. Then we are setting up this array as an attribute, so that whenever we need to use it in JSP, we can call this attribute.

Then in the body we are using the forEach tag, and we have set up a temporary variable “temCity” just like we commonly use “i in the for loop in C++”. So what the forEach tag is doing is that it is using the tempCity variable to loop through the data in myCities and then displaying it.

Further Help

For help with more of these core tags, you can refer to the official documentation available at the JSTL site.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search