Combine the power of XPath and JSP tag libraries

Achieve simple data access in JSPs using XPath expressions

1 2 Page 2
Page 2 of 2

At runtime, a tag handler parses and extracts the root of an XPath expression. For example, if the XPath expression is /ORDER/SHIPTO/NAME, the root of the expression is ORDER. The tag handler then looks up a DOM document instance with the name ORDER in page, session, request, and application scopes.

After looking up the DOM document instance, the tag handler evaluates the XPath expression against it and the result is substituted.

These semantics are not exposed to the JSP page author. They are hidden in the custom tag library implementation.

The XPath-JSP Test Application

I've included an XPath-JSP test application so that you can get some hands-on experience and validate the concepts we discussed in the previous sections.

The test application shows how to create DOM document objects using the gencontent tag in a JSP. It also shows how to access the DOM document instance from presentation JSPs using the various XPath custom tags we covered earlier.

You will also see how an XPath expression in a JSP is parsed and mapped to a DOM document object in memory.

Install the XPath-JSP Test Application

Before downloading and installing the test application, make sure you have the following software packages:

  • Java Development Kit

    You must download and install a Java 2 (version 1.2 or later) JDK implementation for your operating system platform. See


    for download information.

  • Servlet container

    You must download and install a servlet container that is compatible with the Servlet API Specification (version 2.2 or later) and the JSP Specification (version 1.1 or later). One popular choice: download Apache's Tomcat (version 3.1 or later). Note: the XPath-JSP test application has been tested on Apache Tomcat version 3.1 only.

After you install the aforementioned software packages, download the XPath-JSP Test Application, contained in a Web application archive (WAR) file. (The Zip file contains the required WAR file.) You can install it on any servlet container compatible with Servlet API 2.2 or later versions.

Now, here's some legal language to keep everyone happy.

IMPORTANT: This product includes software developed by the Apache Software Foundation (

IMPORTANT: This product includes software developed by OpenXML Inc.

On deploying the xpath-jsp.war file, the directory structure illustrated in Figure 2 results.

Figure 2. The xpath-jsp.war directory structure

CLASSPATH settings

The XPath-JSP WAR file contains the Apache XML parser xerces.jar. For the test application to work properly it might be necessary for the xerces.jar to appear before any other XML parser in the CLASSPATH.

For Tomcat 3.1, make sure the xerces.jar appears before the default parser xml.jar in the CLASSPATH.

Use the XPath-JSP Test Application

To use the XPath-JSP Test Application, follow these steps:

  1. Go to the xpath-jsp/test-DOMs directory and open the dom.jsp file.
  2. The content of this file has some valid XML content enclosed within a <gencontent/> tag. When this JSP executes, it creates a DOM representation of the XML content.

    The DOM document instance is stored in session scope. The "key" or "name" used to store the instance is the document element name of the XML content. In this case, the document element is Order.

  3. Go to xpath-jsp directory and open the viewOrder.jsp and the editShipTo.jsp file.
  4. Note the XPath expressions that will be applied to the DOM document created using the dom.jsp.
  5. To test, access the dom.jsp from your browser. Then access the viewOrder.jsp and the editShipTo.jsp files.

IMPORTANT: The code for this application is still in experimental stage and should not be used for production applications. The code has not been rigorously tested.

That's all there is to it.


No solution exists without tradeoffs. With that in mind, let's quickly look at the pros and cons of combining XPath expressions in a JSP.

The XPath-JSP tag custom tag library offers a unique solution to making presentation code simpler and Java-free. It forces separation of presentation and content by providing a small set of tags that enable the use of XPath expressions in JSPs to access DOM documents. Though this requires learning of XPath syntax, the basic syntax is easy to pick up. The DOM documents are fully processed in the backend, which keeps the presentation layer clean and simple.

Performance is potentially slower due to overhead of converting backend data into DOM, then accessing it via XPath. However, it is unclear exactly how much of a penalty may result, under what circumstances, and if it is noticeable in real-world scenarios.


  • A standard data access and substitution syntax using XPath
  • Clear separation between the role of page author and developer
  • Page author: expert in UI and presentation, uses XPath Tag Libraries
  • Developer: expert in server-side logic, creates and uses tag libraries and DOM documents
  • XPath provides a simple yet powerful traversal syntax


  • Representing domain objects as DOM documents incurs a performance overhead


After reading this tutorial, you should have a good idea of how to build an XPath-JSP application. You should also have some idea of its limitations. We've seen how to use the simple yet powerful XPath data access syntax in JSPs with the help of a custom tag library. In addition, we've examined a couple of approaches to represent domain data as DOM document objects. Further, we've seen how the custom tag library maps XPath expressions to DOM document objects at runtime. Finally, we've looked at the pros and cons of using this approach and how it helps in writing clean and simple JSPs.

I am confident you will have a greater appreciation for this approach after using the test application and the examples. The XPath custom tag library covered in this article is still at a proof-of-concept stage and has a lot of scope for improvements and enhancements. In fact, I've been thinking of how to extend this tag library into a Web application framework that will make it easier to develop and deploy a Web application using the technologies and techniques we have discussed.

Thanks to my colleague Andy Hakim for his comments on drafts of this article and a lot of very helpful feedback on the XPath custom tag library.

Stanley Santiago is a senior software engineer at iPlanet Ecommerce Solutions, a Sun/Netscape Alliance. He has more than eight years of experience working on object-oriented development projects and Internet-based applications using Java and XML.

Learn more about this topic

  • JavaWorld resources
  • Other important resources

1 2 Page 2
Page 2 of 2