http://www.developer.com/

Back to article

Working with RSS and Oracle JDeveloper 10.1.3


August 1, 2005

A "How To" article on working with RSS and JSP with Oracle JDeveloper 10.1.3

Introduction

You may or may not have noticed that many Web sites today are starting to offer RSS feeds. What are RSS feeds?

RSS, which stands for Really Simple Syndicate, is basically an XML format for content on the Web that purely represents the content of an Web page without any extraneous presentation or "look and feel" and can thus be syndicated and represented in multiple sites. This contrasts with a traditional Web page that comes with its own formatting and appearance. This article explains how to work with RSS with Java and JSPs along with using Oracle's latest Java development tool, JDeveloper 10.1.3.

Getting Started—RSS and JSP

When I was getting familiar with RSS and how to use it in JSP, I found a very helpful tutorial by Rodrigo Oliveira at Sun that 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

The next set of steps shows how to add this JSP tag library into JDeveloper and then use it to build a simple RSS-enabled JSP application. After downloading the JSP tag library (rss_utils_1.1.zip) and unzipping it on your filesystem, you will add the JSP tag library to your JDeveloper 10.1.3 Preview environment.

Important: It should be noted that basically the same steps can be performed on JDeveloper version 10.1.2, but this article shows how to do it with the current 10.1.3 Preview version of JDeveloper that can be downloaded for free from JDeveloper's home page at Oracle Technology Network.

After downloading and installing JDeveloper (if you haven't done so already), you can proceed to add the JSP tag library to JDeveloper's tag library repository. Adding a JSP tag library to JDeveloper is simply a matter of adding the jar file, which contains the tag handler Java classes, as a "User java Library" and then adding the Tag Library Descriptor (TLD) as a "JSP Tag Library" to JDeveloper. Sometimes, the TLD and tag handlers classes are embedded in a single jar or sometimes they are separate. In this case, the RSS tag library keeps these separate, so these next steps will show how to register both a basic "Java Library" (based on the RSS jar file) as well as a "JSP Tag Library" (based on the RSS TLD) into JDeveloper.

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

    A picture named managelibs.jpg

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



    Click here for a larger image.

  3. Name the library RSS and add a Classpath entry that points to the location of your rssutils.jar that was provided in the downloaded zip file.

    A picture named newlib2.jpg

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



    Click here for a larger image.

  5. 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:



    Click here for a larger image.

  6. Once you've defined the location of the TLD, click OK and you'll see most of 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. Also, change the display name to "Rss Tag Library". This is the title that will appear on the Component Palette for these tags.

    The next step is to associate the jar file or RSS library with this TLD. Click on the "Browse" button for the Libraries.



    Click here for a larger image.

  7. And in the next dialog, shuttle over the RSS library that you added earlier. Then, click OK.



    Click here for a larger image.

  8. Before clicking OK on the parent Manage Libraries dialog, make sure that the checkbox is checked for "Show Tag Libraries in Palette." You'll need these on the palette when you build a JSP application with these tags later. Click OK to dismiss the dialog.

Building a Simple JSP RSS App

Now, it's time to build a simple JSP Web application where you can test the new RSS tags that you've added to JDeveloper.

  1. To start building a new application, click on File->New..->General->Application.  Give it a Name such as "FunWithRSS" and a default package prefix ("com.rss"). Also, select "No Template" for the application templates.
  2. A picture named newapp.jpg

  3. Click OK to generate an empty Application Workspace. A create project dialog then will appear automatically.

  4. Name the project "web" (because it will be a Web project).

    A picture named webapp.jpg

  5. Next, you'll create a JSP page by choosing File->New..->Web-Tier->JSP->JSP.
  6. As the wizard invokes, name the new page "hellorss.jsp" (or whatever you prefer) and click Next to proceed in the JSP wizard.
  7. Before completing the JSP wizard, you'll encounter a step that allows you to specify that you want to use the RSS Tag Library on the page. Shuttle over the "Rss Tag Library..." onto the Selected side. This will pre-insert the RSS JSP taglib directive into your new JSP page.



    Click here for a larger image.

  8. You can now Finish to generate your JSP.
  9. When the JSP appears in the visual editor, type in banner text such as "Fun with RSS" and format it to H2 using the toolbar. You also can drag and drop a CSS file onto the page to beautify it. To drop a CSS file, select the "CSS" palette page and drop one of the CSS items onto the page. It then will automatically appear with the new look and feel.



    Click here for a larger image.

  10. Now, you can start working with the new RSS JSP tags. Change the Component Palette page to "Rss Tag Library" (the new Tag library you added) and drag and drop the Feed tag onto the page:

    A picture named rsstaglib.jpg

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

  12. 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.

    Add the following address to the URL field:

    http://www.oracle.com/technology/syndication/rss_otn_news.xml

    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.
  13. Click OK to finish the dialog.

  14. In general, you can find many RSS-enabled pages on the Web, such as CNN, OTN.Oracle.com, and Developer.com.

    To see the live RSS feed in its native XML format, simply view the link in a browser such as Mozilla's Firefox or Internet Explorer.

    http://www.oracle.com/technology/syndication/rss_otn_news.xml

    This will help you get a better feel for the data that you'll be parsing with JSP tags.



    Click here for a larger image.

  15. In case you are wondering how to find RSS feeds, usually an RSS-enabled Web site will have an icon indicating that it supports RSS newsfeeds. You can find the live RSS XML feed from OTN's home page on the navigation bar on the upper right side:



    Click here for a larger image.

  16. Getting back to the application. After you assign the OTN RSS News URL to the URL attribute of the Feed tag dialog, click OK. This will insert an RSS feed tag onto your page.

    Your source for your feed tag should look like this:

    <rss:feed feedId="otn_news"
              url="http://www.oracle.com/technology/syndication/
                   rss_otn_news.xml"/>
    

    Next, you can add some code to your JSP to display some data extracted from the live feed.

  17. Back in the visual designer, add a new paragraph by entering <return> and drag and drop the tag ChannelTitle tag from the palette. Assign the feedId to "otn_news" (the name you used for your feed tag). The source of your tag will look like the following:

    <p><rss:channelTitle feedId="otn_news"/></p>
  18. At this point, you could run the page and see the title of OTN's news appear, but add more tags to the page to show all the news items. You can copy and paste the following code:

    Important: Make sure to paste the code to the source of the JSP instead of the JSP designer. Click on the "Source" tab at the bottom of the JSP visual editor to edit the source.
    <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>
    

    Save and run the page by right-clicking on your JSP in JDeveloper's file navigator and select Run. Your JSP will have OTN's news items on the page. Cool, huh!?



    Click here for a larger image.

  19. Next, you can build content from Developer.com's RSS News Feed from: http://developer.com/icom_includes/feeds/special/dev-5.xml into a JSP page.

Using JSP Segments to Show Multiple RSS Feeds

These next steps show how to build a JSP page that will pull content from multiple RSS news feeds by using JSP Segments.

  1. To embed both the OTN and Developer.com News feeds onto a page, you can create two JSP Segments: developer.jspf & otn.jspf.
  2. To create the JSP segments, invoke the JSP Segment creation wizard in JDeveloper: File->New..->Web-Tier->JSP-> JSP Segment.
  3. After naming and generating empty JSP Segment files, copy and paste the respective code samples into the empty segment files. This code will pull the content from the different feeds. Also, notice that no taglib directives are needed for these pages; they are just segments.

    Important: Again, make sure to paste the code below into the source of the JSP Segment; otherwise, it will be translated to HTML markup.

    developer.jspf—For Developer.com

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

    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/>
    <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>
    
  4. Next, create a master JSP page (that is not a JSP Segment) in a similar fashion to how you created your original JSP page. You can name the page "double.jsp".

  5. Once your JSP is created, drag and drop two RSS feed tags onto the page. These will allow you get content from both the OTN and Developer.com RSS feeds.

  6. Next, you can create an HTML table and drop two JSP Include directives onto the page that will point to the different OTN and developer jspf (segment) files that you created earlier.

    • To drop an HTML table, switch the Component Palette to "HTML Common" and drag and drop the Table component onto the JSP. Specify a table with 1 row, 2 columns, and 100% width.
    • To drop JSP Include Directives into the table cells, switch the palette to "JSP" and, because there are two "Include" on the palette, drop the second JSP Include, which is the Directive type of JSP Include. You can hover over the palette item to see which type of Include is which. The JSP include directive noted by the "@" symbol pulls the included content before the JSP is compiled.

    Here is the final code of a JSP page that shows both OTN and Developer.com'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>
             double
          </title>
          <link href="css/jdeveloper.css"
                rel="stylesheet"
                media="screen"></link>
       </head>
       <body>
          <h2>
             Fun with RSS
          </h2>
          <rss:feed feedId="otn_news"
                    url="http://www.oracle.com/technology/
                                syndication/rss_otn_news.xml"/>
          <rss:feed feedId="developer_news"
                    url="http://developer.com/icom_includes/
                                feeds/special/dev-5.xml"/>
    
           <table cellspacing="2"
                  cellpadding="3"
                  border="1"
                  width="100%">
              <tr>
                 <th>
                    Oracle Technology Network
                 </th>
                 <th>
                    Developer.com
                 </th>
              </tr>
              <tr>
                 <td>
                    <%@ include file="/otn.jspf"%>
                 </td>
                 <td>
                    <%@ include file="/developer.jspf"%>
                 </td>
              </tr>
          </table>
       </body>
    </html>
    
  7. At this point, you now can run the JSP page, double.jsp, by right-clicking and selecting Run. This will launch the page in JDeveloper's local embedded OC4J J2EE container (server). Your page should have the following appearance:


  8. Click here for a larger image.

  9. 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.

  10. One more thing!! For your homework assignment: Create a news feed page for my Blog's RSS Feed!: http://www.jroller.com/rss/cschalk
  11. Note: You may not be able to view the RSS feed for my blog in your browser because it does not have a ".xml" extension, but the JSP feed tags should work regardless.

Summary

This article hopefully helped open the world of RSS to you by showing how easy it is to experiment with RSS using common J2EE technologies such as JSP and JSP tag libraries in a productive development environment such as JDeveloper. Good luck and have fun building your own custom RSS news fed pages using the JSP tag libraries highlighted here!

About the Author

Chris Schalk is a Principal Product Manager and Java Evangelist for Oracle's application server and development tools division. Chris' primary expertise is Web application development and he is responsible for defining the Web development experience for Oracle JDeveloper.

Chris has written numerous samples and articles for various publications, including Javapro and Oracle Magazine and is currently co-authoring JavaServer Faces—The Complete Reference through McGraw-Hill-Osborne. Chris also maintains a popular Blog on J2EE Web development at http://www.jroller.com/page/cschalk.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date