Required Library file:
- jstl-1.2.jar
- mysql-connector-java-5.1.6-bin.jar
Example:
<%@ taglib
uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib
uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql"
prefix="sql" %>
<html>
<head>
<title> Tag Example </title>
</head>
<body>
<h2> 1.<font
color="green">jstl core tags started
http://java.sun.com/jsp/jstl/core</font></h2>
------------------------Simple IF LOOP-------------------------
<c:set var="salary"
scope="session" value="${2000*2}"/>
<c:if
test="${salary > 2000}">
<p>My salary is: <c:out
value="${salary}"/><p>
</c:if>
--------------------Exception--------------------------------
<c:catch var
="catchException">
<% int x = 5/0;%>
</c:catch>
<c:if test =
"${catchException != null}">
<p>The exception is :
${catchException} <br />
There is an exception:
${catchException.message}</p>
</c:if>
----------------------IF LOOP WITH ELSE--------------------------
<c:set
var="salary1" scope="session"
value="${1000}"/>
<c:if
test="${salary1 > 2000}">
<p>My salary is: <c:out
value="${salary1}"/><p>
<c:otherwise>
<p>My salary is: <c:out
value="${salary1}"/>
</c:otherwise>
</c:if>
-----------------------WHEN CONDITION------------------------------
<c:set var="s11"
scope="session" value="${1000}"/>
<p>Your salary is: <c:out
value="${s11}"/></p>
<c:choose>
<c:when
test="${s11==9999||s11==10000}">
Salary is Good
</c:when>
<c:when
test="${s11>=5000}">
Salary is Medium Level
</c:when>
<c:otherwise>
You had Low salary
</c:otherwise>
</c:choose>
-----------------------------DIV IMPORT----------------------------
<div>
<c:import varReader="java.io.Reader"
url="newjsp.jsp"/>
<%--<c:out
value="${data}"/>--%>
</div>
--------------------------------FOREACH LOOP------------------------
<br/>
<br/>
<c:forEach
var="i" step="2" begin="1" end="10">
Item: <c:out
value="${vengat}"/>
</c:forEach>
--------------------------------------------------------------------
<br/>
<br/>
<c:forEach
var="t"
items="dinesh,ganesh,murugan,balaji,anand,lalitha"
step="2">
Item: <c:out value="${t}"/>
<p>
</c:forEach>
<br/>
<br/>
----------------------PASSING VARIABLE TO OTHER PAGE----------------
<br/>
<br/>
<c:url
value="newjsp.jsp" var="myURL">
<c:param name="trackingId"
value="1234"/>
<c:param name="reportType"
value="summary"/>
</c:url>
<c:import
url="${myURL}"/>
--------------------------------------------------------------------
<br/>
<br/>
<c:forTokens
var="e1"
items="henry,murugan,anand,lalitha,ganesh,dinesh,balaji"
delims=",">
<p> Item: <c:out
value="${e1}"/> </p>
</c:forTokens>
<%--<c:redirect
url="newjsp.jsp" >
<c:param name="trackingId"
value="123456789"/>
<c:param name="reportType"
value="summary11111111"/>
</c:redirect>--%>
--------------------------------URL---------------------------------
<br/>
<br/>
<a href="<c:url
value="newjsp.jsp"/>">Test</a>
---------------------------------FORMAT TAG--------------------------
<br/>
<br/>
<h2> 2. <font
color="blue">jstl Formatting tags started
http://java.sun.com/jsp/jstl/fmt</font></h2>
<c:set
var="sr" value="120000.2309"/>
<b>1.
<fmt:formatNumber value="${sr}"
type="currency"/>
</b></p>
<b>2.
<fmt:formatNumber value="${sr}" maxIntegerDigits="3"
type="number"/> </b> </p>
<b>3.
<fmt:formatNumber value="${sr}" type="number"
maxFractionDigits="3"/> </b> </p>
<b>4.
<fmt:formatNumber value="${sr}" type="number"
groupingUsed="false"/></b></p>
<b>5.
<fmt:formatNumber value="${sr}"
type="percent"/></b></p>
<b>6.
<fmt:formatNumber value="${sr}" type="percent"
maxIntegerDigits="3"/></b></p>
<b>7.
<fmt:formatNumber value="${sr}" type="percent"
minFractionDigits="3"/></b></p>
<b>8.
<fmt:formatNumber value="${sr}" type="percent"
minIntegerDigits="2"/></b></p>
<b>9.
<fmt:formatNumber type="number" pattern="###.###E0"
value="${sr}" /></b></p>
<p>Currency in USA :
<b> <fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${sr}"
type="currency"/></b></p>
-------------------------------------------------------------------
<br/>
<br/>
<c:set
var="k7" value="1250003.350"/>
<fmt:parseNumber
var="j" type="number" value="${k7}" />
<b> Parsed format
with fraction 1: <c:out
value="${j}"/></b></p>
<fmt:parseNumber var="j"
integerOnly="true"
type="number" value="${k7}" />
<b> Parsed format
without fraction 2: <c:out value="${j}"/></b></p>
<h3>Number
Parsing:</h3>
<c:set
var="k6" value="1250003.350" />
<fmt:parseNumber
var="i" type="number" value="${k6}" />
<p>Parsed Number (1)
: <c:out value="${i}" /></p>
<fmt:parseNumber
var="i"
integerOnly="true"
type="number"
value="${k6}" />
<p>Parsed Number (2)
: <c:out value="${i}" /></p>
-------------------------------------------------------------------
<br/>
<br/>
<c:set
var="dt" value="<%=new java.util.Date()%>"/>
<p>Formatted Date 1:
<fmt:formatDate type="time"
value="${dt}"
/></p>
<p> Formatted Date
2: <fmt:formatDate type="date"
value="${dt}"/></p>
<p> Formatted Date
3. <fmt:formatDate type="both" value="${dt}"/>
<p> Formatted Date
4. <fmt:formatDate type="both" dateStyle="short"
value="${dt}"/>
<p> Formatted Date
5. <fmt:formatDate type="both" dateStyle="medium"
timeStyle="short" value="${dt}"/>
<p> Formatted Date
6. <fmt:formatDate type="both" dateStyle="long"
timeStyle="long" value="${dt}" />
<p> Formatted Date
7. <fmt:formatDate type="time" timeStyle="medium"
pattern="G"
value="${dt}" />
<p> Formatted Date
8. <fmt:formatDate pattern="yyyy-mm-dd" value="${dt}" />
--------------------------------------------------------------------
<br/>
<br/>
<c:set
var="pd" value="09-05-2012"/>
<fmt:parseDate
value="${pd}" var="pdat"
pattern="dd-mm-yyyy"/>
<p>Parsed
Date:<c:out value="${pdat}"/></p>
<c:set
var="now" value="20-10-2010" />
<fmt:parseDate
value="${now}" var="parsedEmpDate"
pattern="dd-MM-yyyy"
/>
<p>Parsed Date:
<c:out value="${parsedEmpDate}" /></p>
--------------------------------------------------------------------
<br/>
<br/>
<fmt:bundle
basename="Example" prefix="count.">
<fmt:message
key="one"/><br/>
<fmt:message
key="two"/><br/>
<fmt:message
key="three"/><br/>
</fmt:bundle>
<fmt:bundle
basename="Example">
<fmt:message
key="count.one"/><br/>
<fmt:message
key="count.two"/><br/>
<fmt:message
key="count.three"/><br/>
</fmt:bundle>
--------------------------------------------------------------------
<br/>
<fmt:bundle
basename="samplebundle" prefix="v.">
<fmt:message
key="1"/><br/>
<fmt:message
key="2"/><br/>
<fmt:message
key="3"/></br>
</fmt:bundle>
<fmt:bundle
basename="samplebundle">
<fmt:message
key="v.1"/><br/>
<fmt:message
key="v.2"/><br/>
<fmt:message
key="v.3"/></br>
</fmt:bundle>
--------------------------------------------------------------------
<br/>
<br/>
<!-- Change the Locale
-->
<fmt:setLocale
value="es_ES"/>
<fmt:bundle
basename="samplebundle">
<fmt:message
key="v.1"/><br/>
<fmt:message
key="v.2"/><br/>
<fmt:message
key="v.3"/><br/>
</fmt:bundle>
--------------------------------------------------------------------
<br/>
<br/>
<fmt:setLocale
value="en" />
<fmt:setBundle
basename="setBundle2" var="hi"/>
<fmt:setBundle
basename="samplebundle" var="si"/>
<fmt:setBundle
basename="Example" var="ex"/>
<fmt:message
key="s.a" bundle="${hi}"/></p>
<fmt:message
key="v.2" bundle="${si}"/></p>
<fmt:message
key="count.three" bundle="${ex}"/></p>
--------------------------------------------------------------------
<br/>
<br/>
<c:set
var="now" value="<%=new java.util.Date()%>" />
<table border="1"
width="100%">
<tr>
<td width="100%" colspan="2"
bgcolor="#0000FF">
<p align="center">
<b>
<font
color="#FFFFFF" size="4">Formatting:
<fmt:formatDate
value="${now}" type="both"
timeStyle="long"
dateStyle="long" />
</font>
</b>
</p>
</td>
</tr>
<c:forEach var="zone"
items="<%=java.util.TimeZone.getAvailableIDs()%>">
<tr>
<td width="51%">
<c:out
value="${zone}" />
</td>
<td width="49%">
<fmt:timeZone
value="${zone}">
<fmt:formatDate
value="${now}" timeZone="${zn}"
type="both" />
</fmt:timeZone>
</td>
</tr>
</c:forEach>
</table>
--------------------------------------------------------------------
<br/>
<br/>
<fmt:requestEncoding
value="UTF-8" />
<fmt:setLocale
value="es_ES"/>
<fmt:setBundle
basename="setBundle2" var="lang"/>
<fmt:message
key="s.a" bundle="${lang}"/><br/>
<fmt:message
key="s.b" bundle="${lang}"/><br/>
<fmt:message
key="s.c" bundle="${lang}"/><br/>
<br/>
<br/>
--------------------Mysql database connection ----------------
<h2> 3.<font
color="pink">jstl core tags started http://java.sun.com/jsp/jstl/sql</font></h2>
<sql:setDataSource
var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/sample" user="root"
password="root"
/>
<c:set
var="un" value="ganesh"/>
<sql:query
dataSource="${snapshot}" var="rs">
select * from login where
username="${un}";
</sql:query>
<table
border="1" width="100%">
<tr>
<th>User
Name</th>
<th>Password</th>
<th>Date</th>
</tr>
<c:forEach
var="row" items="${rs.rows}">
<tr>
<td><c:out
value="${row.username}"/></td>
<td><c:out
value="${row.pass}"/></td>
<td><c:out
value="${row.date}"/></td>
</tr>
</c:forEach>
</table>
-----------------------------Form Creation---------------------------
<br/>
<br/>
<form
action="welcome_jstl.jsp" method="post">
Enter the User Name:
<input type="text" name="un"/> </p>
<br/>
Enter the Password: <input type="text"
name="pw"/> </p>
<br/>
<center> <input
type="submit" value="Resume"/> <input
type="reset" value="Reset"/></center>
</form>
<%--<sql:update
dataSource="${snapshot}" var="up">
insert into login
values("${paramValues.un}","${paramValues.pw}");
</sql:update>
----------------------------------------------
<sql:query
dataSource="${snapshot}" var="rs1">
select * from login;
</sql:query>
----------------------------------------------
<table
border="1" width="100%">
<tr>
<th>User
Name</th>
<th>Password</th>
</tr>
<c:forEach
var="row1" items="${rs1.rows}">
<tr>
<td><c:out
value="${row1.username}"/></td>
<td><c:out value="${row1.pass}"/></td>
</tr>
</c:forEach>
</table>--%>
-----------------------------------------------
------------------------------------------------
</body>
</html>
|
0 comments:
Post a Comment