jstl tags

                                                                Jstl Tags

used for displaying the content on client after escaping XML and HTML markup tags
This tag is useful for setting up a variable value in a specified scope. It basically evaluates an expression and sets the result in given variable
It is used for removing an attribute from a specified scope or from all scopes (page, request, session and application). By default removes from all.
<c: if>
it is used for testing conditions. There are two other optional attributes for this tag which are var and scope, test is mandatory
<c:choose>
It’s like switch statement in Java.
<c:when>
It’s like case statement in Java.
<c:otherwise>
It works like default attribute in switch-case statements.
<c:catch>
This tag is used in exception handling.
<c:import>
it is used for importing the content from another file/page to the current JSP page. Attributes – var, URL and scope
<c:forEach>
it is used for executing the same set of statements for a finite number of times
<c:forTokens>
It is used for iteration but it only works with delimiter
<c:param>
This JSTL tag is mostly used with <c:url> and <c:redirect> tags. It adds parameter and their values to the output of these tags.
<c:url>
It converts a relative url into a application context’s url
<c:redirect>
used for redirecting the current page to another URL

                                                      JSTL Functions

This function checks whether the given string is present in the input
It checks whether the provided string is present but it is not case sensitive.
It is used for finding out the start position of a string in the provided string. Function returns -1 when string is not found in the input.
It is used for HTML/XML character escaping which means it treats html/xml tags as a string. Similar to the escapeXml attribute of <c:out> tag.
it concatenates the strings with a given separator and returns the output string.
fn:split()
it splits a given string into an array of substrings
It is used for computing the length of a string or to find out the number of elements in a collection. It returns the length of the object.
It checks the specified string is a prefix of given string.
 It  is used for checking the suffix of a string. It checks whether the given string ends with a particular string.
used to get substring from given string
It is used for getting a substring which is present in the input string before a specified string.
It gets a substring from input which comes after a specified string.
It  removes spaces from beginning and end of a string and function.
it converts string to uppercase string
it converts string to lower case string
it  search for a string in the input and replace it with the provided string. It does case sensitive processing.


c:out

attributes
value     :used to assign value
escapeXml :used to escape html/xml tags if it is set to true(by default),if it is set                              to false it would  not escape HTML/XML tags and the tags will                          work.
default   :used to give default value and it will be excuted if the value is                          null.

c:out tag with value attribute
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>c:out Tag </title>
</head>
<body>
     <c:out value="${'<b>This is a <c:out> Tag </b>'}" />
</body>
</html>
output
<b>This is a <c:out> Tag </b>

c:out tag with value and escapeXml attributes
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>c:out Tag </title>
</head>
<body>
     <c:out value="${'<b>This is a <c:out> Tag </b>'}"   escapeXml="false"/>
</body>
</html>
output
This is a Tag

c:out tag with value and default attributes

index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>c:out Tag </title>
</head>
<body>
     <%!String s=null; %>
     <c:out value="${s}"    default="default message"/>
</body>
</html>
output
default message

 

c:set


attributes:
var   : used to give a variable name
scope : used to specify the scope like page, request, session, application.
value : used to give the value to the variable or object
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Example of c:set tag</title>
</head>
<body>
     <c:set var="name" scope="application" value="java for job" />
     <a href="process.jsp">Display</a>
</body>
</html>
process.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:out value="${name}" />
output
java for job

c:remove
attribute
var  :variable name to be removed
scope:scope from which variable needs to be removed
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>c:remove tag</title>
</head>
<body>
     <c:set var="domain" scope="session" value="www.javaforjob.com" />
     <c:set var="developer" scope="session" value="seshadri" />
     <c:remove var="developer" />
     <a href="process.jsp">process.jsp</a>
</body>
</html>
process.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${domain}"/><br>
<c:out value="${developer}" default="attribute is removed hence no value"/>
output
www.javaforjob.com
attribute is removed hence no value


c:if
attributes
test  :used to test the condition
var   : Variable name in which the test result would be stored.
scope : It defines the scope for storing the value. For e.g. if it is set to  session the         stored var value can  be accessed till the session is active.
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL c:if Tag Example</title>
</head>
<body>
     <c:set var="num" value="14" />
     <c:if test="${num%2==0}" var="result" scope="request">
           <c:out value="${num} is  a even number" />
     </c:if>
     <c:if test="${num%2!=0}">
           <c:out value="${num} is  a odd number" />
     </c:if>
     <a href="process.jsp">process.jsp</a>
</body>
</html>
output:
14 is a even number process.jsp
process.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${result}" default="result has no value"/><br>
output:
true