ContentsApplication DesignUsage scenarios
Previous: Execution cycle | Next: Who does what - logical delegation

Usage scenarios

Some of the data for our application comes from the user, and some from an external data source. The external data source that I've chosen to use is the Consumer Price Index (CPI) from the US Bureau of Labor Statistics. This seems like a reasonable source of this data, but I'm not an economist - so there is probably something more appropriate. For the intent of this tutorial, though, let's assume that this is the data that we want.

We could go after the CPI data on demand from our user. In order to do that, we would launch an HTTP request to the BLS server and parse the data that it sends back. The parsed data would be stored in a value array and then appended to the chart along with the user data.

There really is no reason to trouble the BLS server for every request that our user makes. We could do it, but the network traffic from our server to the BLS server isn't warranted. Furthermore, the BLS server may be busy, and the latency would appear as a performance problem for our user. Instead, we can cache the CPI data and reuse it for each chart the user wants to make. In fact, we can cache the data and reuse if for each user. In fact, the data only changes once a year, so it can be downloaded and stored so that we don't need to launch the HTTP request programmatically. Since the BLS may change various characteristics of their web site over a year, it isn't worth the effort of writing that feature into the servlet.

To a lesser degree, the same consideration should be made about accessing ANY remote data. While it may be (and often is) possible to get refreshed data frequently, you should think about the burden that it adds to the infrastructure. Use dynamic data acquisition sparingly - cache everything you can for the sake of performance. Your users will appreciate it just as much as the system administrators who host the external data.

In our example, the CPI data is only loaded once in the lifecycle of the servlet - when the servlet is loaded into the servlet container. The servlet init() method is called at that time. I've chosen to only hardwire one bit of information into the init() method - the name of the variable that specifies a configuration properties file. Take note that specification of servlet initialization parameters is not standardized, and that each servlet container vendor is free to use whatever technique they choose for setting this up. In Apache JServ, there are different property files for each servlet zone. This zone configuration file may specify servlet aliases and initialization parameters. You'll need to study the administration information for your particular platform. If you want to, you can just hardwire all of the information initialization info into the servlet to avoid the use of the property file code / config file

Next we add the code to parse the comma-delimited data and build our cache, which we are storing as an array of CPI objects. code / data

Now we have everything we need to make our chart. We add the necessary code to the makeChart() method to display the entire range of CPI data code / output

Finally, we make a BarValueSet for the salary inputs and clip the CPI data to the years covered by the data. It was necessary to add another page (dataReady) in order to meet the goal of providing the chart in a separate browser window. Adding this new page involved defining another constant (DONE_DATA), adding the test of the ACTION parameter to the doPost() method to sense this state change, and defining the page with the dataReady() method. Also note that the base value for the cost of living data comes from an additional entry in the configuration file code / config file / output

After testing the prototype with more usage scenarios, and on different platforms (including moving from Java2 to v1.1 of the JVM) a few improvements were made -> code / live demo (you supply the data)

Other things you might like to add to the configuration file would be control over the colors that are used and other things that were hard-wired in my version of this prototype.


ContentsApplication DesignUsage scenarios
Previous: Execution cycle | Next: Who does what - logical delegation