Home Jstl JSTL Tutorial Part 2
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JSTL Tutorial Part 2

Advertisement
In this tutorial you will learn about some advance JSTL example of JSP

JSTL Tutorial Part 2

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

Example

<html>
<body  bgcolor=lightblue>
  <form   method=post  action="demo3.jsp">
  <select name="combo1">
   <option value="1">1   </option>
   <option value="2">2   </option>
   <option value="3">3   </option>
   <option value="4">4   </option>
   <option value="5">5   </option>
   <option value="7">7   </option>
  </select>

  <input  type=submit>
  <c:set var="s"   value="${param.combo1}"  />
 
Today is  
  <br>

<font size=24 color=red>

<c:choose>  
 
<c:when  test="${
s==1}">Sunday   </c:when>  

 
<c:when  test="${s==2}">Monday</c:when>  
 
<c:when  test="${s==3}">Tuesday</c:when>  
 
<c:when  test="${s==4}">Wednesday</c:when>  
 
<c:when  test="${s==5}">Thursday</c:when>

 <c:otherwise> 
 
select between 1 & 5     
 
</c:otherwise>
</c:choose>  

</body>  
</html>  

---------------------------------------------

Demo-5  deals with Iteration tag.
We are familiar with the 'for-each' construct.
JSTL's 'for-each'  also has the same functionality.
  In the following example, we have a String array.  named as 'colors'.
  By using the <c:forEach> tag, we iterate through the array and display the values.

------------------------------------

//demo5.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>
<% pageContext.setAttribute("colors",
 new String[]  {"red","green","blue","orange","black"} );   %>

<table>
<c:forEach  var="n"  items="${colors}"
   varStatus="a">
<tr>
<td>   <c:out value="${a.index}"   /> </td> <td>   <c:out value="${a.count}"   /> </td>
<td>  <c:out value="${a.first}"   /> </td>
<td>  <c:out value="${a.last}"  /> </td>
<td>   <c:out value="${a.current}" /> </td>
<tr>
</c:forEach>
</table>

We get the following display, when we execute the program.  

0

1

true

false

red

1

2

false

false

green

2

3

false

false

blue

3

4

false

false

purple

4

5

false

true

black

===============================================

<c:forEach> action tag contain the following attribute list:

items  :  the collection of items like String[]

var    :  a symbolic name for the collection

begin  :  the starting index of iteration

end  :  the ending index of iteration

step   :  incremental step

varStatus: symbolic name for current status.

If we assign the symbolic name 'a' for the status, we are able to access its properties such as index, count, whether it is first item,

whether it is last item and the current value.

Demo6 also deals with iteration tag.In the following example, the iteration starts at value 3 and ends at value 8 .It displays the values of n in each iteration.Each iteration increments the value of n automatically by 1,if step   is  not specified.

-----------------------------------------------

demo6.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>
<c:forEach  var="n"  begin="3"  end="8" >
  <c:out  value="${n}"  />   <br> 
</c:forEach>

3

4

5

6

7

8

===============================================

Demo7 deals with JSTL's 'forTokens' tag.<c:forTokens>, which iterates over a string of tokens separated by a set of delimiters like the stringTokenizer class in Java.

--------------------------------------------

demo7.jsp
<%@ page contentType="text/html" %>  
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>  
  <c:set var="s" value="SAM,DELHI,MCA,24,90"  />
<html>
<body>
   <table border="1">
   <tr>
   <th>Name</th>
   <th>Place</th>
   <th>Degree</th>
   <th>Age</th>
   <th>Mark</th>
   </tr>
   <tr>
<c:forTokens   items="${s}" 
  
delims=","   var="token"  >
   <td><c:out value="${token}"  /></td>
</c:forTokens>
  </tr>
  </table>
  <br>
  </font>
  </body>
  </html>
------------------------------------------

Name

Place

Degree

Age

Mark

kala

Kkdi

mca

23

87


------------------------------------------

The essential attributes of 'forTokens' tag are:

Attribute  Description

items      The string to tokenize

delims     The delimiter characters         that separate the tokens of   the string.

===============================================

Demo8 deals with URL-Related actions.<c:import>action tag imports the conent of a URL-based resource and provides a simple way to access URL-based resources that can either be included or processed within the JSP..
 In the following example,the import action tag imports the content of welcome.htm file here.So it displays the contents of demo8.jsp and welcome.htm.

--------------------------------------------

// welcome.htm
<html>
<body>
WELCOME
<br>
</body>
</html>
---------  

demo8.jsp
<%@ taglib  prefix="c"  uri="http://java.sun.com/jstl/core" %>
<c:import url="welcome.htm"/>
<c:out   value="to our web-site!" />
===============================================

In demo9 we discuss the <c:url> action tag.

<c:url> prints the value  of URL.It is easier to construct the hyperlinks.It is useful for session preservation (URL-Encoding).

In the following example,we use <c:url> to make a link to another html file. When we execute demo9, we get a link , with text 'send'.

When we click on the link, we are taken to welcome.htm.

--------------------------------------

demo9.jsp

<%@ taglib  prefix="c" uri="http://java.sun.com/jstl/core" %>  
<a   href="<c:url   value="http://localhost:8080/welcome.htm/
>">  
send  
</a>

--------------------------------------

===============================================

demo10 deals with <c:redirect> action tag.

This tag forwards the browser to the specified URL .

demo10.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>
<c:redirect
 url="http://localhost:8080/welcome.htm  />

===============================================

Finally, <c:param> tag is useful to send some parameter value to the page to which redirect occurs. In our example, the code redirects to sample.jsp, but it takes the param named 'a'

with value='SAM' , to the redirected page.
And, the sample.jsp accepts this param and prints it.
  <c:out value="${param.name1}"/>

----------------------------------------------

demo11.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>
<c:redirect  url="http://localhost:8080/jstldemos/
   core/sample.jsp" >
<c:param name="name1" value="SAM"/>
</c:redirect>
-------------------------------------------

( in a different file)
sample.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:out value="${param.name1}"/>

And to end our whirlwind tour of core-tags in JSTL, here is a demo which mixes EL of JSP-2(Expression Language of JSTL) with 'Expression' (also known as request-time Expression ) of JSP1.2.

 

demo12.jsp

<%@ page contentType="text/html" %>  
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %>  
JSTL  welcomes <br>  
<c:out   value="${param.text1}"   />  
<br>  
JSP Expression welcomes  
<%=request.getParameter("text1") %>

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 5, 2011

Ask Questions?    Discuss: JSTL Tutorial Part 2  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
taha
February 4, 2012
thank

thaks to you a lot this information is so useful for me and others
DMCA.com