☰ See All Chapters |
JSP Standard Tag Library (JSTL)
JSP Standard Tag Library (JSTL) is a collection of tags, these tags are like readymade code, and by using these tags we can avoid JSP code to perform tasks. We have to import the tag library to use the tags. The JSTL tags are classified according to their functionality. The URIs for the libraries is as follows:
Tag Lirbrary | URIs |
JSTL core tags | <%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> |
JSTL formatting tags | <%@taglib uri="https://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> |
JSTL SQL tags | <%@taglib uri="https://java.sun.com/jsp/jstl/sql" prefix="sql"%> |
JSTL XML/XSL tags | <%@taglib uri="https://java.sun.com/jsp/jstl/xml" prefix="x"%> |
JSTL function tags | <%@taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn"%> |
How to use the JSTL core tags in JSP ?
Step 1:- Use the <taglib> directive in the JSP.
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="something"%>
Step 2:- Add necessary jars (libraries) jstl.jar and standard.jar to WEB-INF\lib folder of your web application. These jars can be collected from tomcat server. The location of these two jars is present in apache-tomcat\webapps\examples\WEB-INF\lib folder.
With tomcat 7.0 you can find, jstl.jar and standard.jar.
With tomcat 8.0 you can find, taglibs-standard-impl-1.2.5.jar and taglibs-standard-spec-1.2.5.jar
JSTL core tags
Tag | Description |
<c:set> | Set tag creates the variable with and some name and initial value. This variable will be stored in the given scope. Default value of scope is “page”. |
<c:catch> | catch tag is used to handle the exceptions in the jsp without writing try catch blocks. |
<c:choose> <c:when> <c:otherwise> |
Used for writing logic similar to if, else-if, else-if, else. |
<c:forEach> | Used to iterate collections and arrays. |
<c:forTokens> | Used for tokenizing. Iterates after tokenizing by the supplied delimiters. |
<c:if> | if tag is used to perform simple conditional checks. It is conditional tag used for testing the condition and execute the body content only if the expression evaluates to true. |
<c:import> | import tag is used to include html or jsp page within other jsp. |
<c:out> | out tag is used to print the expression. |
<c:param> | It is used to pass the data to the destination jsp while forwarding request from one jsp to another jsp. |
<c:redirect> | redirect tag is used to redirect the request from one jsp to another jsp. |
<c:remove> | remove tag is used to remove the specified variable from specified scope. |
<c:url> | url tag is used for url rewriting while doing session management. |
<c:set> , <c:remove> and <c:out>
Project directory structure
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:set var="name" value="Manu Manjunatha" scope="session" /> <c:out value="${name}" /> <c:remove var="name" scope="session" /> </body> </html> |
Output
<c:if>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:if test="${5>2}"> <c:out value="5 is greater than 2" /> </c:if> </body> </html> |
Output
<c:choose>,<c:when> and <c:otherwise>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:set var="x" scope="session" value="${100}"/> <c:set var="y" scope="session" value="${50}"/> <c:choose> <c:when test="${x<y}"> x is lesser than y </c:when> <c:when test="${x>y}"> x is greater than y </c:when> <c:otherwise> x is equal to y </c:otherwise> </c:choose> </body> </html> |
Output
<c:forEach>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:forEach var="i" begin="0" end="4"> <c:out value="${i+1}"/><p> </c:forEach> </body> </html> |
Output
<c:forTokens>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:forTokens items="1,2,3,4,5" delims="," var="num"> <c:out value="${num}"/><p> </c:forTokens> </body> </html> |
Output
<c:import>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:import var="data" url="import.jsp"/> <c:out value="${data}"/> </body> </html> |
import.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> This is from Imported JSP </body> </html> public class Test { public static void main(String[] args) { } } |
Output
<c:redirect>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:redirect url="https://java4coding.com"/> </body> </html> |
Output
<c:url>, <c:param>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:url value="/home.jsp" var="completeURL" > <c:param name="sessionId" value="123" /> </c:url> ${completeURL} </body> </html> |
Output
<c:catch>
JSTLCoreTagsDemo.jsp
<%@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <body bgcolor='wheat'> <c:catch var ="catchtheException"> <% int x = 10 / 0; %> </c:catch> ${catchtheException} </body> </html> |
Output
All Chapters