Home Jsp JSP Write XML
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JSP Write XML

Advertisement
JSP technology is basically used for building HTML pages with dynamic content but you can use it to generate dynamic content in other formats as well, including XML.

JSP Write XML

        

In this section you will study how to generate dynamic XML using JSP.

JSP technology is basically used for building HTML pages with dynamic content but you can use it to generate dynamic content in other formats as well, including XML. 

Understand with Example

The Tutorial illustrate an elaborate example on JSP Write XML. To understand the example we make use of java bean that include the string type variables. The java bean can make use of these variables by using setter ( ) and getter ( ) method. The setter ( ) and getter ( ) method are used to define the property of class variable.

set xxx( ) : The set xxx ( ) is used to set the values in variable.

get xxx( ) : The get xxx ( ) is used to access the values from the set xxx ( ) method.

You can see in the given example that we have used xml document in a jsp page and therefore we need to  indicate the JSP page compiler by using a JSP page directive tag to set the content type 'text/xml'. The page compiler will use the attributes of the <jsp:useBean> tag to build the Java code to instantiate the bean and populate it with data from the request. The scriplets <% out.print(xml.getId()); %>insert the results into the XML document from the Bean.

Here is the code of GenerateXml.java

package form;
public class GenerateXml{
  private String id = "1";
  private String name = "AAAA";
  private String address="Delhi"
  private String contactNo="9999999999"
  public void setId(String id) {
  this.id = id;
  }
  public String getId() {
  return this.id;
  }
  public void setName(String name) {
  this.name = name;
  }
  public String getName() {
  return this.name;
  }
  public void setAddress(String address) {
  this.address = address;
  }
  public String getAddress() {
  return this.address;
  }
  public void setContactNo(String contactNo) {
  this.contactNo = contactNo;
  }
  public String getContactNo() {
  return this.contactNo;
  }
}

Here is the code of xml.jsp

<?xml version="1.0" encoding="ISO-8859-1"?>
<%@ page contentType="text/xml;charset=ISO-8859-1" %>
<jsp:useBean id="xml" class="form.GenerateXml"/>
<Company>
<Employee>
<Employee_Id><% out.print(xml.getId()); %></Employee_Id>
<Employee_Name><% out.print(xml.getName()); %></Employee_Name>
<Employee_Address><% out.print(xml.getAddress()); %></Employee_Address> 
<ContactNo><% out.print(xml.getContactNo()); %></ContactNo>
</Employee>
</Company>

Output will be displayed as:

Download Source Code:

 

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2009

Ask Questions?    Discuss: JSP Write XML   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
David
March 9, 2012
JSP And Xml

Hi This is David i am getting problem to pass parameters from Jsp to Xml, can u please tell me how to pass parameters from JSP to XML. Thank You.
Ajmal
March 14, 2012
Congratz

Simplest way of doing it right! thanks!!
deva
May 8, 2014
Jsp write xml

sir, i am very new in this topic, can you please describe how to run the given example using netbeans with glass fish server.
DMCA.com