Sun Java Solaris Communities My SDN Account Join SDN
 
FAQ

Questions and Answers - Client Tier

 
 


Guidelines, Patterns, and code for end-to-end Java applications.

Questions and Answers - Client Tier
  1. How should my applets talk to my enterprise beans ?

  2. Can enterprise bean data be imported directly into a StarOffice spreadsheet?

  3. Is there a way to import data from EJBs directly into an Excel spreadsheet?

1.  How should my applets talk to my enterprise beans ?

We recommend making services from EJB technology-based components ("enterprise bean services") available to applets through a web-tier servlet. The servlet acts as a facade that unifies the applet's access to back-end data, and isolates the applet code from changes in the enterprise bean classes.

Applet accesses enterprise beans through servlet

It's especially useful for the servlet to serve XML to the applet, if the applet has need of data that is structured. If you choose to serve XML, be sure that your applet uses a small-footprint XML parsing mechanism, such as a lightweight SAX implementation.

Placing a servlet between the applet and the enterprise beans the applet uses is good since:
  • You may don't have control over the Java Runtime Environment which is running the applet, so you don't know if you can access enterprise beans at all. Any version of the JRE can handle a simple XML parser, which is all that is needed to provide services to the applet.
  • Clients behind firewalls may not be able to make RMI-IIOP connections, so they can't access the enterprise beans directly. The servlet can serve any format deliverable over HTTP through the firewall.
  • Exposing the EJB tier to the outside world is a security risk. The servlet acts as a single point of control for what enterprise bean functions are externally available.
  • The servlet can decouple applets (and other clients) from the EJB tier, allowing their implementations to vary independently. This ability may be less important for applets, which are typically loaded at runtime, but custom clients that require installation can benefit from this decoupling. The web tier implementation can be changed as needed changed to correspond to changes in the entity beans' APIs, while maintaining the original contract with the installed base of clients. (This is a general benefit of the Facade design pattern.)

Using a web-tier component as a facade is a powerful design pattern for making enterprise bean functionality available to medium- and heavyweight clients without exposing the EJB tier. For example, the servlet described here could also make enterprise bean data accessible to a spreadsheet, as described below. This pattern is similar to the Session Facade design pattern, where a web-tier stateless session bean is used as a facade for access to entity beans.

2.  Can enterprise bean data be imported directly into a StarOfficeTM spreadsheet?

StarOfficeTM spreadsheets cannot directly communicate with enterprise beans, but a servlet or page created with JavaServer PagesTM technology (JSP page) in the web tier can access services in the EJB tier and present the results as XML, which the StarOffice spreadsheet can then import.

It's best that communication between J2EE technology-enabled servers and spreadsheets be loosely-coupled. While many spreadsheets incorporate full-fledged application development languages which could, in theory, access enterprise beans directly, such a solution would probably be complicated and brittle.

XML makes it easy to present EJB services on the web to non-J2EE clients. Both StarOffice and Excel provide an easy way to transport data over HTTP, and XML manipulation facilities are available from within both applications. When interacting with XML-aware, non-J2EE technology-enabled clients, we recommend using a servlet to expose selected enterprise bean services as XML data sources, and using the client's HTTP transport and XML processing facilities.

The Java Pet Store sample application provides an example of this technique. All pending orders in the Java Pet Store are available in XML format from the web page estore/pendingorders.jsp. This page produces an XML description of the current pending orders. StarOffice accesses the abovementioned web page, receives the data in XML format, and displays the data in the spreadsheet. See the document docs/starofficeDemo.html in the Java Pet Store install directory for a description of how to access the Java Pet Store from StarOffice.

This technique is not limited to spreadsheets. Any client capable of making HTTP requests and using the XML returned from the servlet can access the Java Pet Store in this way. In fact, this is an identical solution to the one described in the discussion on applets and enterprise beans above, except that a spreadsheet is accessing the XML data source, instead of an applet doing so.

3.  Is there a way to import data from EJBs directly into an Excel spreadsheet?

Excel can access the Java Pet Store just as can StarOffice. See the document docs/excelDemo.html in the Java Pet Store install directory for a description of how to access the Java Pet Store from Excel.

See also:

Session state in the Client tier