Updated: 3/5/2005; 10:21:03 AM.
Chris Schalk's J2EE Weblog
This Blog Discusses Tips and Tricks for building J2EE Web Applications with Oracle JDeveloper
        

Wednesday, February 23, 2005

If you are reading this Blog, chances are you already have an understanding of what RSS (Really Simple Syndicate) is and that it is basically just an XML format for content on the Web.

I recently looked into how to work with RSS in Java and JSP and happened to find a nice tutorial by Rodrigo Oliveira at Sun which showed a simple RSS enabled JSP taglib:

http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/

In this tutorial is a link where you can download a JSP RSS Utility Tag library:

http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/rss_utils_1.1.zip

So after downloading unzipping the contents, I decided to play with the JSP taglib in JDeveloper 10.1.3 Preview.

Here's what I did...

After downloading, I first had to add the JSP tag library to JDeveloper. This is easily done by first adding the jar file as a "User java Library" and then adding the TLD as a JSP Tag library. This is an important process to know if you wish to import external JSP tag libraries into JDeveloper.

To start, open JDeveloper 10.1.3 and click Tools->Manage Libraries:

A picture named managelibs.jpg

 

  • Next, click on the "User" node on the and click "New".

    A picture named newlib.jpg

  • Name the library RSS and add a Classpath entry which points to the location of your rssutils.jar which you downloaded.


    A picture named newlib2.jpg

  • Next you'll want to click on the "JSP Tag Libraries" tab in the Manage Libraries dialog.

    A picture named newlib4.jpg

  • In the JSP Tag Libraries panel, click on the "User" node and and click new. This is when you will browse for the location of the RSS taglib's TLD file:

    A picture named rsstld.jpg

  • Once you've defined the location of the TLD, click ok and you'll see most off the details for the Taglib filled in. You'll want to change the default prefix to "rss" just to make it easier to work with later on. The next step is to associate the jar file or RSS library with this TLD. Click on the "Browse" button for the Libraries....

    A picture named taglib2.jpg
  • And in the next dialog, shuttle over the RSS library. Then click OK.

    A picture named newlibjar.jpg

  • Before clicking OK on the Manage Libraries dialog, make sure that the checkbox is checked for: "Show Tag Libraries in Palette". You'll need these on the palette for when you build an app. Click OK to dismiss the dialog. 
  • Now it's time to build a simple JSP Web application where we can test the tags... To start building a new application, click on File->New..->General->Application.  Give it a Name and default package prefix. Also select "No Template" for the application templates.

    A picture named newapp.jpg

  • Click OK to generate an empty Application Workspace, a create project dialog will then appear automatically. Name the project "web". (Since it will be a Web project.)

    A picture named webapp.jpg
  • Next we'll create a JSP page. File->New..->Web-Tier->JSP->JSP.

  • As the wizard invokes name it whatever you like and specify that you want to use the RSS Tag Library:

    A picture named addtaglib.jpg

  • Click OK to generate your JSP.

  • When the JSP appears in the visual editor, add an HTML banner such as "Fun with RSS". Format it to H2 using the toolbar. You can also drag and drop a CSS file onto the page to beautify it. To drop a CSS file, select the "CSS" palette page and drop on of the CSS items onto the page. It will then automatically appear with the new look and feel.

    A picture named newpage.jpg

  • Now we can start working with the tags. Change the Component Palette page to "RSS Tag Library" and drag and drop the Feed tag onto the page:
    A picture named rsstaglib.jpg

  • A dialog will appear showing the required field FeedId. Set it to "otn_news".

  • Now click on the Advanced Properties tab in the same dialog. You'll see Proxy settings and a URL field. This is where you will insert a web address pointing to a RSS page.

    Note: You won't need to set the proxy in JDeveloper if they're already set in the JDeveloper "Web Browser and Proxy" setting in the preferences. However if you deploy this to another J2EE server, you'll either need to run the server with the Proxy Server setting or just user these tag attributes for proxy settings.

  • You can find many RSS enabled pages on the Web like CNN, OTN.Oracle.com, TheServerSide.com. Let's use OTN's Main News RSS Feed. For this we need the URL: http://www.oracle.com/technology/syndication/rss_otn_news.xml

    Here's where to find the live XML feed from OTN's home page:

  • Assign the OTN RSS News URL to the URL attribute of the Feed tag dialog and click OK. This will insert a feed tag onto your page.

  • Feel free to check out the XML from the live feed. This will help you get a better feel for the data that you'll later be parsing with JSP tags.

  • Next we'll add some code to our JSP to display some data from the live feed.

  • In a new paragraph drop drag and drop the tag ChannelTitle tag from the palette. Assign the feedId to "otn_news". The source of your tag will look like:  <p><rss:channelTitle feedId="otn_news"/></p>

  • At this point you could run the page and see the title of OTN's news appear.. but let's add more tags to the page to show all the news items.. You can copy and paste the following code:

     <p><rss:channelTitle feedId="otn_news"/></p>
        <p><rss:channelImage feedId="otn_news" asLink="true"/></p>
        <p><rss:channelLink feedId="otn_news" asLink="true"/></p>
        <hr/>
     
       <rss:forEachItem feedId="otn_news">
                  <li>
                   <a href="<rss:itemLink feedId="otn_news"/>" >
                      <rss:itemTitle feedId="otn_news"/>
                   </a>
                   
                   <br/>  
                    <rss:itemDescription feedId="otn_news"/>
                  </li>
         
        </rss:forEachItem>

  • When you compare the tags and the live XML feed you'll understand how the tags pull the data out of the XML feed.

  • Save and run the page. Your JSP will have OTN's news items on the page. Cool huh!?


  • For fun, check out TheServerSide's RSS News Feed from:  http://www.theserverside.com/rss/theserverside-rss2.xml

  • To embed both OTN and TheServerSide News feeds onto my page.. You can create 2 JSP Segments  using File->New..->Web-Tier->JSP-> JSP Segment, which have just the Feed code needed to see a particular content like:

    tss.jspf - The ServerSide

    <p><rss:channelTitle feedId="tss_news"/></p>
        <p>
        <rss:channelImage feedId="tss_news" asLink="true"/>
        </p>
        <p>
          <rss:channelLink feedId="tss_news" asLink="true"/></p>
       
        <hr/>
         <ul>
          <rss:forEachItem feedId="tss_news">
           <li><a href="<rss:itemLink feedId="tss_news"/>">
                 <rss:itemTitle feedId="tss_news"/></a>
           <br/><rss:itemDescription feedId="tss_news"/></li>
          </rss:forEachItem>
         </ul>

    and the JSP segment for OTN:

    otn.jspf - For OTN

      <p><rss:channelTitle feedId="otn_news"/></p>
        <p>
        <rss:channelImage feedId="otn_news" asLink="true"/>
        </p>
        <p>
          <rss:channelLink feedId="otn_news" asLink="true"/></p>
       
        <hr/>
         <ul>
          <rss:forEachItem feedId="otn_news">
           <li><a href="<rss:itemLink feedId="otn_news"/>">
                 <rss:itemTitle feedId="otn_news"/></a><br/>
                 <rss:itemDescription feedId="otn_news"/></li>
          </rss:forEachItem>
         </ul>

  • You can then create  a master page which has a taglib directive for the RSS taglib as well as 2 feed tags for both OTN and TSS.

  • Next you can create an HTML table and drop 2 JSP Include directives onto the page which point to the different OTN and TSS jspf (segment) files.

    Here is the  code of a JSP page which shows both OTN and TheServerSide's News Feeds:

    double.jsp

       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ taglib uri="http://sun.com/cnpi/rsstag" prefix="rss"%>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
        <title>
          hellorss
        </title>
        <link href="css/jdeveloper.css" rel="stylesheet" media="screen"></link>
      </head>
      <body>
        <h2>
          Fun With RSS
        </h2>
     
        <rss:feed feedId="tss_news"
                  url="http://www.theserverside.com/rss/theserverside-rss2.xml"/>
       
        <rss:feed feedId="otn_news"
                  url="http://www.oracle.com/technology/syndication/rss_otn_news.xml"/>         
                  
        <table cellspacing="2" cellpadding="3" border="1" width="100%">
            <tr>
              <th>
                OTN
              </th>
              <th>
                TheServerSide
              </th>
            </tr>
            <tr>
              <td>
                <%@ include file="/otn.jspf"%>
              </td>
              <td>
                <%@ include file="/tss.jspf"%>
              </td>
            </tr>
          </tbody>
      </body>
    </html>

  • Neat huh?

    A picture named double.jpg

  • A final note, everything shown here is with RSS version 2.0. The taglib can also work with older version of RSS, but the examples I've shown here are all 2.0.

  • Okay one more thing!! For your homework assignment: Create a news feed page for my Blog's RSS Feed!: http://radio.weblogs.com/0130966/rss.xml


    1. 1:50:13 PM    comment []

      © Copyright 2005 Chris Schalk.
       
      February 2005
      Sun Mon Tue Wed Thu Fri Sat
          1 2 3 4 5
      6 7 8 9 10 11 12
      13 14 15 16 17 18 19
      20 21 22 23 24 25 26
      27 28          
      Jan   Mar


      Search blog with Google:
       








      Click here to visit the Radio UserLand website.

      Subscribe to "Chris Schalk's J2EE Weblog" in Radio UserLand.

      Click to see the XML version of this web page.

      Click here to send an email to the editor of this weblog.