In this tutorial you will learn about the JSTL xml tag choose.
JSTL XML Tag Choose
In this tutorial you will learn about the JSTL xml tag choose.
<x:choose> tag in JSTL xml library is used for executing the conditional statements.
Attributes of <x:choose> tag
This tag does not contain any attribute.
Example :
An example is being given below will demonstrate you about the JSTL xml <x:choose> tag. In this example at first I have created an xml file which will be parsed and read in the JSP page. Then I have created a JSP page to use this tag. On the JSP page I have first import the xml file named studentDetail.xml and saved the url into a variable named 'stu' using attribute var. In the next line I have used the <x:parse> tag to parse the xml document using 'doc' attribute and saved the result into the variable named 'doc'. Then used the tag <x:forEach> to iterate over the elements and saved the result into a variable named 'n' using var attribute. In the next line used the <x:choose> and <x:when> to execute the conditional statements.
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>
JstlXmlChoose.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:choose Example</title> </head> <body> <c:import url="studentDetail.xml" var="stu"/> <x:parse doc="${stu}" var="doc" /> <x:forEach var="n" select="$doc/students/student" > <x:choose> <x:when select="$n[roll <= '47']" > <p>Name : <x:out select="$n/name"/></p> <p>Roll : <x:out select="$n/roll"/></p> <p>Grade : <x:out select="$n/grade"/></p> </x:when> </x:choose> </x:forEach> </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 JstlXmlChoose.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 JstlXmlChoose.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 :
[ 0 ] Comments