JSTL (JSP Standard Tag Library): by R4R Team

 JSTL (JSP Standard Tag Library):

The JSP Standard Tag Library (JSTL) represents a set of tags to simplify the JSP development.

Advantage of JSTL:

1)Fast Developement JSTL provides many tags that simplifies the JSP.
2)Code Reusability We can use the JSTL tags in various pages.
3)No need to use scriptlet tag It avoids the use of scriptlet tag.

There JSTL mainly provides 5 types of tags:

Tag Name  Description
core tags The JSTL core tag provide variable support, URL management, flow control etc. The url for the core tag is http://java.sun.com/jsp/jstl/core . The prefix of core tag is c.
sql tags The JSTL sql tags provide SQL support. The url for the sql tags is http://java.sun.com/jsp/jstl/sql and prefix is sql.
xml tags The xml sql tags provide flow control, transformation etc. The url for the xml tags is http://java.sun.com/jsp/jstl/xml and prefix is x.
 
internationalization tags The internationalization tags provide support for message formatting, number and date formatting etc. The url for the internationalization tags is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.
 
functions tags The functions tags provide support for string manipulation and string length. The url for the functions tags is http://java.sun.com/jsp/jstl/functions and prefix is fn.

For creating JSTL application, you need to load jstl.jar file.

JSTL Core Tags:

The JSTL core tags mainly provides 4 types of tags:

miscellaneous tags: catch and out.
url management tags: import, redirect and url.
variable support tags: remove and set.
flow control tags: forEach, forTokens, if and choose.

Syntax for defining core tags

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

c:catch:
It is an alternative apporach of global exception handling of JSP. It handles the exception and doesn't propagate the exception to error page. The exception object thrown at runtime is stored in a variable named var.

Example of c:catch:

Let's see the simple example of c:catch.

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

<c:catch>

int a=10/0;

</c:catch>

c:out:

It is just like JSP expression tag but it is used for exression. It renders data to the page.

Example of c:out


Let's see the simple example of c:out.

index.jsp

<form action="process.jsp" method="post">

FirstName:<input type="text" name="fname"/><br/>

LastName:<input type="text" name="lname"/><br/>

<input type="submit" value="submit"/>

</form>

process.jsp

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

First Name:<c:out value="${param.fname}"></c:out><br/>

Last Name:<c:out value="${param.lname}"></c:out>


c:import:

It is just like jsp include but it can include the content of any resource either within server or outside the server.

Example of c:import

Let's see the simple example of c:import to display the content of other site.

index.jsp

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

<h1>ABC.com</h1>

<hr/>

<c:import url="http://www.ycd.com"></c:import>


Example of c:import to display the source code

Let's see the simple example of c:import to display the source code of other site.

index.jsp

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

<h1>ABC.com</h1>

<hr/>

<c:import var="data" url="http://www.ycd.com"></c:import>

<h2>Data is:</h2>

<c:out value="${data}"></c:out>

c:forEach:

It repeats the nested body content for fixed number of times or over collection.

Example of c:forEach

Let's see the simple example of c:forEach.

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

<c:forEach var="number" begin="5" end="10">

<c:out value="${number}"></c:out>

</c:forEach>


c:if

It tests the condition.

Example of c:if

Let's see the simple example of c:if.

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

<c:set var="number" value="${200}">

<c:if test="${number<500}">

<c:out value="number is less than 500"></c:out>

</c:if>

c:redirect

It redirects the request to the given url.
 

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

<c:redirect url="http://www.ycd.com"></c:redirect>                                        

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!