Chapter 10. Getting a Graphical Weather Forecast: The Forecaster Project
Here's a cool onethe Forecaster project. It displays a four-day weather forecast for your area, starting with today's high and low temperatures. All you've got to do is to tell the Forecaster your ZIP Code, and it'll give you the forecast.
The Forecaster does its thing by getting the weather data from the National Weather Service, whose website is http://www.crh.noaa.gov. When the Forecaster knows your ZIP Code, it'll fetch your weather forecast from the National Weather Service, create a JPEG image online with the high and low temperatures plotted, and send your browser that graph. You can see what this looks like in Figure 10.1. The forecasted high temperatures are the upper figure (in red, although you can't tell that here), and the low temperatures are the lower figure (in blue in real life).
Figure 10.1. The forecaster in a browser.
This project is written as a JSP, forecast.jsp, partly to show how you can create JPEG images on a server interactively and send them back to a browseran impressive technique that online programmers should have in their arsenals. But this one's too good to miss just because you might not have access to a Java-enabled ISP that can run JavaServer Pages. For that reason, the Forecaster project also comes as a standalone Java application you can run on a desktop machine (as long as you're connected to the Internet so the code can get the needed weather data), as you see in Figure 10.2.
Figure 10.2. The standalone forecaster.
The Forecaster project depends on the National Weather Service website for its data, and it extracts its data from the forecast page for your ZIP Code. Although the format of these pages has been the same for years, there's always the possibility that it may change by the time you read this. In that case, you'll have to update the way the Forecaster project searches those pages for its datajust find the new way the temperatures are displayed, and update the search text in the Forecaster accordingly.
This chapter starts by creating the Forecaster JSP, complete with the technology to create JPEG images online and send them back to the browser. After creating the JSP (forecast.jsp), you'll continue by creating the standalone Java application, Forecast.java, in case you want to use that application instead of the online JSP page.