JSP – Standard Tag Library (JSTL)

In this article, we will learn the different tags in JSP. The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful utility JSP tags which encapsulates the core functionality common to many JSP applications.

Advantages of JSTL

  • Easy to read and maintain.
  • Reusability of the code.
  • JSTL reduces the use of scriptlets in a JSP page.
  • JSTL allows using predefined tags instead of writing the java code.
  • We can easily access and manipulate the application data without using the scriptlets.
  • JSTL is easy to learn and understand without knowledge of Java programming.
  • It easier to read because of JSTL based on XML which is similar to HTML.

Disadvantages of JSTL

  • Large complex JSP pages make use of JSTL tag libraries which exceeds class size limitation. Because the size of the java class is limited.
  • JSTL is not so flexible and not as extensive as JSP Scriptlet. The JSTL is not nearly as evolved as Java itself.
  • It is easier for a programmer who knows HTML skill set. If JSTL gets translated into Java code, an experienced programmer may wonder why he does not do that himself and save time. So it may difficult to carry out for experienced programmers.

To use JSTL tags, you have to do the following.

  1. Copy jstl.jar and standard.jar to your project WEB-INF/lib folder.
  2. Use the following tag lib directive in any JSP.
    <%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>

List of JSTL Core Tags

  1. <c:out>
  2. <c:set>
  3. <c:remove>
  4. <c:if>
  5. <c:choose>
  6. <c:when>
  7. <c:otherwise>
  8. <c:forEach>
  9. <c:import>
  10. <c:redirect>
  11. <c:param>
  12. <c:catch>
  13. <c:url>

1) <c:out>: out tag is used to display data to JSP output stream. attributes: value.
Usage:

<c:out value=”msg”/>
<c:out value=”${msg}”/> 
${msg}

2) <c: set>: set tag is used to set the attribute in the required scope. attributes: var, value,scope
Usage:

<c:set var=”em” value=”[email protected]” scope=”session”/>

3) <c: remove>: remove tag is used to remove the attributes from the required scope. Attributes: var, scope
Usage:

<c:remove var=”em” scope=”session”/>

4) <c: if>: if a tag is used to perform simple conditional checks. Attributes: test
Usage:

<c:if test = “${sname eq ‘xadmin’}”>
Your name is xadmin
</c:if>
<c:if test=”${sname ne ‘xadmin’}”>
Your name is not xadmin
</c:if>

5) <c:choose>

6) <c:when>

7) <c:otherwise>

These tags are used to perform simple conditional checks like a switch statement
attributes:
choose- no attributes available
when –test
otherwise – no attributes available

Usages:

<c:choose>
<c:when test=”${sid eq 99}”>
id is 99
</c:when>
<c:when test=”${sid eq 88}”>
id is 88
</c:when>
<c:otherwise>
id is unknown
</c:otherwise
</c:choose>

8) <c:forEach>: forEach tag is used to access the elements of collection or array.
Attribute: var, items, start, end, step, varStatus

Usage:
using forEach tag, You can access the following type of data.

  1. Collection of strings, Wrappers, and Date.
  2. Collection of Collections
  3. Collection of User-defined class object.
  4. Map objects
  5. Collection of Map objects.

1) Collection of Strings, Wrappers, and Date.

In Servlet

ArrayList al = new ArrayList();
al.add(new Integer(123));
......
String str[]= {“dd”, “ss”, ....};
req.setAttribute(“Al”, al);
ses.setAttribute(“STR”, str);

In JSP

<c:forEach var=”x” items=”${requestScope.AL}”>
${x} 
<br/>
</c:forEach>
<c:forEach var =”x” items = “${sessionScope.STR}”>
${x}
 <br/>
</c:forEach>

2) Collection of Collections

In Servlet

ArrayList al = new ArrayList();
ArrayList al1= new ArrayList();
al1.add(new Integer(123));
ArrayList al2= new ArrayList();
al2.add(new Integer(123));
al.add(al1);
al.add(al2);
ses.setAttributes(“AL”,al);

In JSP

<c:forEach var=”list” items=”${sessionScope.AL}”>
<c:forEach var =”x” items = “${list}”>
${x} 
<br/>
</c:forEach>
</c:forEach>

3) Collection of User Defined class object

In Servlet

ArrayList al = new ArrayList();
......
al.add(cust1);
al.add(cust2);
ses.setAttribute(“AL”, al);

In JSP

<c:forEach var=”cust” items=”${sessionScope.AL}”>
Cid: ${cust.cid}Cname:${cust.cname}
Email: ${cust.email}
Street: ${cust.address.street}
City:${cust.address.city}
</c:forEach>

4) Map object

In servlet

HashMap hm = new HashMap();
hm.put(“sid”, “11”);
hm.put(“sname”,”xadmin”);
ses.setAttribute(“HM”,hm);

In JSP

<c:forEach var=”key” items = “${sessionScope.HM}”>
${key} : ${sessionScope.HM[‘${key}’]}
</c:forEach>

5) Collection of Map objects

In Servlet

HashMap hm= new HashMap();
hm.put(“sid”,”11”);
hm.put(“sname”, “xadmin”);
..
ArrayList al = new ArrayList();
......
al.add(hm);
al.add(hm);
ses.setAttribute(“AL”, al);

In JSP

sid:${sessionScope.HM[“sid”]}
Sname: ${sessionScope.HM[“sname”]}
<c:forEach var=”map” items=”${sessionScope.AL}”>
<c:forEach var=”key” items=”${map}”>
${key}:${map[“${key}”]}
</c:forEach>
</c:forEach>

Pagination

In Servlet

ArrayList al= new ArrayList();
al.add(new Integer(123));
req.setAttribute(“AL”, al);
int start=1;
int end=100;
ses.setAttribute(“START”,start);
ses.setAttribute(“END”,end);
Integer i1=(Integer)ses.getAttribute(“START”);
Integer i1=(Integer)ses.getAttribute(“START”);
int start=i1.intValue()+100;
int end=i2.intValue()+100;
ses.setAttribute(“START”,start);
ses.setAttribute(“END”,end);
<c:forEach var=”x” items =”${requestScope.AL}” start=”${START}” end=”${END}”>
${x} 
<br/>
</c:forEach>

9) <c:import>: import tag is used to include the resource(htlm or jsp); attributes:url

Usage:

<c:import url=”header.jsp”/>

<c:import url=”http://www.google.co.in”/>

<jsp:inculde> can include the resources which are in same server. Where as <c:import> can inculde the resource which are in same server and different server.

10) <c:redirect>: redirect tag is used to redirect the request to the specified resource(htnl or jsp). Attributes:url

Usage:

<c:redirect url=”header.jsp”/>
<c:redirect url=”http://www.google.co.in”/>
jsp:forward> can forward request to resources which are in same application where as <c:redirect> can forward request to the resources which are in same application and different application running in the same server or different server.

11) <c:param>: param tag is used to define parameters. Attributes: name, value

Usage:

<c:param name= “email” value=-”[email protected]”/>
<c:param> must be used along with <c:import> and <c:redirect>

12)<c:catch>: catch tag is used to impement try catch functionality.

Usage:
<c:catch>
………
</c:catch>

13)<c:url>: url tag is used to encode the URL with session id

Attributes: value

Usage:

<c:url value=”hello.jsp”/>
<a href=”hello.jsp”>
 CLICK HERE
</a>
<a href=’<c:url value=”hello.jsp”/>’> 
Click here
</a>
<a href=’<%=response.encodeURL(“hello.jsp”)%>’> 
Click Here
</a>

Note:

${entry} and ${sessionScopeHM[‘${entry}’]} are providing Map.Entry class objects.
<c:forEach var=”entry” items=”${sessionScope.HM}”>
${entry.key}:${entry.value}
</c:forEach>

Conclusion:

In this article, we learned about the JSP- JSTL. JSTL is built on top of a server-side JSP technology and the JSTL pages are also JSP pages because the work is done inside the JSP page but the tags are separate.JSTL basically helps in order to separate the logic of business from the logic of presentation in an excellent manner. Please feel free to drop a comment in the comment box, if you have any doubt regarding the topic or you want to share more information about the topic.

Happy learning!