JSTL

1.What

JSTL(JSP Standard Tag Library) is a collection of useful jsp tags which to handle jsp applications.

<c:xxx> tags to control the flow in the jsp page.

<fmt:xxx> tags for date/number formatting and i18n.

<sql:xxx> tags for sql operation

<x:xxx> for handling xml

${fn:xxx()} are some utility EL functions.

2.How

JSTL is part of the Java EE API and included in Java EE application servers like TomEE, Glassfish, but not in servelet containers such as Tomcat, Jetty.

  • Add dependency in mave :

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

  • Declare tags in jsp page :

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

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

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

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

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

  • Finally, the declared servlet version in web.xml is very important to get JSTL and EL to work properly.  We need to ensure that you're using a servlet container that supports the minimun required Servlet version for the JSTL version. Take tomcat 6 for example, the web.xml looks like :

<?xml version="1.0" encoding="UTF-8"?>

<web-app

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

</web-app>

This works for both jstl 1.1 and 1.2.

Written on August 23, 2016