A “How To” article on working with RSS and JSP with Oracle JDeveloper 10.1.3
- Introduction
- Getting Started—and JSP
- Building a Simple JSP RSS App
- Using JSP Segments to Show Multiple RSS Feeds
- Summary
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:
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.
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.
- 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.
- Click OK to generate an empty Application Workspace. A create project dialog then will appear automatically.
- Name the project “web” (because it will be a Web project).
- Next, you’ll create a JSP page by choosing File->New..->Web-Tier->JSP->JSP.
- As the wizard invokes, name the new page “hellorss.jsp” (or whatever you prefer) and click Next to proceed in the JSP wizard.
- 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.
- You can now Finish to generate your JSP.
- 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.
- 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 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.
Add the following address to the URL field:
- 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:
-
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.
- 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>
- 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!?
- 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.
- To embed both the OTN and Developer.com News feeds onto a page, you can create two JSP Segments: developer.jspf & otn.jspf.
- To create the JSP segments, invoke the JSP Segment creation wizard in JDeveloper: File->New..->Web-Tier->JSP-> JSP Segment.
-
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>
- 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“.
- 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.
- 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>
- 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:
- 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.
- One more thing!! For your homework assignment: Create a news feed page for my Blog’s RSS Feed!: http://www.jroller.com/rss/cschalk
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.