10.1 Presenting the News

One of the interesting things about this particular application is that it makes a series of calls to a number of servers, and, therefore, can easily take 15 to 20 seconds to completely populate the data from the various systems. This is far too long for any user to wait for a page to load (and indeed, most web browsers will time out after this long a delay). Therefore, when the application first starts, it displays a notice to the user, as shown in Figure 10-1. This notice is returned to the user immediately, and the news is fetched on another thread (in this case, using the built-in JDK java.util.TimerTask service).

Figure 10-1. Waiting for the news
figs/rww_1001.gif


As shown in Figure 10-2, after the data has been loaded, the viewer can see the results of the search. The main news stories as chosen by the Yahoo editorial team are displayed on the left. On the right, a feed retrieved from BlogDex (http://blogdex.net/) shows popular stories as determined by the weblog community.

Figure 10-2. Viewing the news
figs/rww_1002.gif


Links providing easy access to pages with additional detail are shown in context. For example, links to reports with additional detail about the North Korean nuclear program are displayed next to the Yahoo news story. These links are based on data generated by Google search results.

As shown in Example 10-1, the bulk of the user interface is devoted to minimal formatting. The most important line, at the top, is the call to NewsSheet.init(); this verifies that the data is ready to be read. The remainder of the code is mostly concerned with looping through a set of returned data, formatted as Story objects.

Example 10-1. News JSP interface
<%@ page contentType="text/html; charset=iso-8859-1" 

language="java" import="com.cascadetg.ch10.*" errorPage="" %>

<html>

<head>

<title>News Sheet</title>

<meta http-equiv="Content-Type" 

content="text/html; charset=iso-8859-1" />

<link href="../ch04/www/default.css" rel="stylesheet" type="text/css" />

</head>

<body>

<h1>News Sheet</h1>

<p align="center"><i><b><font size="+1">What's Going On Right Now

</font></b></i></p>

<% if(!NewsSheet.init( )) { %>

<p align="center">News is still being prepared. Come back soon!</p>

<% } else { %>



<table width="100%"  border="0" cellspacing="5" cellpadding="5">

  <tr>

    <td width="50%"><strong>Main Stories From Yahoo </strong>

    <hr /></td>

    <td width="50%"><strong>Popular In The Blog World </strong>

    <hr /></td>

  </tr>

<%

    for (int i = 0; i < 3; i++)

    {

%><tr>

<%

        for(int ii = 0; ii < 2; ii++)

         { 

            Story current = NewsSheet.getStory(ii, i);

%>

    <td valign="top">

    <p><a target="_blank" href="<%=current.getLink( )%>"><%= current.getTitle( )%>

</a></p>

    <p><%= current.getDescription( )%></p>      

<%  if(ii != 2) { %>    

<table width="90%"  border="0" align="right" cellpadding="3" cellspacing="3" 

bgcolor="#FFFF99">

      <tr>

        <td>

          <% 

            for(int x = 0; x < 3 ; x++)

            { 

%>

          <p><small><a target="_blank" href="<%=current.getRelatedLink(x)%>"><%= 

current.getRelatedTitle(x)%> : <%= current.getRelatedDescription(x)%></a><br />

          </small></p>

          <%

            }

%>

        </td>

      </tr>

    </table><% } %></td><%}%>

</tr><% } %>

</table>

<% } %>

</body>

</html>

The code shown in Example 10-2 serves as a data holder for the information returned by the various web services.

Example 10-2. Story data holder
package com.cascadetg.ch10;



public class Story

{

    protected String title = new String( );

    protected String description = new String( );

    protected String link = new String( );



    protected String[] related_title = new String[3];

    protected String[] related_link = new String[3];

    protected String[] related_description = new String[3];



    public String getTitle( )

    {

        return title;

    }

    

    public String getDescription( )

    {

        return description;

    }



    public String getLink( )

    {

        return link;

    }



    public String getRelatedTitle(int i)

    {

        return related_title[i];

    }

    

    public String getRelatedLink(int i)

    {

        return related_link[i];

    }



    public String getRelatedDescription(int i)

    {

        return related_description[i];

    }

}