JSTL XML Tag out Example

JSTL XML Tag out Example


Posted in : Java Posted on : April 26, 2012 at 7:45 PM Comments : [ 0 ]

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

JSTL XML Tag out Example

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

<x:out> tag of JSTL xml tag library is used for inserting the values directly into the output i.e the into current JspWriter object. This tag puts the result into the output after evaluating the current context node XPath expression.

Attributes of <x:out> tag :

  • select : This is a required attribute which specifies the evaluation of the XPath expression
  • escapeXml : This attribute decides whether the characters like <, >, & etc should be escaped or not. This is an optional attribute and its default value is true. The true value specifies that the conversion of the special characters into their corresponding character entity codes should have been escaped.

Example :

Here the example is given below will demonstrate you about the use of <x:out> tag of JSTL. In this example at first I have created an XML file named employeeDetail.xml that contains some records of Employees like name, age, dob (date_of_birth) of an employee. 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 'emp' 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 'record' using the attribute 'var' and to iterate over all the elements I have used the <x:forEach> tag and stored the value in the variable 'n' using 'var' attribute of this tag then in further statements I have used the <x:out> tag where the 'select' attribute will evaluate the XPath expression and output the result.

employeeDetail.xml

<?xml version="1.0" ?>
<employees> 
<employee>
<name>Noor An Ahmed</name>
<age>33</age>
<dob>12-07-1979</dob>
</employee>
<employee>
<name>Ankit Sharma</name>
<age>25</age>
<dob>05-06-1987</dob>
</employee>
<employee>
<name>Rajesh Sharma</name>
<age>30</age>
<dob>06-05-1982</dob>
</employee>
<employee>
<name>Manish Pandey</name>
<age>33</age>
<dob>07-12-1979</dob>
</employee>
<employee>
<name>Jaydeep</name>
<age>32</age>
<dob>02-08-1980</dob>
</employee>
<employee>
<name>Amit Pandey</name>
<age>27</age>
<dob>06-05-1985</dob>
</employee>
</employees>

JstlXmlOut.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:out Example</title>
</head>
<body>
<c:import url="employeeDetail.xml" var="emp" /> 
<x:parse doc="${emp}" var="record" />
<p>Details of Employees</p>
<table>
<x:forEach var="n" select="$record/employees/employee">
<tr><td align="center">Name : </td> <td><x:out select="$n/name" /></td></tr>
<tr><td align="center"> Age :</td> <td><x:out select="$n/age" /></td></tr> 
<tr><td align="center"> DOB :</td> <td><x:out select="$n/dob" /></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 JstlXmlOut.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 JstlXmlOut.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