JSP/Servlet | JSTL(JSP Standard Tag Library)
JSTL stands for Jsp Standard Tag Library. It is a set of tags that standardizes syntax used in JSP pages.
Why Is JSTL Needed?
When JSP(Java) code and HTML code are mixed together in a JSP page, the code becomes complex and difficult to maintain.
Compare the following code written only with JSP and the code written with JSTL.
Written Only with JSP
<%
if (list.size() > 0) {
for(int i = 0; i < list.size(); i++) {
HashMap map = (HashMap)list.get(i);
%>
제목 : <%=map.get("title")%>
<%
}
}
%>
Written with JSTL
<c:if test="${!empty list}">
<c:forEach var="item" items="${list}">
제목 : ${item.title}
</c:forEach>
</c:if>
The JSTL version is clearly easier to read and understand.
JSTL Types
Core
Supports variables, flow control, and URL handling.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
fmt
Displays locale, message formats, numbers, and date formats.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
functions
Handles strings and collections.
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
How to Apply JSTL
Add the Library
You only need to add the jstl-1.2.jar file. If you use Maven, add the following dependency to pom.xml.
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Declare the Tag Library
To use JSTL, you need a declaration inside the JSP file indicating that JSTL will be used.
The following directive declares the JSTL core, also known as the C tag.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Applied to a JSP file, it looks like this.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>JSTL</title>
</head>
<body>
Hello World!! JSTL
</body>
</html>
However, if you declare it this way, you must add the directive to every JSP file, which is inconvenient.
To include it in every JSP, set <include-prelude> in web.xml as follows.
<?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_3_0.xsd"
version="3.0">
... omitted ...
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude>/WEB-INF/jsp/common/directive.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
... omitted ...
</web-app>
The configured JSP file (directive.jsp) is always included in all JSP (*.jsp) files. Writing the JSTL directives there makes the setup easier.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>