JSTL XML Tag otherwise

JSTL XML Tag otherwise


Posted in : Java Posted on : April 26, 2012 at 8:03 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL xml tag otherwise.

JSTL XML Tag otherwise

In this tutorial you will learn about the JSTL xml tag otherwise.

<x:otherwise> tag in JSTL xml library is a subtag of <x:choose> which may used with <x:when> tag and is executed if the conditional statement of <x:when> returns the boolean value false. An if-then-else clause replacement in JSTL xml library is the tags given below :

       <x:choose>
        <x:when> </x:when>
        <x:otherwise> </x:otherwise>
       </x:choose>

Attributes of <x:otherwise> tag

This tag does not contain any attribute.

Example :

An example is being given below will demonstrate you about how to use the JSTL <x:otherwise> tag and how it is evaluated. In this example at first I have created an xml file named studentDetail.xml then created a JSP file to parse the data of xml file and read them. On the JSP page imported the xml file then parsed it using the <x:parse> tag using the attribute 'doc' and saved it into a variable named 'doc' then used the <x:choose> action to select one of the possible alternatives. This action contained the <x:when> and the element <x:otherwise> an optional follower element of <x:when> tag.

studentDetail.xml

<?xml version="1.0" ?>
<students> 
<student>
<name>Amit</name>
<roll>33</roll>
<grade>A</grade>
</student>
<student>
<name>Mukesh</name>
<roll>47</roll>
<grade>B</grade>
</student>
<student>
<name>Manoj</name>
<roll>24</roll>
<grade>B</grade>
</student>
<student>
<name>Rajnish</name>
<roll>84</roll>
<grade>C</grade>
</student>
</students>

JstlXmlOtherwise.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL x:otherwise Example</title>
</head>
<body>
<c:import url="studentDetail.xml" var="stu"/>
<x:parse doc="${stu}" var="doc" />
<x:choose>
<x:when select="$doc/students/student[grade = 'D']" >
<p>You have gotten the 65% marks</p>
</x:when>
<x:otherwise>
<p>You have gotten either below 65% or higher than 65%</p>
</x:otherwise>
</x:choose>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar
  • xalan-2.3.1.jar
  • xercesImpl-2.7.1.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlXmlOtherwise.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlXmlOtherwise.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When you will execute the above JSP page you will get the output on your eclipse browser as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics