Our first sample servlet
To get things started, we’ll write a 10-line servlet that generates an XML document. As we build our XML-aware servlets, we’ll follow three steps:
- Set the content type to
- Create the XML document.
- Write the XML document back to the client.
In most of our servlets, most of our effort will be in the second step. We might create an XML document from a database query, we might generate it based on an HTTP parameter passed to us from the client, or we might use some other kind of data retrieval or generation method. In our examples here, we’ll focus on HTTP parameters and database queries.
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.
The result generated by this exciting piece of code looks like this:
Generating XML fragments
Well, we’ve created a servlet that is hardcoded to generate a simple, useless XML document. In this next servlet, we build a DOM tree from scratch, then we print some of the DOM tree back to the requestor. The part of the DOM tree that gets sent back to the requestor depends on the HTTP parameters our servlet receives. This example illustrates a couple of useful techniques: using HTTP parameters to control processing and generating a DOM tree without an XML source document.
Listing 3 shows the section of the code that processes the HTTP parameters:
As in our earlier example, we set the content type to
text/xml. Once that’s done, we use the
HttpServletRequest.getParameterNames method to retrieve all the parameters from the HTTP request.
After we’ve processed the parameters, we need to find the information the user requested. The information we’re using builds a DOM tree from objects; that DOM tree contains the text of a Shakespearean sonnet, along with other information about the sonnet. We’ll return a portion of the DOM tree based on the HTTP
subtree parameter. Listing 4 shows some of the code that builds the DOM tree:
We create an instance of the Java class that implements the DOM
Document interface, then we ask that node to create various nodes for us. You could easily rewrite this application to generate the DOM tree by parsing an XML file. To simplify the example (and to lighten my workload) we’ve defined instance variables that hold the values of the nodes we intend to serve. These values are declared at the top of the class declaration, and are initialized in the
Our last step is to deliver the requested part of the DOM tree back to the user. To do this, we use a recursive method,
printDOMTree, that processes a node and all of its children. Because the method is recursive, it doesn’t matter if we start at the root node of the document or anywhere else in the DOM tree. If the request is for one of the nodes we know about, we pass that node to the
printDOMTree method. Otherwise, we pass the
Document node. Listing 5 shows this step.
subtree parameter is author, the results are:
subtree parameter is
title, the results are:
Interfacing with a database
Our final example generates XML from a database query. There are many ways of doing this; for this example, we’ll use IBM XML Extender for DB2 (see Resources). This free product lets you store XML documents in DB2. Our query extracts those documents from DB2, then passes them on to the user.
If you’re using Oracle 8i instead of DB2, you’ll find that it boasts similar functions (see Resources). For databases that aren’t XML-aware, you could store the XML document as a character large object (CLOB) and retrieve the document as a chunk of text.
However your database is set up, you need to do three things to get this code to work:
- First, change the
DbPasswdvariables to the correct values for your system.
// Be sure to change these three strings appropriately, or the //
// servlet won't work. //
DbUserid = "xxxxxxxx";
DbPasswd = "xxxxxxxx";
DbOwner = "xxxxxxxx";
Next, use the JDBC driver that works with your system. We’re using DB2.
staticStringJDBCDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
System.out.println("Can't get the driver!"); e.printStackTrace();
If you want to, change the SQL query. To keep the demo simple, we’re simply retrieving all of the XML documents from the
ordercolumn in the
// We hardcoded the SQL statement here; this would be more
// sophisticated if we qualified the query based on user input.
Stringquery = "select order from " + DbOwner + ".sales_order_view";
service method, our servlet connects to DB2, executes a query (the results of which are a set of XML documents), parses the query results, and writes the parsed data to the output stream. Listing 6 shows the most relevant sections of the code:
Although none of the example servlets will change the world, they do illustrate how well XML and servlets work together. Servlets perform as a great mechanism for delivering content to clients, and XML makes a wonderful mechanism for delivering structured data. You can also use servlets to process XML documents on the server and deliver their contents to clients. Best of all, both of these technologies are cross-platform, giving you more flexibility and portability in your applications.