Wednesday, February 17, 2016


jstl(jsp standard tag library)

Not lower than JSP1.2 and Servlet 2.3 can use jstl
JSP2.0 can use jstl1.2
  1. normal usage tag
  2. conditional tag
  3. iterator tag
  4. url related tag(xml, sql)
Config jstl library
We need two librarys: jstl and taglibs-standard


<%@ taglib prefix="c" uri="" %>

Four levels, 

  1. pageContext.setAttribute("abc", "abc1
  2. <c:if test="${empty abc}">
  3. abc is empty
  4. </c:if>
  5. <c:if test="${!empty abc}">
  6. abc is not empty
  7. </c:if>
  8. ");
  9. request.setAttribute("abc", "abc2");
  10. session.setAttribute("abc", "abc3");
  11. application.setAttribute("abc", "abc3");

${pageContext.request.contextPath} is the contextPath I prefer to use, it returns the web app path, as same as request.getContextPath()

detect if value is null, "", empty array, empty map or empty collection
<c:if test="${empty abc}">
abc is empty
<c:if test="${!empty abc}">
abc is not empty


quick condition

call a function with zero to multiple arguments

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

${} is the mark for that enclosed value is a variable or expression
With the same attribute name, c:out value always looking for the lowest level value first.

<c:out value="${abc}" default="default_value"></c:out> : default value if no attribute found in any level

<c:out value="${abc}"  escapeXml="false"></c:out>
true: make the output to be text only
false: make the output to be html
default is true

<c:out value="${}"></c:out>: user is an object, name is the field of the object
Which equals ((User)request.getAttribute("user")).getName();

<c:set var="abc" value="abc4" scope="request"></c:set>
Do not include ${} in var because it's already variable name, only use ${} when it's for value

<c:remove var="abc" scope="request"/>
If scope is not set, this will remove all four levels attributes with that variable name

<c:catch var="myException" >
<% int i=8/0; %>
<c:out value="${myException}"></c:out><c:out value="${myException.message}"></c:out>
Catch and assign the exception a variable name, so you can catch it latter

<c:if> can only work with single condition
<c:if test="${a=='hello'}">
<c:if test="${a!='hello'}">

<c:if test="${age>56}">

<c:if test="${age1>10 and age1<56}">
in middle

<br />

Same as:
<c:when test="${age>56}">Old</c:when>
<c:when test="${age<29}">Young</c:when>
<c:when test="${age>=29 and age<=56}">Middle</c:when>
<c:otherwise>Age not available</c:otherwise>


ArrayList<User> list = new ArrayList<User>();
list.add(new User("aaa",19));
list.add(new User("bbb",29));
list.add(new User("ccc",39));
pageContext.setAttribute("list", list);
1.Simple forEach
<c:forEach items="${list}" var="user">
<c:out value="${}" />
<c:out value="${user.age}" />
<br />
2. iterate fix times, like for loop
<c:forEach var="i" begin="1" end="10">
<c:out value="${i}" />
3. iterate fix times with number of steps, like for loop
instance 1 can use `step` also
<c:forEach var="i" begin="1" end="10" step="3">
<c:out value="${i}" />
4. can iterator through map or set
<c:forEach items="${map}" var="entry">

<c:forEach items="${set}" var="value">
5. nested level also work fine
<c:forEach items="${set}" var="value">

<c:forTokens items="a,b,c,d,e,f" begin="0" end="2" step="2" delims="," var="value">
<c:out value="${value}" />

<c:redirect url=""></c:redirect>

import other pages, can also pass parameters, use ${} to get passed value
<c:import url="/WEB-INF/a.jsp">
<c:param name="name" value="abc"></c:param>

No comments:

Post a Comment