JSTL Core Tags


JSTL core tags provide some basic scripting tasks.

Syntax for including JSTL core tags library in our jsp page is:

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


1. <c:out> tag: It is used to print the result of an expression and it automatically escapes tags.

Attribute Description
value It specifies expression to be evaluated.
default It specifies value if the expression is null.

Example: The following example will print a welcome message to user.

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:out value = “${‘Welcome param.name’}” default=”Welcome Abhishek” />
    </body>
    </html>


2. <c:set> tag: It is used to set the value in a variable in specified scope or update the property of the JavaBean object.

Attribute Description
var Name of variable.
value Value to save in the variable.
scope Scope of variable.
target Name of variable whose property is to be modified.
property Property to be modified.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”name” scope=”session” value=”Abhishek” />
        <c:out value = “${‘name’}” default=”Welcome user” />
        <c:set target=”name” property=”name” value=”Raj” />
        <c:out value = “${‘name’}” default=”Welcome user” />
    </body>
    </html>


3. <c:remove> tag: It is used to remove the specified variable from a particular scope.

Attribute Description
var Name of variable to be removed.
scope Scope of variable to be removed.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”name” scope=”session” value=”Abhishek” />
        Before removing : <c:out value = “${‘name’}” />
        <c:remove var=”name” />
        After removing : <c:out value = “${‘name’}” default=” user” />
    </body>
    </html>


4. <c:import> tag: It is used to dynamically add the content from the provided URL to our current jsp page.

Attribute Description
url URL from the data to be retrieved.
var Name of variable to store the imported data.
scope Scope of variable used to store data.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:import var=”u” url=”https://www.tutorialsduniya.com” />
        <c:out value = “${u}” />
    </body>
    </html>



5. <c:if> tag: It evaluates an expression and displays its body content only if the expression evaluates to true.

Attribute Description
var Name of variable to store the result.
test Condition to evaluate.
scope Scope of the variable.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”marks” scope=”session” value=”75” />
        <c:if test = “$(marks > 70)” >
            Marks are : <c:out value=”$(marks)” />
        </c:if>
    </body>
    </html>


6. <c:choose> tag: It is used to choose between a number of alternatives.

It doesn’t has any attribute and <c:when> and <c:otherwise> are used within with this tag.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”marks” scope=”session” value=”95” />
        <c:choose>
            <c:when test = “$( marks > 93 )” >
                Good marks
            </c:when>
            <c:when test = “$( marks > 75 )” >
                Average marks
            </c:when>
            <c:otherwise>
                Need t work hard
            </c:otherwise>
        </c:choose>
    </body>
    </html>


7. <c:when> tag: It is a mutually exclusive tag which evaluates the body of the first when tag whose test condition evaluates to true and then the control exits to choose block. It is used within <c:choose> tag.

Attribute Description
test Condition to be evaluated.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”marks” scope=”session” value=”95” />
        <c:choose>
            <c:when test = “$( marks > 93 )” >
                Good marks
            </c:when>
            <c:when test = “$( marks > 75 )” >
                Average marks
            </c:when>
            <c:otherwise>
                Need t work hard
            </c:otherwise>
        </c:choose>
    </body>
    </html>


8. <c:otherwise> tag: It follows <c:when> tag and evaluates only if all the above <c:when> conditions are false.

This tag doesn’t has any attribute and used in <c:choose> along with <c:when> tag.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:set var=”marks” scope=”session” value=”95” />
        <c:choose>
            <c:when test = “$( marks > 93 )” >
                Good marks
            </c:when>
            <c:when test = “$( marks > 75 )” >
                Average marks
            </c:when>
            <c:otherwise>
                Need t work hard
            </c:otherwise>
        </c:choose>
    </body>
    </html>



9. <c:forEach> tag: It is used to iterate over a collection of objects.

Attributes Description
items Items to loop over.
var Name of variable.
begin Element to start with.
end Element to end with.
step Process every step item.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:forEach items = “Abhi, Raj, SRK” var=”name”>
            <c:out value = “${name}” />
        </c:forEach>
    </body>
    </html>


10. <c:forTokens> tag: It has similar attributes as <c:forEach> tag with 1 additional attribute delims which specifies the characters to use as delimiter.

Attributes Description
items Items to loop over.
var Name of variable.
begin Element to start with.
end Element to end with.
step Process every step item.
delims Characters to use as delimiters.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:forTokens items = “Abhi, Raj, SRK” delims=”,” var=”name”>
            <c:out value = “${name}” />
        </c:forTokens>
    </body>
    </html>


11. <c:catch> tag: It is used in error handling to catch any throwable exception that occurs in the body.

Attribute Description
var Name of variable to hold java.lang.Throwable if thrown by any element in the body.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:catch var=”catchException”>
            <% int c = 7/0; %>
        </c:catch>
        <c:if test = “${catchException != null}” >
            <p> The type of exception is : ${catchException} <br/>
            There is an exception : ${catchException.message} </p>
        </c:if>
    </body>
    </html>


12. <c:url> tag: It creates a URL with optional query parameter and used for URL encoding or URL formatting.

Attribute Description
value Base URL.
var Name of variable to expose the processed URL.
scope Scope of variable to expose the processed URL.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:url value=”/Register.jsp” />
    </body>
    </html>



13. <c:param> tag: It is used to add the parameter in the ‘import’ tag’s URL. It allows the proper URL request parameter to be specified within URL and automatically performs any necessary encoding.

Attribute Description
name Name of request parameter to be set in the URL.
value Value of request parameter to be set in the URL.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:url var=”newURL” value=”/user.jsp” />
            <c:param name=”user” value=”Abhishek” />
            <c:param name=”id” value=”15572” />
        </c:url>
    </body>
    </html>


14. <c:redirect> tag: It is used to redirect the browser to an alternate URL by using automatically URL rewriting.

Attribute Description
url URL to redirect the user’s browser to.
context / followed by name of local web application.

Example:

    <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=”c” %>
    <html>
    <body>
        <c:redirect url=”https://jsp.tutorialsduniya.com” />
    </body>
    </html>