Understanding Internet Resources

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 9.  Accessing Internet Resources with JSTL


Understanding Internet Resources

Numerous sources of XML information are available on the Internet. Many are subscription-based and require you to make payments to their provider. There are also many free XML resources that you can take advantage of on the Internet.

One benefit of using Internet resources is that many times they allow two platforms to work together. Microsoft Passport is an example of Java working with a subscription-based XML service available on the Internet. Passport attempts to solve the problem of identifying users. Often, a user will maintain accounts with several different subscription-based Web sites that the user frequents. This requires the user to register with each individual Web site.

Microsoft Passport attempts to alleviate this burden by having the user register just once, and then provides a universal registration to every site that the user frequents. This universal login is provided by the Microsoft Passport Service.

In order for a typical Web site to maintain such a relationship with Microsoft Passport, the site must exchange information with Microsoft. To accomplish this, XML is used. This is an example of how a Web site might use the Internet resource tags provided by JSTL.

There are also many free sources of XML data that you can integrate into your application. One such service is RSS, which we use for many of this chapter's examples. Let's take a look.

Introducing RSS

Rich Site Summary (RSS) is an XML standard for making the headlines available through a news Web site available to the other Web sites. This standard was introduced by Netscape. You can obtain the official specification from http://my.netscape.com/publish/formats/rss-spec-0.91.html.

This relationship is good for both the site that produces the news stories as well as the site that displays the stories. Sites that receive the stories are given a source of fresh content that can easily be integrated right into their Web site. For news producers, RSS can increase traffic, since all exported stories contain links back to the story at their site.

Many news-related sites make RSS feeds available. Table 9.1 lists some of the commonly used RSS feeds.

Table 9.1. Some RSS Feeds

Web Site

RSS Feed URL

FreshMeat

http://freshmeat.net/backend/fm.rdf

MacWeek

http://macweek.zdnet.com/macweek.xml

Motley Fool

http://www.fool.com/About/headlines/rss_headlines.asp

NewsForge

http://www.newsforge.com/newsforge.rdf

NewsForge

http://www.newsforge.com/newsforge.rss

Reuters Health

http://www.reutershealth.com/eline.rss

Salon

http://www.salon.com/feed/RDF/salon_use.rdf

Slashdot

http://www.slashdot.org/slashdot.rdf

Wired

http://www.wired.com/news_drop/netcenter/netcenter.rdf

The RSS format is a relatively simple XML format. Listing 9.4 shows a sample of RSS data.

Listing 9.4 A Sample RSS Feed File (sample.rss)
<?xml version="1.0" ?> <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"   "http://www.wired.com/news_drop/rss-0_91.dtd"> <rss version="0.91">   <channel>     <title>My Information Service</title>     <link>http://www.jeffheaton.com/</link>     <description>       This is where the description for this channel goes.     </description>     <language>en-us</language>     <pubDate>Tue, 21 May 2002 17:38 PDT</pubDate>     <image>       <title>My Image Title</title>       <url>http://www.jeffheaton.com/images/jeffpic1.jpg</url>       <link>http://www.jeffheaton.com</link>     </image>     <item>       <title>First Story Title</title>       <link>http://www.jeffheaton.com/news/story1.html</link>       <description>         This is where the brief description to the first story   belongs.       </description>     </item>     <item>       <title>Second Story Title</title>       <link>http://www.jeffheaton.com/news/story2.html</link>       <description>         This is where the brief description to the second story   belongs.       </description>     </item>     <item>       <title>Third Story Title</title>       <link>http://www.jeffheaton.com/news/story3.html</link>       <description>         This is where the brief description to the third story   belongs.       </description>     </item> </channel> </rss> 

Let's examine the major tags used by the XML-based RSS file format. As you can see in Listing 9.4, the entire file is contained in the <rss> tag. This tag can contain one or more channels. Most RSS feeds will have a single channel.

The <channel> tag encloses one news feed. Inside the <channel> tag, you first encounter some tags that provide general information about the channel. The first piece of information is usually the title:

<title>My Information Service</title> 

The title is typically displayed on a Web site with a link back to the original news site that provided the story. The Web application must know where this link should point. The destination of the title <link> tag should specify the Web address that the news provider would like people to visit when they click on the title. The following is an example of a <link> tag:

<link>http://www.jeffheaton.com/</link> 

In addition to the link and title, the RSS format specifies that a description of the channel be provided. The following code shows the <description> tag:

<description>   This is where the description for this channel goes. </description> 

The RSS standard also allows for languages other than English to be used within RSS feeds. To do this, you must specify the language that your site is in by using the <language> tag. The following <language> tag specifies that U.S. English is the language being used:

<language>en-us</language> 

How current news stories are is often a critical factor in their success. The <pubDate> tag allows you to specify the date that the channel was published. Here's an example of the <pubDate> tag:

<pubDate>Tue, 21 May 2002 17:38 PDT</pubDate> 

In addition to the title provided by the RSS feed file, you are given an image to display. The image is determined by the tags beneath the <image> tag. The following shows how the <image> tag is represented in an RSS feed file:

<image>   <title>My Image Title</title>   <url>http://www.jeffheaton.com/images/jeffpic1.jpg</url>   <link>http://www.jeffheaton.com</link> </image> 

As you can see, the <image> node contains a number of subnodes that describe the image. The <title> tag contains the title of the image, which is usually specified as a part of the HTML <img> tag's ALT attribute. The <url> tag specifies the URL of the image, which is included in the display. Finally, the <link> tag specifies the URL that users will be taken to if they click the image.

These tags provide the header information for an RSS news feed. Now, let's look at the individual news stories contained in a feed. A news story is stored in an <item> tag:

<item> 

Each story is provided with a title. The title is specified using the <title> tag:

<title>First Story Title</title> 

Usually, this title will be linked to the actual story on the news provider's Web site. The following <link> tag specifies the URL to be accessed if the user clicks the title:

<link>http://www.jeffheaton.com/news/story1.html</link> 

A short description accompanies the RSS news story. This allows readers to get a general idea of what the story is about before they click the link that will take them back to the news publisher's site:

  <description>     This is where the brief description to the first story belongs.   </description> </item> 

Now that you understand the basic format of the RSS XML file format, let's look at a simple Web application that displays the headlines of the news provider.

Using the RSS Format

When executed, our sample Web page will display a table that tells readers what news stories are available from the Wired RSS feed. This program is shown in Listing 9.5.

Listing 9.5 Reading from an RSS feed(news.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %> <html>   <head>     <title>Reading RSS</title>   </head>   <body>     <c:import var="news"     url="http://www.wired.com/news_drop/netcenter/netcenter.rdf" />     <x:parse var="doc" xml="${news}" />     <table border="1" width="100%">       <tr bgcolor="blue">         <td align="center">           <font color="white" size="+2">             <b>               <x:out select="$doc/rss/channel/title" />             </b>           </font>           <br />           <font color="white" size="-2">             <x:out select="$doc/rss/channel/pubDate" />           </font>         </td>       </tr>       <tr>         <td valign="top">           <x:out select="$doc/rss/channel/description" />         </td>       </tr>       <x:forEach var="story" select="$doc/rss/channel/item">         <tr bgcolor="blue">           <td align="center">             <a href="<x:out select="link"/>">             <font color="white">               <x:out select="title" />             </font>             </a>           </td>         </tr>         <tr>           <td valign="top">             <x:out select="description" />           </td>         </tr>       </x:forEach>     </table>   </body> </html> 

The first thing that our program does is connect to the Wired RSS feed:

<c:import var="news" url="http://www.wired.com/news_drop/netcenter/netcenter.rdf" /> 

As you can see, the contents of the Wired RSS feed are copied into the scoped variable news. This XML data must then be parsed:

<x:parse var="doc" xml="${news}" /> 

With the document properly parsed, our program is ready to display the header information. The program displays this information in an HTML table.

The title of the news story is displayed using an <x:out> tag, and can easily be accessed using an XPath. The following tag displays the title:

<x:out select="$doc/rss/channel/title" /> 

The publication date and description are displayed in a similar manner:

  <x:out select="$doc/rss/channel/pubDate" /> <x:out select="$doc/rss/channel/description" /> 

The individual stories are contained in a collection. To display the stories, we use the <x:forEach> tag:

<x:forEach var="story" select="$doc/rss/channel/item"> 

Individual stories can now be displayed as the <x:forEach> tag iterates through each story in the collection:

<a href="<x:out select="link"/>"> <font color="white">   <x:out select="title" /> 

Our program displays the title with an enclosing <a> tag that provides the hyperlink to the real story. The description of the story is displayed using the <x:out> tag:

<x:out select="description" /> 

As you can see, the RSS format can easily be integrated into most Web sites. Other XML formats are available that you can include just as easily into your Web site, and still others are being created. By using XML from external resources, you can keep your Web site content fresh. The URL tags allow you to take advantage of XML data sources.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch09lev1sec2]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net