Using JSP to implement web-based RSS reader

Source: Internet
Author: User
Tags implement net response code version window client netbeans java se
Js|rss|web

One: RSS introduction

According to Wikipedia (Http://zh.wikipedia.org/wiki/RSS), "RSS is a data exchange specification for sharing news and other Web content", a series of canonical combinations that take the form of XML. At present the domestic RSS application Most is in the news website and the blog website.

Many websites can use RSS reader to personalize their own pages, such as displaying the latest Sina news, showing their best friends the latest blog posts, showing the latest Google forum content. In addition, the use of RSS reader can also achieve other purposes, such as:

Get the weather forecast

Receive emails, like Gmail, to provide RSS feeds

Get the latest stock quotes

Get music, radio programs and video clips, etc.

Two: Rome introduction

This article uses the Rome Open Source tool to implement the RSS reader. Rome supports a lot of formats, there are RSS 0.90, RSS 0.91 Netscape, RSS 0.91 userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, Ato M 1.0 and so on, almost all of the current RSS and Atom versions. The latest Rome version can be obtained from the http://wiki.java.net/bin/view/Javawsxml/Rome.


The implementation of RSS reader, mainly using Rome parsing function, is to read out the corresponding content from the XML file. I use some simple code to illustrate how to use the classes and methods in Rome.


URL feedurl = new URL ("Http://rss.sina.com.cn/news/marquee/ddt.xml");

Syndfeedinput input = new Syndfeedinput ();

Syndfeed feed = input.build (new XmlReader (Feedurl));

Table one: Get the RSS Feed


"Http://rss.sina.com.cn/news/marquee/ddt.xml" is an RSS address for Sina News. With three lines of code, you can get an RSS feed object that corresponds to this address. This object contains all the RSS content we need. If you use SYSTEM.OUT.PRINTLN (feed), you will get the results in table two. The structure of the Syndfeed class can be clearly seen from it.


Syndfeedimpl.contributors=null

Syndfeedimpl.title= News Center-News Highlights

Syndfeedimpl.categories[0].name=

Syndfeedimpl.categories[0].taxonomyuri=null

Syndfeedimpl.link=http://news.sina.com.cn/iframe/o/allnews/input/index.htm

Syndfeedimpl.publisheddate=thu June 13:20:01 CST 2006

Syndfeedimpl.entries[0].updateddate=null

Syndfeedimpl.entries[0].contributors=null

Syndfeedimpl.entries[0].title= al-Zawahiri in the videotape called on Afghans to resist foreign aggression.

Syndfeedimpl.entries[0].categories[0].name=

Syndfeedimpl.entries[0].categories[0].taxonomyuri=null

Syndfeedimpl.entries[0].link=http://news.sina.com.cn/w/2006-06-22/11569270955s.shtml

Syndfeedimpl.entries[0].publisheddate=thu June 11:56:00 CST 2006

Syndfeedimpl.entries[0].authors=null

Syndfeedimpl.entries[0].modules[0].descriptions=[]

syndfeedimpl.entries[0].modules[0].creators[0]=www.sina.com.cn

Syndfeedimpl.entries[0].modules[0].contributors=[]

......

Syndfeedimpl.author=null

Syndfeedimpl.copyright=copyright 1996-2005 SINA Inc. All Rights Reserved

Table II: SYNDFEED data structure


As can be seen from the above output, each news item is represented by entry. The following code gets the entry from the feed

List List = Feed.getentries ();

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

Syndentry entry = (syndentry) list.get (i);

}

Table III: Getting Syndentry from Syndfeed


If the program is behind a firewall, you need to add some proxy settings to your program. For example, use the following HTTP proxy:

Properties systemsettings = System.getproperties ();

Systemsettings.put ("Http.proxyhost", "myproxyserver.com");

Systemsettings.put ("Http.proxyport", "80");

System.setproperties (systemsettings);

Table IV: Proxy settings

Occasionally, you may experience an error message for the Java.io.IOException:Server returned HTTP response code:403 for URL. Typically, because the server's security settings do not accept Java programs as client-side access, the solution is to set the client's user Agent, as shown in the sample code:

URLConnection Feedurl = new Jurl (URLSTR). OpenConnection ();

Feedurl.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) ");

Syndfeedinput input = new Syndfeedinput ();

Syndfeed feed = input.build (new XmlReader (Feedurl));

Table five: Setting up user-agent


ROME provides a lot of functionality, in addition to parsing feeds, you can also generate feeds. Use Rome to create RSS feeds for your Web site content so that others can keep abreast of your content updates through an RSS reader.


The use of Rome requires two conditions:

Java SE 1.4 version, download address: http://java.sun.com/

Open source software jdom, download address: http://www.jdom.org/


Three: Quickly develop a simple example with NetBeans

Here's a simple example prototype development step to show how to quickly use Rome and NetBeans to build a web-based RSS reader.


NetBeans is an open source Java IDE software, download address: http://www.netbeans.org. NetBeans is chosen because it has a built-in tomcat that can save a lot of configuration and run time. And powerful, it can efficiently complete the development of various applications such as Java SE, Java EE and Java me.


Create Web Project with NetBeans 5.0.

Open NetBeans, select menu "File-> New Project", in the New Project window, "category" Select "Web", Project Select "Web Application", and click "Next". In the New Web application window, enter the name of the project, such as "Webrssreader" and the project location, with the rest by default, and click "Finish".


Figure I : creating a Web project for NetBeans

  • In the newly established "Webrssreader" project, add two jar files:

    Jdom.jar:jdom Open Source project (http://www.jdom.org/)

    Rome.jar:ROME Open Source project (http://wiki.java.net/bin/view/Javawsxml/Rome)


    Figure II: Adding a library file

  • Add code to index.jsp

    <% @page contenttype= "text/html"%>

    <% @page pageencoding= "UTF-8"%>



    <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

    <title>sina news</title>

    <body>

    <%

    Java.util.Properties systemsettings = System.getproperties ();

    Systemsettings.put ("Http.proxyhost", "mywebcache.com");

    Systemsettings.put ("Http.proxyport", "8080");

    System.setproperties (systemsettings);



    String urlstr = "Http://rss.sina.com.cn/news/marquee/ddt.xml";

    Java.net.URLConnection Feedurl = new Java.net.URL (URLSTR). OpenConnection ();

    Feedurl.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) ");

    Com.sun.syndication.io.SyndFeedInput input = new Com.sun.syndication.io.SyndFeedInput ();

    Com.sun.syndication.feed.synd.SyndFeed feed = input.build (new Com.sun.syndication.io.XmlReader (Feedurl));

    %>

    <div align= "center" >

    <table border=1 cellpadding=3 width= ">"

    <tr>

    <th>Number</th>

    <th>Title</th>

    <th>Time</th>

    </tr>

    <%

    Java.util.List List = Feed.getentries ();

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

    Com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry) list.get (i);

    %>

    <tr>

    <td><%=i+1%></td>

    <td><a href= "<%=entry.getlink ()%>" ><%=entry.gettitle ()%></a></td>

    <td><%=entry.getpublisheddate ()%></td>

    </tr>

    <%}%>

    </table>

    </div>

    <br>

    </body>

        Table Six : index.jsp All source code

      1. Run the project. Right-click the "webrssreader" Item and select "Run Project".


        Figure III : running the program

      2. The results of the operation are as follows.


    Figure Four : program Run results

    Four: summary

    RSS belongs to Web2.0 technology. Web2.0 advocates personalization and participation. And this simple example of development, the adoption of a number of open source software, open source software is "Everyone for me, I for everyone" a manifestation of the spirit. Most of the time, we don't need to develop some tools from scratch, standing on the shoulders of our predecessors, and developing some better apps might be something that programmers should think about.


    Author Introduction:

    Lili is currently in Sun Microsystems Senior Software engineer, with nearly ten years of experience in software development, worked in AIG , CA such as software engineer and system analyst and other positions, in java EE Field has a wealth of practical development experience. Contact Way:Ada.Li@Sun.com.



  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.