• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Core Tags in JSTL

May 22, 2011 //  by Krishna Srinivasan//  Leave a Comment

[ 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 simpleexamples.]

We are now ready toexperiment 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
Bergstentitled,"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 asthe server threwexception.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:iftest= “

<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.jspuses<c:set&
<c:outtags.

:We create demo1.jspas:

e:tomcat5webappsrootdemo1.jsp

—————————————–

//demo1.jsp

<%@
page contentType="text/html" %>

<%@
taglibprefix="c"uri="http://java.sun.com/jstl/core"%>

<html>

<bodybgcolor=lightblue>

<formmethod=postaction="demo1.jsp">

NAME <inputtype=textname="text1"><br>

PLACE<inputtype=textname="text2"><br>

<input type=submit>

</form>

NAME:<c:outvalue="${param.text1}"/><br>

PLACE:<c:outvalue="${param.text2}"/>

</body>

</html>

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

We start
Tomcat5, and type the urlas :

‘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 thevalues entered by
the user.due to the JSTL tags

<c:outvalue=”${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

packageourbeans;

public class player

{

Stringname;

Stringplace;

Stringgame;

public player()

{

name="";

place="
";

game="
";

}

//—————————

public voidsetName(String a)

{name=a; }

public void setPlace(String b)

{place=b; }

public void setGame(String c)

{game=c;}

//——————————

public StringgetName()

{ returnname; }

public StringgetPlace()

{ returnplace; }

public StringgetGame()

{ returngame; }

}

———————————

In demo2.jsp, we collect the data and then displaythe 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:outvalue="${bean1.name}"/>

<c:outvalue="${bean1.place}"/>

<c:outvalue="${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=postaction="demo2.jsp">

<jsp:useBean id="bean1"
class="ourbeans.player">

<jsp:setPropertyname="bean1"
property="*"
/>

</jsp:useBean>

Name
<inputtype=textname="name"><br>

Place<inputtype=textname="place"><br>

Game<inputtype=textname="game"><br>

<input
type=submit>

</form>

Name:
<c:outvalue="${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:iftag
.

———————————————-

//demo3.jsp

<%@
page contentType="text/html" %>

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

<html>

<bodybgcolor=lightblue>

<formmethod=postaction=demo3.jsp>

<selectname="combo1">

<optionvalue="sam">sam

<optionvalue="tom">tom

</select>

<inputtype=submit>

</form>

<c:setvar="s"value="${param.combo1}"/>

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

<br>

<c:iftest="${seq'sam'}">

<c:outvalue="Good
Morning…SAM!"/>

</c:if>

<c:iftest="${s=
=
'tom'}">

<c:outvalue=" 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
Americanauthors say!But
, if we are carelessin 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 quotein the 'test expression'. Second point worth noting in the above
example is that we can use either ==( double equal to) or eqto test equality.

***********************************************

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

The
syntax is:

<c:choose >

<c:whentest="">

<c:otherwise>something</c:otherwise>

</c:choose>

The
peculiarity to be noted here is thatunlike <c:if ,
where we had to explicitlyuse <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!

———————————————–

//demo4.jsp

<%@
page contentType="text/html" %>

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

<html>

<bodybgcolor=lightblue>

<formmethod=postaction="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>

<inputtype=submit>

<c:setvar="s"value="${param.combo1}"/>

Today
is

<br>

<font
size=24 color=red>

<c:choose>

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

<c:whentest="${s==2}">Monday</c:when>

<c:whentest="${s==3}">Tuesday</c:when>

<c:whentest="${s==4}">Wednesday</c:when>

<c:whentest="${s==5}">Thursday</c:when>

<p class="MsoN

Category: Java EETag: JSTL

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Content Types and Storage
Next Post: Cache Management with Windows Azure »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact