Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

A J2EE presentation pattern: Applets with servlets and XML

Enhance your Web interfaces with powerful XML-configured applet components

In the J2EE (Java 2 Platform, Enterprise Edition) architecture's presentation tier, servlets and JSPs (JavaServer Pages) generally render data derived from the business logic tier as HTML for browser presentation. Implementing your application in HTML offers many advantages: HTML is easy to write, very lightweight, and looks aesthetic.

However, sometimes HTML can be limiting when you require a more complex GUI (graphical user interface). For example, the <table> tag is simple (yet powerful), but it doesn't have scrolling, selection, and editing capabilities. What happens if you need a table that behaves more like a spreadsheet, where the user scrolls through the results and carries out complex cell selections? In addition, what if you need a hierarchical tree display. No HTML tag displays a tree structure that lets users expand and contract, drag and drop, or rename and delete nodes; something you might expect in a mailbox display that mimics a desktop mailbox.

You can implement some table and tree behaviors using HTML and DHTML. For example, you can implement simple selection in tables using checkboxes and cell background color. However, DHTML is difficult to write and maintain and has notorious browser deployment problems. Conversely, due to its rich object-oriented API, Java is well suited to this complex GUI behavior in the form of browser-embedded applet components. But what exactly is the relationship between applets and J2EE, and how can you integrate applets into a J2EE architecture?

In The J2EE Tutorial, Sun Microsystems briefly mentions that one client option is to embed applets in Webpages. However, the tutorial is unclear on how to integrate an applet into a J2EE system; instead, it concentrates on Web components (servlets and JSPs). This is due to the simplicity of those technologies; no plug-ins are required. Sun also mentions that you can use JSPs for outputting XML documents, which is generally useful for producing data in a standardized computer-readable format for Web service consumption.

However, in this article, I present a valuable J2EE pattern to embed presentation applets in your standard JSP-generated HTML, which consumes XML as a source of display data that your Web components also generate. This avoids your applet components having to make RMI/IIOP (Remote Method Invocation over Internet Inter-ORB Protocol) calls to retrieve data from EJBs (Enterprise JavaBeans) or other J2EE components that introduce complicated firewall issues. It gives you the best of both worlds from Web clients and Java applications: Flashy HTML that contains components with potentially complex behavior configured by simply formatted data.

Presentation data

So how do you do it? First, let's consider the concept of presentation data. Presentation data is data derived from your business logic tier (and ultimately your data tier) that has been processed and simplified to essentially configure a viewer. Presentation data comprises data that will appear in the GUI component and any extra data that describes the structure and state of that GUI component. For example, think of a hierarchical tree viewer; here is the data required to represent an instance of such a tree:

Resources