Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Servlets and XML: Made for each other

Return to article


Code to generate an XML document from scratch
//---------------------------------------------------------------------------||
// xmlfromscratch.java                                                       ||
//                                                                           ||
// This code generates an XML document from scratch.                         ||
//---------------------------------------------------------------------------||
// Written 17 February 2000 by Doug Tidwell.                                 ||
//---------------------------------------------------------------------------||
//                                                                           ||
// (C) Copyright IBM Corp. 1999  All rights reserved.                        ||
//                                                                           ||
// US Government Users Restricted Rights Use, duplication or                 ||
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.         ||
//                                                                           ||
// The program is provided "as is" without any warranty express or           ||
// implied, including the warranty of non-infringement and the implied       ||
// warranties of merchantibility and fitness for a particular purpose.       ||
// IBM will not be liable for any damages suffered by you as a result        ||
// of using the Program. In no event will IBM be liable for any              ||
// special, indirect or consequential damages or lost profits even if        ||
// IBM has been advised of the possibility of their occurrence. IBM          ||
// will not be liable for any third party claims against you.                ||
//                                                                           ||
//---------------------------------------------------------------------------||

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

public class xmlfromscratch extends HttpServlet 
{
  public void service(HttpServletRequest request,
                      HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    
    out.println("<?xml version=\"1.0\"?>");
    out.println("<greeting language=\"en_US\">");
    out.println("  Hello, World!");
    out.println("</greeting>");
  }
}


Return to article