An example of JSP technology implementing RSS subscription function

  • 2021-11-29 08:10:40
  • OfStack

RSS, also known as aggregated RSS, is an easy way to share content online (also known as aggregated content, Really, Simple, Syndication). Usually, using RSS subscription on time-sensitive content can get information more quickly, and the website provides RSS output, which is beneficial for users to get the latest updates of website content. At the same time, it is also the English abbreviation of proper nouns in medicine, physics, mathematics and other disciplines.

The original RSS dates back to 1995, when Ramanathan V. Guha and other advanced technology groups at Apple Computer developed a content framework for testing. More information about RSS can be found here. Let's generate RSS for our JSP website.

RSS Purpose:

1. Subscribe to BLOG

(You can subscribe to technical articles you need for your job; You can also subscribe to the Blog of authors who share your hobbies. In short, you can subscribe to whatever you are interested in.)

2. Subscribe to the news

(Whether it's anecdotes, star news, sports, as long as you want to know, you can subscribe.) You don't have to visit one website, one website and one webpage anymore. As long as this subscribes the content you need to an RSS reader, it will automatically appear in your reader, and you don't have to constantly refresh the web page for a message you are eager to know, because the RSS reader will notify you once an update is made.

The most basic RSS structure for realizing RSS function with JSP technology:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" 
xmlns:wfw="http://wellformedweb.org/CommentAPI/"   
xmlns:slash="http://purl.org/rss/1.0/modules/slash/">  
</rss>  
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<rss version="2.0"  
xmlns:dc="http://purl.org/dc/elements/1.1/"  
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"  
xmlns:wfw="http://wellformedweb.org/CommentAPI/"  
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"> 
</rss> 

Realizing RSS function with JSP technology is actually an XML file!

In < rss > < /rss > We are free to configure the format of RSS to be released. For example, the RSS we are going to release this time is like this:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" x  
mlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"  
xmlns:wfw="http://wellformedweb.org/CommentAPI/" x  
mlns:slash="http://purl.org/rss/1.0/modules/slash/">  
<channel>  
<item>  
<title>JSP Website RSS Implementation of </title>  
<author>neeke</author>  
<pubDate>2008-10-04</pubDate>  
<description> This is from Nick's technology blog RSS</description>  
<category>J2EE Technology </category>  
</item>  
</channel>  
</rss> 

Knowing its structure makes it easy to implement. We create the IO stream and get the collection of RSS resources we want to publish from the database, and then write it to an RSS. XML file line by line according to its format and structure.


public static void publishRss(String rssPath)  
  {  
  // Object to be published RSS Data set   
    ArrayList rssArticle = ArticleManager.getArticlesAll();  
    try { // Create an input-output stream   
      FileWriter fw = new FileWriter(rssPath);  
      BufferedWriter bw = new BufferedWriter(fw); // Start writing data in format   
      bw.write(  
          "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n");  
      bw.write("<?xml-stylesheet type=\"text/xsl\" href=\"CSS/rss.xslt\"?>");  
      bw.write("<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"  
 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\"  
 xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"  
 xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\">\r\n");  
      bw.write("<channel>\r\n");  
      for (int i = 0; i < rssArticle.size(); i++) {  
        ArticleBean article = (ArticleBean) rssArticle.get(i);  
        bw.write("<item>\r\n");  
        bw.write("<title>" + article.getTitle() + "</title>\r\n");  
        bw.write("<author>" + article.getAuthorId() + "</author>\r\n");  
        bw.write("<pubDate>" + article.getPostTime() + "</pubDate>\r\n");  
        bw.write("<description>" + article.getIntro() +  
             "</description>\r\n");  
        bw.write("<category>" + article.getCateId() + "</category>\r\n");  
        bw.write("</item>\r\n");  
      }  
      bw.write("</channel>\r\n");  
      bw.write("</rss>");  
      // Close the stream, RSS Release complete.   
      bw.close();  
      fw.close();  
    } catch (IOException ex) {  
      ex.printStackTrace();  
    }  
  } 

This use of JSP implementation RSS is released, after my test open will prompt error, with Notepad to open the source file generated, however, our RSS file is a problem without wow, how is it possible? Then I opened this XML file with JBuilder, fainted ~ Chinese characters are all garbled, and immediately realized where the problem was. Where is it? In fact, it is the coding problem of files! Will the original bw. write (" < ?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"? > \ r\ n "); UTF-8 in this line of code is changed to GBK, run the program again once, and cut OK once!


Related articles: