In this tutorial you will learn about the JSTL of JSP

In this tutorial you will learn about the JSTL of JSP


In the second part of this tutorial on JSTL, the author explains how the tags in the core-group can be used in JSP pages, with a number of simple examples.

We are now ready to  experiment with all the tags in the ?core? library. The core tags have the following uniform  ?uri?.


( However, in the book by Hans Bergsten  titled,"Java Server Pages" ( third edition),

(OReilly pub)the uri is consistently given as :

''.It looks as if there has been some change in specification and grammar, after it was published!

This appears to be wrong as  the server threw   exception.The correct uri is :


The prefix is  ?c:? 

The following tags are available in the ?core? library.

( Remember them as a dozen!).

<c:if  test= ?  
<c:choose  ,   <c:when ,  <c:otherwise


We will now see simplest illustrations for the above tags.There are a dozen demos, to bring out the features of each of these tags.


demo1.jsp  uses   <c:set &  <c:out  tags.

:We create demo1.jsp  as:



//   demo1.jsp

<%@ page contentType="text/html" %>
<%@ taglib  prefix="c"  
uri=""  %>
<body   bgcolor=lightblue>
<form   method=post  action="demo1.jsp">
NAME <input  type=text  name="text1"><br>
PLACE<input  type=text  name="text2"><br>
<input type=submit>

 NAME:<c:out  value="${param.text1}"  /><br>
PLACE:<c:out   value="${param.text2}"  />

In all the previous examples, we invoked the JSP file through a html  file. But, in demo1.jsp, we are  posting the page to itself.( in style!).( but there is no 'retention of data' , unlike

We start Tomcat5, and type the url as :

 ?http://localhost:8080/demo1.jsp?.  in the browser.We get a form with two text boxes and a submit button. We fill up the textboxes with ?name? and ?place? and submit.

The demo1.jsp executes and displays the  values entered by the user.due to the JSTL tags

<c:out   value=?${param.text1}   />  etc.

That is about our first and introductory example.

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

   The second example is very important. When the user enters data in a number of fields, it is tedious to collect the data and transfer it to jsp page for processing. In our example, we are collecting data about a player,  such as his name, place and game. We can have much more but we are restricting for space considerations. JSP has an action tag , known as 'jsp:setProperty'. Using this along with a standard javabean, we can extract data and transfer it to our program in a single step.

The syntax is

id="bean1"  class="ourbeans.player"  >
name="bean1"   property="*"   />

:( the * sign denotes 'all').


   But, we should first create the 'player ' bean with all the attributes and getter & setter methods, as shown.


package ourbeans;

public class player{
String   name;
String   place;
String   game;
public player(){
name="  ";
place=" ";
game=" ";  

 public void setName(String a){

 public void setPlace(String b){

 public void setGame(String c){

 public String   getName(){
   return  name;

 public String  getPlace(){
   return  place;

 public String  getGame(){
   return  game;



   In demo2.jsp, we collect the data and then display  the data entered by the user.

Note that instead of {param.text1}, we are using {}. We should carefully name the html form controls with the corresponding attribute names given in the bean. We cannot name the controls as 'text1' etc, now!

<c:out   value="${}"   />
<c:out   value="${}"  />
<c:out   value="${}"   />


 We get correct result.


//   demo2.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" 
uri=""  %>

<form method=post action="demo2.jsp">
<jsp:useBean id="bean1" class="ourbeans.player">

name="bean1" property="*"   />

Name <input   type=text   name="name"><br>
Place<input  type=text  name="place"><br>
Game<input   type=text   name="game"><br>
<input type=submit>

Name: <c:out value="${}"  /><br>
Place: <c:out value="${}"  /><br>
Game: <c:out value="${}"  />


Once again, it will be noticed that there is no java code in this example, as everything is being done by tags, only..


We are now ready to take up examples for 'condition'  tags.

  There are two types of 'condition tags'.

namely, <c:if>   &  <c:choose>.

In the third demo, we learn how to use the <c:if  tag.


<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri=""  %>
<body  bgcolor=lightblue>  
<form   method=post   action=demo3.jsp>  
  <select  name="combo1"> 
<option   value="sam">sam 
  <option  value="tom">tom
<input   type=submit>
<c:set   var="s"   value="${param.combo1}"  />
<c:out value="${s}"  />

<c:if  test="${s  eq   'sam'  }"   >
   <c:out   value="Good Morning...SAM!"  />

<c:if   test="${s   = =   'tom'}"  >  
  <c:out  value=" How Are You?....TOM!"  />  




There is a combo with two options, namely

'sam' and 'tom'. If the user selects 'sam' and submits the form, he gets 'GoodMorning ...SAM!". If he selects 'tom' instead, he gets

'How are you..TOM?'.  

  The above code is no ?Rocket-Science? as American  authors say!But , if we are careless  in typing the names ?sam? or ?tom? in the test condition, we could spend hours together , trying to coax this code into functioning! We should not leave space after the single quote  in the 'test expression'. Second point worth noting in the above example is that we can use either == ( double equal to) or eq  to test equality.


In the fourth example which follows, we take up <c:choose> tag.

The syntax is:

<c:choose >
   <c:when   test="   "   > 
<c:otherwise>  something  </c:otherwise> 

The peculiarity to be noted here is that   unlike <c:if , where we had to explicitly  use <c:out for printing , no such <c:out has been used here., and yet the result is displayed,

because 'choose' includes 'displaying'..

When we choose '7', "select between 1 & 5 " will be displayed!

Click Here to read more



Copyright © RoseIndia.Net 2018