JSTL XML Tag set Example


Posted in : Java Posted on : April 26, 2012 at 7:53 PM Comments : [ 0 ]
Advertisements
In this tutorial you will learn about the JSTL xml set tag.

JSTL XML Tag set Example

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

<x:set> tag of JSTL xml core tag is used to set the value of an XPath expression.

Attributes of JSTL <x:set> tag

  • var : This is required attribute that holds the value of the specified action.
  • select : This is an optional attribute which specifies the evaluation of the XPath expression.
  • scope : This is an optional attribute specifies the scope of the 'var' attribute.

Example :

In the example given below will demonstrate you about the JSTL <x:set> tag. In this example at first I have created an xml file that has to be parsed. Then created a JSP page where  used the <c:import> tag of JSTL core to retrieve the XML document and stores it to the variable 'stu' by using 'var' attribute of this tag, then used the <x:parse> tag to parse the document using the attribute 'doc' and stores the result into the variable named 'doc' using the attribute 'var' and set the value of XPath expression '$doc/students/student' into the variable named 'record' using 'var' attribute then to iterate over all the elements I have used the <x:forEach> tag and stored the value in the variable named 'n' using 'var' attribute of this tag and in further statements I have used the <x:out> tag where the 'select' attribute will evaluate the XPath expression and output the result.

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>

JstlXmlSet.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> 
<!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:set Example</title>
</head>
<body>
<c:import url="studentDetail.xml" var="stu" /> 
<x:parse doc="${stu}" var="doc"/>
<x:set var="record" select="$doc/students/student"/>
<p>Details of Student</p>
<table>
<x:forEach var="n" select="$record">
<tr><td align="center">Name : </td> <td><x:out select="$n/name" /></td></tr>
<tr><td align="center">Roll :</td> <td><x:out select="$n/roll" /></td></tr> 
<tr><td align="center">Grade :</td> <td><x:out select="$n/grade" /></td></tr> 
</x:forEach>
</table>
</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 JstlXmlSet.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 JstlXmlSet.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