Friday 1 April 2016

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 ishttp://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 ishttp://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/fmtand 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


  1. <%@ 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.

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <c:catch>  
  3. int a=10/0;  
  4. </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

  1. <form action="process.jsp" method="post">  
  2. FirstName:<input type="text" name="fname"/><br/>  
  3. LastName:<input type="text" name="lname"/><br/>  
  4. <input type="submit" value="submit"/>  
  5. </form>  
process.jsp

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. First Name:<c:out value="${param.fname}"></c:out><br/>  
  3. 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.

No comments:

Post a Comment