RSS feeds using JAVA

RSS feedsRSS feeds are always becoming used more and more. Using RSS feeds is a good way to keep updated on a particular subject, it is also a very good way to keep a website always updated without the need of manual updates. RSS feeds are very easy to install into a website and with their help, your website will never be out of date!

In order to add RSS feeds to your website, follow these steps below:

Movies

Assuming we want the latest RSS feeds about movies. We would primarily need the URL of the feeds.

In our case we will be using the following link providing Yahoo RSS:

http://rss.news.yahoo.com/rss/movies

Step 1: To begin with, the following libraries need to be installed: Rome and JDOME,  by adding the jar files to the project’s library. * Refer to bottom of page for download links of libraries!


Step 2: Create a new Java Servlet (name it RSSfeeds.java)

Step 3:  Add the following imports to Servlet:

import com.sun.syndication.feed.synd.SyndEntry;

import com.sun.syndication.feed.synd.SyndFeed;

import com.sun.syndication.io.SyndFeedInput;

import com.sun.syndication.io.XmlReader;

import java.net.URL;

import java.util.List;

Step 4: In the process request add the following two lines:

String printFeeds = getRSSfeeds(http://rss.news.yahoo.com/rss/movies);

out.println(printFeeds);

Step 5: Create a method and name it getRSSfeeds which contents should look like:

protected String getRSSfeeds(String urlFeed)

{

StringBuffer sb = new StringBuffer();

try

{

URL feedURL = new URL(urlFeed);

SyndFeedInput input = new SyndFeedInput();

SyndFeed feed = input.build(new XmlReader(feedURL));

List list = feed.getEntries();

if(list.size() > 0)    {

for (int i = 0; i < list.size(); i++ )

{     String container = “<p class=\”RSSlist\”>”;

sb.append(container);

String title = ((SyndEntry)list.get(i)).getTitle();

sb.append(title+”\n\n”);

String link = ((SyndEntry)list.get(i)).getLink();

String url = “ …read more “;

sb.append(url);

String endContainer = ”

“;

sb.append(endContainer);

}

}

}

catch(Exception e){}

finally   {            return sb.toString();            }

Step 6:
Finally all code is ready. All is left to do is to call the RSS feed method from the JSP files by using where ever desired on the page. This will display the title and a read more link. To call the servlet just copy and paste this include tag:

<jsp:include page=”RSSfeeds”/>
****************** Downloading Libraries ********************

You can find these Jar files from the following links:

Rome 1.0 DOWNLOAD HERE

JDome  DOWNLOAD HERE ( Make sure to download the following zip file: jdom-1.1.1.zip )

JDome important download notes:

You only need one jar file which can be found in the build file after extracting files from the zipped download.

Both jar files must be stored in a fixed location and added to library in the website project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s