Servlet How To

How To Use the Plotting Widget in a Servlet

Note: July 26 2000 - The examples in this tutorial have been updated to fix a number of bugs which occurred with newer versions of the JDK. The new implementations should be more robust across different platforms and JDK's. They have been tested on windows NT using JDK 1.3 and the JRun servlet engine with IIS, and under Linux using JDK 1.1.8, 1.2.2 and 1.3 with Apache jserv.

Introduction to Servlets

Servlets allow Java code to be run inside a web server to provide much the same functionality as CGI scripts. Servlets are now supported by most web servers including Apache, Netscape, Microsoft IIS and others. 

Using Java servlets instead of CGI programs generally results in more efficient and maintainable code. Java servlets are more efficient because a new process does not have to be forked off for each request. Since Java servlets can be written in a clean OO language they are often mode maintainable than complex Perl scripts.

Java servlets also offer a number of advantages over Java applets. Java applets require the Java code to be run in the user's browser, and suffer from complicated compatibility problems due to differences between browsers and possibly slow startup due to the the need to download many compiled classes into the browser. Java servlets, since they run in the server, will work with any browser and do not result in any code being downloaded to the user's machine.

Using the JAS Plotting Widget in a Servlet

The JAS plotting widget can be used as part of a Java servlet or as part of a Java applet. Which you choose to use will depend on your particular application. The table below summarizes some of the pro's and con's.

For more information on using JAS with an applet, see the JAS Applet Howto. The remainder of this document will describe how to use JAS in a servlet.

Getting Started

The first thing to do is to make sure your web server has been configured to run Java servlets and that you know how to invoke a simple servlet such as the HelloWorldServlet supplied with most servlet implementations. 

Once you have done this then the next step is to try to run the following servlet which demonstrates how to create a GIF image using the GIFEncoder class supplied as part of the acme.jar file downloadable from www.acme.com.

To run this servlet you will need to configure your servlet engine to have access to the acme.jar by adding in to the engines CLASSPATH. If all is well then accessing the servlet from your web browser should produce something like this:

Note: If your web server is running on Unix, you may at first experience some problems getting this servlet to work. Due to a limitation in the way Java graphics work, the servlet will only work if it is able to access an X display. This is because even though it does not display anything on the screen, it will build the image in an off-screen buffer and use the X server's fonts to render the button text. If you cannot or do not want to give you servlet engine access to an X display, then an alternative is to use the XVFB server which is available for Linux and other Unix platforms. In future it may be possible to work around this limitations by using the font handling and in-memory image facilities of JDK 1.2. Servers running on Microsoft windows should not suffer from this problem.

Once you have this working, the next thing to try is to use a simple Servlet which generates an image obtained from a Swing component. The following example illustrates this:

If your servlet engine is using a version of Java earlier than JDK 1.2 the swingall.jar file distributed as part of swing 1.1.1fcs for JDK 1.1 will need to be added to your servlet engine's CLASSPATH. If all is well then when you access the servlet using your web browser you should see something like this:

Building a Simple Histogram Servlet

Once you have got the simple swing servlet running, you can try this simple Histogram servlet. If you are not already familiar with how JASHist - the JAS Plotting Widget, works then you may want to first read the appendix of the JAS Users' Guide dealing with that subject. This servlet creates a JASHist object and connects it to a MemoryDataSource, resulting in a plot showing the Java memory used by the servlet engine since the first time this servlet was invoked.

Note that this servlet extends the SwingServlet used in the previous example, so you will need to have installed that servlet before this new servlet will work. You will also need to add the jas.jar and hep.jar files from the JAS distribution to your servlet engines CLASSPATH. All being well you will see something like this when you access the servlet from your web browser:

Another Simple Histogram Servlet

This example is a little more useful, it reads the data to be plotted from the query string (the part of the URL following the ?). For example the following query string would produce the plot below.

title=Test%20Histogram&min=0&max=10&data=0,1,2,3,4,5,6,5,4,3,2,0&transparent=yes

Here is the source code for this example. Note how QueryHistogram servlet overrides SwingServlet's doGet method and attaches a new data source before calling SwingServlet's doGet method. The hard work is done by the inner class QueryDataSource which parses the query string and implements Rebinnable1DHistogramData.

Using XML to Specify the Contents of the Plot

Since the Plot bean can read XML files describing plots it is easy to create a servlet which gets its data any style attributes from an XML file. The following example shows how to do this.

A example plot produced by the above servlet is shown below. For more information on using XML to specify the contents of plots see the JAS-XML How To.

Using the Histograming Classes in your Servlet

This next example shows how to use the Histogram classes distributed in the hep.jar file in your servlet. This example simply creates and fills a histogram with random Gaussian distributed random numbers and then displays the resulting histogram using the JASHist plotting widget. Although the jas.hist package and the hep.analysis package are completely independent of one another, there is a jas.hep package which contains a single public class PartitionAdapter which can be used to connect a histogram to a plot.

When run this servlet will produce the following output:

A More Complex Example

The final example is the servlet used to create the download statistics on the JAS Home Page. This servlet illustrates how to connect to a database and create histograms from the data contained within. There are numerous ways to use the JASHist class within a servlet, hopefully this Howto has given you enough information to get started. Please use the feedback form at the bottom of this document to send us and you fellow readers your comments or suggestions.


Page last updated: Wednesday, January 14, 2004 by SLAC\tonyj