Home Jstl AN INTRODUCTION TO JSTL
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

AN INTRODUCTION TO JSTL

Advertisement
In this tutorial you will learn about the JSTL of JSP

AN   INTRODUCTION  TO   JSTL

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?.

?http://java.sun.com/jstl/core'

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

(OReilly pub)the uri is consistently given as :

'http://java.sun.com/jsp/jstl/core'.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 :

'http://java.sun.com/jstl/core'.)

The prefix is  ?c:? 

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

( Remember them as a dozen!).

   <c:set
  
<c:out
  
<c:if  test= ?  
  
<c:choose  ,   <c:when ,  <c:otherwise
  
<c:forEach
  
<c:forTokens
  
<c:import
  
<c:url
  
<c:redirect
  
<c:param

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

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:

e:\tomcat5\webapps\root\demo1.jsp

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

//   demo1.jsp

<%@ page contentType="text/html" %>
<%@ taglib  prefix="c"  
uri="http://java.sun.com/jstl/core"  %>
<html>
<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>
 
</form>

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

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 asp.net style!).( but there is no 'retention of data' , unlike asp.net).

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

<jsp:useBean 
 
id="bean1"  class="ourbeans.player"  >
 
<jsp:setProperty 
  
name="bean1"   property="*"   />
</jsp:useBean>

:( the * sign denotes 'all').

-----

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

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

//  player.java
package ourbeans;

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

 public void setName(String a){
  
name=a;
 }

 public void setPlace(String b){
  
place=b;
 }

 public void setGame(String c){
 
game=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 {bean1.name}. 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="${bean1.name}"   />
<c:out   value="${bean1.place}"  />
<c:out   value="${bean1.game}"   />

---

 We get correct result.

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

//   demo2.jsp

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

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

<jsp:setProperty
name="bean1" property="*"   />

</jsp:useBean>
Name <input   type=text   name="name"><br>
Place<input  type=text  name="place"><br>
Game<input   type=text   name="game"><br>
<input type=submit>
</form>

Name: <c:out value="${bean1.name}"  /><br>
Place: <c:out value="${bean1.place}"  /><br>
Game: <c:out value="${bean1.game}"  />
</body>  
</html>

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

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.

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

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

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

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

</c:if>

</body>  
</html>  

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

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> 
</c:choose>

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Ask Questions?    Discuss: AN INTRODUCTION TO JSTL  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com