Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Display image on JSP page using XML 
 

In this tutorial we are going to know how we can display a image on JSP page by using XML.

 

Display image on JSP page using XML

                         

In this tutorial we are going to know how we can display a image on JSP page by using XML. 

This example will examine how to parse and expose XML information using the JAXP with a JSP page. This tutorial is only geared towards showing how to construct a Java object from an XML document. For this what we need  a XML file in which the images are stored. What we need is to extract the image file from the XML file. 

To make a program over it we have used the following page directive attributes, the first attribute we have used is contentType and the second attribute is import. We need to import some classes and packages which we have described in the tutorial. 

Inside the scriptlet firstly we will define a factory API which allows our application to obtain a Java XML  parser. DocumentBuilderFactory defines a factory API. Now create a DocumentBuilder object to parse an org.w3c.dom.Document from XML. Save the xml file at bin file of  C:\apache-tomat-5.5.23\bin\. Now we need to parse method to actually parse the XML file to create our own Document object. Document object will be created by parse method of the DocumentBuilder

From the Document object we will get a NodeList object represents all of the elements in our XML document name message. NodeList object will be obtained by the getElementByTagName(). 

A NodeList is an array starting from 0 and going up to the length of the array by calling getFirstChild() method. By using this method the desired text node is returned and we can use getNodeValue() for our message. In this way we can retrieve the image from the XML file. 

Code of the imageXML.xml file:

<?xml version="1.0" encoding="UTF-8"?>
 <images>
 <pic1>1.jpg</pic1>
 <pic2>2.jpg</pic2>
 <pic3>3.jpg</pic3>
 <pic4>4.jpg</pic4>
 <pic5>5.jpg</pic5>
 <pic6>6.jpg</pic6>
 <pic7>7.jpg</pic7>
 <pic8>8.jpg</pic8>
 <pic9>9.jpg</pic9>
 </images>

Code of the imageUsingXML.jsp file.

<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"
%>
<html>
<body><center><table border="2">
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("\\imageXML.XML");
NodeList pic1= doc.getElementsByTagName("pic1");
NodeList pic2= doc.getElementsByTagName("pic2");
NodeList pic3= doc.getElementsByTagName("pic3");
NodeList pic4= doc.getElementsByTagName("pic4");
NodeList pic5= doc.getElementsByTagName("pic5");
NodeList pic6= doc.getElementsByTagName("pic6");
NodeList pic7= doc.getElementsByTagName("pic7");
NodeList pic8= doc.getElementsByTagName("pic8");
NodeList pic9= doc.getElementsByTagName("pic9");     
%>
 <%//A NodeList as an array starting from 0 and going up 
to the lentgh of the array  calling getFirstChild() 
the desired text node is returned and 
we can use getNodeValue() for our message.%>
 <tr><td>
 <img  border="2" src=<%= pic1.item(0).
getFirstChild().getNodeValue() %> 
width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic2.item(0).
getFirstChild().getNodeValue() %>
 width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic3.item(0).
getFirstChild().getNodeValue() %>
width="137"  height="140"></td></tr>
 <tr><td>
 <img  border="2" src=<%= pic4.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic5.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic6.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td></tr>
 <tr><td>
 <img  border="2" src=<%= pic7.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic8.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td><td>
 <img  border="2" src=<%= pic9.item(0).getFirstChild().getNodeValue()
 %> width="137"  height="140"></td></tr>
 </table>
 </center>
</body>
</html>

The output of the program is given below:

Download imageUsingXML.jsp file.

Download imageXML.xml file.

                         

» View all related tutorials
Related Tags: c com orm table ant data join select form make tables tab for to between e ul result pe from

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:


Hi, I am trying to display image on JSP by using XML. I am very glad to see this useful web page. Unfortunately, I have tried to code above but it seems doesn't work properly. An error is in line

"Document doc = db.parse("\\imageXML.XML"); "

I copy xml file into bin directory but the error still occur. Can you help me to sort this out, please.

Hopefully, somebody can help. Thanks

Jeffrey

Posted by Jeffrey on Sunday, 03.23.08 @ 05:11am | #53864

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.