Showing posts with label JSP Standard Tag Library. Show all posts
Showing posts with label JSP Standard Tag Library. Show all posts

March 2, 2011

JSTL 1.1 - <c:choose>, <c:when> and <c:otherwise> example


<c:set var="userType" scope="request" value="none" />
<c:choose>
   <c:when test="${requestScope.userType == 'regular'}">
      Welcome! You are a regular user.
   </c:when>
   <c:when test="${requestScope.userType == 'premium'}">
      Welcome! You are a premium user.
   </c:when>
   <c:otherwise>
      Welcome! You are not a registered user.
   </c:otherwise>
</c:choose>

prints,

Welcome! You are not a registered user.

JSTL 1.1 - <c:forTokens> example


Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,

1t1
2t2
3t3
4t4
5t5
6t6
7t7
8t8
9t9

Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="testString" scope="application" value="t1;t2/t3;t4/t5/t6,t7,t8;t9" />
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c" begin="1" end="7" step="2">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,



1t2
2t4
3t6
4t8

JSTL 1.1 - <c:catch> example


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:catch var="ex" >
   <% int j=3/0; %>
</c:catch>
<c:out value="${ex.message}" />

prints,

/ by zero

JSTL 1.1 - <c:remove> example


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="name" value="Dinesh" scope="session" />
<c:out value="${sessionScope.name}" /><br/>
<c:remove var="person" scope="session" />
<c:out value="${sessionScope.name}" /><br/>


prints, (the second <c:out> does not display anything)


Dinesh



JSTL 1.1 - <c:set> example

Example 1: Setting an attribute in some scope

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="userType" scope="request" value="premium" />
<c:out value="${requestScope.userType}" default="normal" />

prints,

premium


Example 2: setting JavaBean property
Note: PersonBean.java is a simple JavaBean with properties firstName and age


<%@ page import="info.icontraining.beans.PersonBean"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
 PersonBean p = new PersonBean();
 session.setAttribute("person", p);
%>
<c:set target="${sessionScope.person}" property="firstName" value="Dinesh" />
<c:set target="${sessionScope.person}" property="age" value="32" />
<c:out value="${sessionScope.person.firstName}" /><br/>
<c:out value="${sessionScope.person.age}" /><br/>


prints,

Dinesh
32


Example 2: setting a key-value pair in a Map
Note: PersonBean.java is a simple JavaBean with properties firstName and age

<%@ page import="java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% 
   Map m = new HashMap();
   session.setAttribute("map", m);
%>
<c:set target="${sessionScope.map}" property="firstName" value="Dinesh" />
<c:set target="${sessionScope.map}" property="lastName" value="PC" />
<c:out value="${sessionScope.map.firstName}" /><br/>
<c:out value="${sessionScope.map.lastName}" /><br/>

prints,

Dinesh
PC

JSTL 1.1 - <c:if> example


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

<% request.setAttribute("userType","premium"); %>
<c:if test="${requestScope.userType == 'premium'}" var="isPremiumMember" scope="session" >
   Welcome! You are a privileged customer.
</c:if>
<br/>
<c:out value="${sessionScope.isPremiumMember}" />

prints,

Welcome! You are a privileged customer.
true

JSTL 1.1 - <c:forEach> example

test.jsp

Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String[] arr = new String[] {"dog", "cat", "pig", "cow"};
   application.setAttribute("arrayAttribute", arr);
%>

<table border="1">
<c:forEach var="element" items="${arrayAttribute}" varStatus="c">
 <tr><td>${c.count}</td>
 <td>${element}</td></tr>
</c:forEach>
</table>

prints,


1dog
2cat
3pig
4cow


Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String[] arr = new String[] {"dog", "cat", "pig", "cow", "horse", "rhino", "hippo"};
   application.setAttribute("arrayAttribute", arr);
%>

<table border="1">
<c:forEach var="element" items="${arrayAttribute}" varStatus="c" begin="1" end="5" step="2">
 <tr><td>${c.count}</td>
 <td>${element}</td></tr>
</c:forEach>
</table>

prints,


1cat
2cow
3rhino

JSTL 1.1 - <c:out> example

test.jsp


Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="<h1>Hello</h1>" escapeXml="false" />

prints:

Hello


Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="<h1>Hello</h1>" escapeXml="true" />

prints:

<h1>Hello</h1>

Example 3:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% session.setAttribute("cart",null); %>
<c:out value="${sessionScope.cart}" default="Shopping Cart is Empty" />

prints:

Shopping Cart is Empty  (when ${sessionScope.cart} evaluates to a null value)