Article
Use Custom Tags to Aggregate RSS Feeds into JSP-Based Web Apps
With the abundance of news and blog (Weblog) sites growing continuously, keeping track of what’s going on can be a daunting task. Fortunately, standards such as RSS (Really Simple Syndication) provide an easy way to grab content from a particular site and aggregate it into a news reader application. This means that, rather than looking for news yourself, your news reader monitors sites that you’re interested in and downloads new content as it’s published.
This is a great model and many people have taken the concept to the Web by aggregating other content on their own Websites and offering aggregation services via the Web. In this article, I’ll show you how to use JSP custom tags to implement this type of functionality as a reusable component within your own JSP-based Web applications. Although I’m assuming that you’re comfortable with Java and you have some working knowledge of building Web applications with JavaServer Pages (JSP), I won’t assume any knowledge of JSP custom tags. Before we get started, download the code we'll use in this article here.
RSS and News Aggregators - a Brief History
The RSS standard has been around for the last few years, but only recently has it really started to catch on. One of the reasons behind this is that software like MovableType and Radio Userland have made blogging available to the masses in an affordable package. Where previously, news sections of Websites contained snapshots of a particular company or individual, and were rarely updated, these RSS tools allow us to take a more dynamic approach, providing an easy mechanism by which to add new news items to sites.
Thanks to these tools, and the proliferation of news being presented via the Internet, keeping up to date is a much more difficult task than ever before. For this reason, a standard format was defined to allow news to be syndicated and aggregated through desktop applications called news readers.
The result was that RSS, the Really Simple Syndication format, was born. In essence, RSS is simply an XML document that can be used to describe the content available on a given Website. Typically, “content” means news items, but other uses of RSS include summarising articles, short stories, and so on. A good example of an RSS feed is the UK news from the BBC. The introduction of the RSS standard format made the aggregation of content much, much easier than before.
Reading RSS Feeds from Java
Since RSS feeds are nothing more than standardised XML documents, reading and processing RSS is fairly easy in any language that provides support for XML. Now that J2SE 1.4 provides integral support for XML, it’s simply a matter of using the appropriate classes to read in the XML document. Once the document has been read, presenting it back to the user in a desktop or Web-based application is then trivial.
For the purpose of this article, I’m going to show how to aggregate content from an RSS feed into your own Web applications. The use of this technique can be applied across a wide range of applications, from corporate intranet sites aggregating content from various departments, to personal Websites aggregating content from friends and family.
Before we talk more about custom tags, let's quickly look at how we will read in the RSS feeds using Java code. Manipulating XML documents in Java code can be tedious and therefore, rather than code against the raw XML, I've chosen to build a very simple object representation of the RSS feed. The first class is called RssFeed, and this represents a given RSS feed that contains a number of items.
package rss;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class RssFeed {
private Collection items = new ArrayList();
public void addItem(RssItem item) {
items.add(item);
}
public Collection getItems() {
return Collections.unmodifiableCollection(items);
}
}
In reality, RSS feeds have other characteristics, but the collection of items is enough for our purposes. Next up is the Java class that represents an item in a RSS feed. Typically, several pieces of information will be presented for each item in the feed; I've chosen to wrap these within an object. The information that’s relevant for each item in this example is its title and a link (URL) back to the full story on the Web.
package rss;
public class RssItem {
private String title;
private String link;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
The final piece of the puzzle is the class that will actually read the XML document and transform it into our object representation -- the RssReader class. Instead of getting bogged down with the syntax and semantics of reading XML files, the implementation of this class has been omitted. Essentially, all that the read() method does is access the RSS feed at the specified URL and convert each item contained within the feed into an RssItem object.
package rss;
public class RssReader {
/**
* Reads the RSS feed at the specified URL and returns an RssFeed instance
* representing it.
*/
public RssFeed read(String url) {
... body of method omitted ...
}
}
This is all the logic we need to read RSS feeds. Let's now take a look at how to hook this into a JSP page.
A technical architect/Java developer, Simon has written and contributed to