Generating XML Document Using Java servlet

Java servlet is a java program which runs on web server for the purpose of generating response to the request submitted by browser.

You can use the power of this J2EE component to generate an XML document. You will understand this better with the help of following example:


_______________________________________________

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class XmlTemplate extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException,IOException
{
resp.setContentType("text/xml");
PrintWriter out = resp.getWriter();
out.println(<?xml version=\1.0\?>);
out.println(<persondata>);
out.println(<name>);
out.println(john hilton);
out.println(</name>);
out.println(<age>);
out.println(45);
out.println(</age>);
out.println(<occupation>);
out.println(manager);
out.println(</occupation>);
out.println(</persondata>);
}
}

As per the basic working technology of a servlet, in order to invoke it web server calls one of the following methods depending upon the requirement: doGet or doPost. After the servlet is invoked it does three basic tasks.

Firstly setContentType() method is called and appropriate information is passed about the type of content which going to be generated by servlet. It should be noted that setContentType() is a method of HttpServletResponse object which is called resp here in above example.

Then the same object HttpServletResponse is used here to create the object called PrintWriter named out in the example. This is done by using getWriter() method.
This is the method used to write text responses. Before using this method you should ensure that content type is set already as you have done here by passing text/xml string to the setContentType() method.

Finally XML tags are passed in the form of string using println() method of servlets.



_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________

Recommended XML Books

cover
cover
cover
cover
cover
cover


| Generating XML Document Using Java servlet | How to Design a XML Document Easily | Understanding Advanced XML Concepts | Understanding Class Loaders in Java | Understanding The Basic Reasons for Development of XML | Understanding Various Methods of Formatting XML Data | Understanding XML Based Web Protocols | Working With ebXML An Indepth Overview | XML Database Program Knowing the Basic Concepts |

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!


New XML Books Titles - Best Seller's @ Amazon.com

Visit XML Training Material Guide Homepage