Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Servlets and XML: Made for Each Other

By Doug Tidwell
2005-05-18


A very basic servlet

For our first example, the middle step of creating the XML document is not our concern; we simply want to generate a valid XML document. We've hardcoded the document into our source code, shown in Listing 1.

Listing 1. xmlfromscratch.java


publicclassxmlfromscratchextendsHttpServlet
{
publicvoidservice(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsIOException, ServletException
{
response.setContentType("text/xml");
PrintWriterout = response.getWriter();

out.println("");
out.println("");
out.println(" Hello, World!");
out.println("
");
}
}

The result generated by this exciting piece of code looks like this:

Listing 2. Results from xmlfromscratch.java



Hello, World!


You can see an HTML view of the complete listing or view the Java source file directly.



Tutorial Pages:
» Our first sample servlet
» A very basic servlet
» Generating XML fragments
» Interfacing with a database
» Summary
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business