XML and servlets
Servlets have become a credible and efficient mechanism for developing Web-based applications. Aside from the fact that servlets are written in Java (and therefore have all the benefits of Java), they also have distinct performance and flexibility advantages over alternatives such as ASP and Perl. Servlets, however, suffer from a problem that is shared among Web applications and that is: lack of a clear separation between data and presentation. If your code contains lines like this, then you know the issue:
writer.println("This is great!
Various schemes have been developed (for servlets as well as other languages) to make this separation distinct. For example, some have chosen to "objectify" HTML components. Instead of writing out the HTML code to create a table with its cells filled with data variables, an object representing an HTML table is created and its properties are set. Then a single method call will translate that object into HTML code that can be sent to a browser. This approach is nice and at least eliminates all of the "writer.println" lines in your, code, thus making it easier to read. The problem I usually encounter with the above approach is that the classes do not fully represent the HTML components and I often end up extending the class definitions to include the features I need. Another issue is that, although the presentation of the data is now contained within these classes, it is still mixed with the rest of the program.
XML in the mixXML certainly did not have its roots in the world of servlets, but it has certain qualities that make it a nice fit for servlet-based applications. XML and its related standards provide a standard way for representing data and interacting with it. For example, an invoice document can be nicely represented using XML-based tags. The tags only mark up the data without consideration as to how the data will actually be displayed. This distinction eliminates all of the "writer.println" lines. An API such as the Document Object Model (DOM) can be used by the servlet to read, write, and change the data contained in the XML document. Again, the API does its operations without regard to the presentation of data.
XML provides an effective means to represent data and thus enhances data exchange.
Your servlets' interaction with XML can be wrapped in special utility classes. Such classes are useful because they hide the details of low-level XML interaction from your program. In the servlet you merely instantiate and manipulate business objects. These objects could come from databases, XML documents, or various other sources. Suppose your application is an online test. The test is composed of questions and each question can be represented by an XML document. By putting all the questions in a directory, your application will be able to dynamically read each question, display it, and receive user's response. Replacing a question is just a matter of replacing a file (which most users know how to do).
Outputting XMLSo far we have focused on using XML as an input data source for your servlets. You gain flexibility and the ability to interact with a universally accepted form of representing data. There are some advantages when the output of your program is also XML. Again, by outputting XML, your servlet will not have to deal with presentation details. Unlike HTML where data and tags for presenting the data on a browser are mixed, with XML you merely output data (marked up with tags that identify the data). Again, it is recommended that you use some utility classes to wrap the details of XML operations from your servlets.
Keep in mind that both XML and XSL are relatively new.
Another advantage of outputting XML is when the data will be used by another application (or servlet). If you have a servlet that integrates with several data sources, then other applications could use the servlet to interact with the various data sources. This is much simpler than rewriting all the connectivity pieces for the new application. XML provides an effective means to represent data and thus enhances data exchange. Using this scheme, it is possible to create powerful servlets that get their data from other servlet-generated XML documents, thus integrating various applications and providing a consistent user interface to all of them.
Presentation detailsIn an ideal world, you would use XSL (Extensible Stylesheet Language) along with XML to include formatting and presentation details. Using this scheme, the data is represented by XML. There is an associated XSL document that contains formatting information for the data. If some aspect of presentation needs to be changed, then you only need to change the XSL document. This also means that you have to have a browser that can understand both XML and XSL. Currently IE 5.0 supports both, but the XSL standard is still incomplete. You also need to have an XSL parser in addition to the XML parser. One such parser is LotusXSL. This processor works with the XML parser from IBM but can be modified to work with other parsers as well. There is even support for transforming the XML/XSL combination into HTML for browsers that are "XML-challenged."
If you plan on exploring XML/XSL/servlets you should download the following software:
- a servlet development/deployment environment such as the JSDK from Sun
- a browser that is capable of understanding XML/XSL, unless you output HTML
Keep in mind that both XML and XSL are relatively new. They offer a framework for creating Web applications and emphasize separation of data and presentation details. Despite their youth, several sites use engines that are based on XML and servlets. The JavaLobby site is a good example.
ConclusionData and presentation of data are two separate things, and they should be kept separate as much as possible. Aside from offering flexibility, the code becomes more readable and user-interface changes (colors, fonts, etc.) become much easier to perform. By using servlets along with XML/XSL, a framework can be created that clearly separates data and its presentation. Although the idea that data and presentation should be separated is not new, the specific implementation using XML and XSL is. You shouldn't be discouraged by the maturity of these technologies. It is now a good time to familiarize yourself with the parsers and the processor mentioned in this article.
Hopefully, you'll use XML and servlets in your next killer site and appreciate the flexibility that you gain.
About the authorPiroz Mohseni is president of Bita Technologies, focusing on business improvement through the effective use of technology. His areas of interest include enterprise Java, XML, and e-commerce applications.