// The following demo is from
// The latest offering from Apress is Pro JSP, 3rd Edition
// Author: Brown et al.
// ISBN : 1-59059-225-5
// URL :Pro JSP, 3rd Edition
// download the jstltest.war for testing the following code.
// set
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:set var="browser" value="${header['User-Agent']}" scope="session"/>
Your browser is : <B><c:out value="${browser}"/></B>
// tokens
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:set var="queryResult" value="Dan,Jepp,Male,26,Java Developer,London"
scope="request" />
<html>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Sex</th>
<th>Age</th>
<th>Occupation</th>
<th>Location</th>
</tr>
<tr>
<c:forTokens items="${queryResult}" delims="," var="token">
<td><c:out value="${token}"/></td>
</c:forTokens>
</tr>
</table>
</body>
</html>
// remove
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:set var="browser" value="${header['User-Agent']}" scope="session"/>
<c:remove var="browser" scope="session"/>
Your browser is : <B><c:out value="${browser}"/></B>
// output
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="com.apress.projsp20.ch04.Book"%>
<jsp:useBean id="book" class="com.apress.projsp20.ch04.Book">
<jsp:setProperty name="book" property="title" value="Pro JSP, 3rd Edition"/>
<jsp:setProperty name="book" property="author" value="Brown et al."/>
<jsp:setProperty name="book" property="isbn" value="1-59059-225-5"/>
<jsp:setProperty name="book" property="url" value="http://www.apress.com/book/bookDisplay.html?bID=256"/>
</jsp:useBean>
The latest offering from Apress is <i><c:out value="${book.title}"/></i><BR>
Author: <c:out value="${book.author}"/><BR>
ISBN : <c:out value="${book.isbn}"/><BR>
URL :<a href="<c:out value="${book.url}"/>"><c:out value="${book.title}"/></a>
// choose
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="com.apress.projsp20.ch04.CalendarBean"%>
<jsp:useBean id="cal" class="com.apress.projsp20.ch04.CalendarBean"/>
The time is currently : <BR><BR>
<c:out value="${cal.time}"/>,
<c:set var="hour" value="${cal.hour}" scope="request" />
<B>
<c:choose>
<c:when test="${hour > 0 && hour <=11}">
Good Morning!
</c:when>
<c:when test="${hour >= 12 && hour <=17}">
Good Afternoon!
</c:when>
<c:otherwise>
Good Evening!
</c:otherwise>
</c:choose>
</B>
// if
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="com.apress.projsp20.ch04.CalendarBean"%>
<jsp:useBean id="cal" class="com.apress.projsp20.ch04.CalendarBean"/>
The time is currently : <BR><BR>
<c:out value="${cal.time}"/>,
<c:set var="hour" value="${cal.hour}" scope="request" />
<B>
<c:if test="${hour > 0 && hour <=11}">
Good Morning!
</c:if>
<c:if test="${hour >= 12 && hour <=17}">
Good Afternoon!
</c:if>
<c:if test="${hour >= 18 && hour <=23}">
Good Evening!
</c:if>
</B>
// import
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:import url="/xml/books.xml"/>
// format
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<fmt:setLocale value="es_ES" scope="request"/>
<fmt:setBundle basename="labels"/>
<H2>Wrox Survey</H2>
<FORM action="">
<TABLE>
<TR>
<TD><fmt:message key="nameQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><fmt:message key="ageQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><fmt:message key="locationQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><input type="submit" value='<fmt:message key="submit"/>'></TD>
</TR>
</TABLE>
</FORM>
// format
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<fmt:setLocale value="en_GB" scope="request"/>
<fmt:setBundle basename="labels"/>
<H2>Survey</H2>
<FORM action="">
<TABLE>
<TR>
<TD><fmt:message key="nameQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><fmt:message key="ageQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><fmt:message key="locationQuestion"/></TD>
<TD><input type="text" size="16"></TD>
</TR>
<TR>
<TD><input type="submit" value='<fmt:message key="submit"/>'></TD>
</TR>
</TABLE>
</FORM>
// format date
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<jsp:useBean id="now" class="java.util.Date" />
<H1>Examples of Date & Time Formatting</H1>
<HR>
<H2>Default Time Zone</H2>
Default format : <fmt:formatDate value="${now}"/><BR>
A Date only in a Custom dd/MM/yyyy format :
<fmt:formatDate value="${now}" type="DATE" pattern="dd/MM/yyyy"/><BR>
A Time only in MEDIUM format :
<fmt:formatDate value="${now}" type="TIME" dateStyle="MEDIUM"/><BR>
A Date and Time in FULL format :
<fmt:formatDate value="${now}" type="BOTH" dateStyle="FULL" timeStyle="FULL"/><BR>
<HR>
<H2>America/Los_Angeles Time Zone</H2>
<fmt:timeZone value="America/Los_Angeles">
Default format : <fmt:formatDate value="${now}"/><BR>
A Date only in a Custom MM-dd-yyyy format :
<fmt:formatDate value="${now}" type="DATE" pattern="MM-dd-yyyy"/><BR>
A Time only in MEDIUM format :
<fmt:formatDate value="${now}" type="TIME" dateStyle="MEDIUM"/><BR>
A Date and Time in FULL format :
<fmt:formatDate value="${now}" type="BOTH" dateStyle="FULL" timeStyle="FULL"/><BR>
</fmt:timeZone>
// format number
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<H2>Number Format Example </H2>
Formatting <B>123.4</B> becomes : <fmt:formatNumber value="123.4" type="NUMBER" minFractionDigits="3" /><BR>
<HR>
<H2>Currency Format Example </H2>
<c:set var="salary" value="125000" />
Salary =<c:out value="${salary}"/><BR>
<fmt:setLocale value="en_GB"/>
Formatting salary with Locale <B>en_GB</B> becomes : <fmt:formatNumber type="CURRENCY" value="${salary}" /><BR>
<fmt:setLocale value="it_IT"/>
Formatting salary with Locale <B>it_IT</B> becomes : <fmt:formatNumber type="CURRENCY" value="${salary}" /><BR>
<HR>
// XML transform
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:import url="books.xml" var="books" />
<c:import url="books.xsl" var="xslt" />
<x:transform xml="${books}" xslt="${xslt}"/>
// XML set
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:import url="book.xml" var="url" />
<x:parse xml="${url}" var="book" scope="session" />
<x:set select="$book/book/title" var="title" scope="session"/>
<x:set select="$book/book/author" var="author" scope="session"/>
<x:set select="$book/book/url" var="bookUrl" scope="session"/>
<B>
<x:out select="$title" />
</B><BR>
<x:out select="$author" /><BR>
<x:out select="$bookUrl" /><BR>
// XML output
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:import url="book.xml" var="url" />
<x:parse xml="${url}" var="book" scope="session" />
<B>
<x:out select="$book/book/title"/>
</B><BR>
<x:out select="$book/book/author"/><BR>
<x:out select="$book/book/url"/><BR>
// XML for each
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:import url="books.xml" var="url" />
<x:parse xml="${url}" var="book" scope="session" />
<x:forEach select="$book/books/book" var="currentBook">
<x:choose>
<x:when select="$currentBook/publisher='Apress'">
<font color="red">
<H1>Another great title from Apress!</H1>
</font>
</x:when>
<x:when select="$currentBook/publisher='Bloggs Publishers'">
<font color="red">
<H2>A reasonable title from Bloggs Publisher!</H2>
</font>
</x:when>
<x:otherwise>
<font color="red">
<H3>A title from an unknown publisher</H3>
</font>
</x:otherwise>
</x:choose>
<TABLE BORDER="0">
<TR>
<TD COLSPAN="2"><B><x:out select="$currentBook/title"/><B></TD>
</TR>
<TR>
<TD><i>Author :</i></TD>
<TD><x:out select="$currentBook/author"/></TD>
</TR>
<TR>
<TD><i>ISBN :</i></TD>
<TD><x:out select="$currentBook/isbn"/></TD>
</TR>
<TR>
<TD><i>Published :</i></TD>
<TD><x:out select="$currentBook/published"/></TD>
</TR>
<TR>
<TD><i>URL :</i></TD>
<TD><a href='<x:out select="$currentBook/url"/>'><x:out select="$currentBook/title"/></a></TD>
</TR>
</TABLE>
<HR>
</x:forEach>
// XML if
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:import url="book.xml" var="url" />
<x:parse xml="${url}" var="book" scope="session" />
<x:if select="$book/book/publisher='Apress'">
Another great title from Apress! :
<P>
<B><x:out select="$book/book/title"/></B><BR>
<x:out select="$book/book/author"/><BR>
<x:out select="$book/book/url"/><BR>
</x:if>
|