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 javax.servlet.http.*;
import javax.servlet.*;

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

throws ServletException,IOException
PrintWriter out = resp.getWriter();
out.println(“<?xml version=\”1.0\”?>”);
out.println(“john hilton”);

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.

SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

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


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


| 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.



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!

Visit XML Training Material Guide Homepage




“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.