Use JSP technology to implement RSS Functions

Source: Internet
Author: User

To use JSP technology to implement the RSS function, Let's first look at what RSS is. RSS is also called aggregate content. Really Simple Syndication) It is now widely used. The original RSS will be traced back to 1995, when ramanstmv. guha and other senior technology groups at Apple Computer have developed a content framework for testing. For more information about RSS, see here. Next we will generate RSS for our JSP website.

The most basic RSS structure for implementing RSS functions using JSP technology:

 
 
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>   
  2. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 
  3. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" 
  4. xmlns:wfw="http://wellformedweb.org/CommentAPI/"     
  5. xmlns:slash="http://purl.org/rss/1.0/modules/slash/">   
  6. </rss>   
  7. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
  8. <rss version="2.0"   
  9. xmlns:dc="http://purl.org/dc/elements/1.1/"    
  10. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"    
  11. xmlns:wfw="http://wellformedweb.org/CommentAPI/"    
  12. xmlns:slash="http://purl.org/rss/1.0/modules/slash/"> 
  13. </rss> 

Using JSP technology to implement RSS is actually an XML file!

Between <rss> </rss>, you can configure the RSS format you want to publish. For example, the RSS we want to publish this time is as follows:

 
 
  1. <?Xml Version="1.0" Encoding=UTF-8" Standalone="Yes"?>
  2. <Rss Version="2.0" Xmlns: dc=Http://purl.org/dc/elements/1.1"X
  3. Mlns: trackback=Http://madskills.com/public/xml/rss/module/trackback"
  4. Xmlns: wfw=Http://wellformedweb.org/CommentAPI"X
  5. Mlns: slash=Http://purl.org/rss/1.0/modules/slash">
  6. <Channel>
  7. <Item>
  8. <Title>Implementation of JSP website RSS</Title>
  9. <Author>Neeke</Author>
  10. <PubDate>2008-10-04</PubDate>
  11. <Description>This is an RSS feed from Nick's technical blog.</Description>
  12. <Category>J2EE Technology</Category>
  13. </Item>
  14. </Channel>
  15. </Rss>
 

It is easy to understand its structure implementation. We create an IO stream, obtain the set of RSS resources to be published from the database, and write a row to an RSS. XML file according to its format and structure.

 
 
  1. Public static void publishRss (String rssPath)
  2. {
  3. // Obtain the RSS data set to be published
  4. ArrayListRssArticle=ArticleManager. GetArticlesAll ();
  5. Try {// create an input/output stream
  6. FileWriterFw=NewFileWriter (rssPath );
  7. BufferedWriterBw=NewBufferedWriter (fw); // starts writing data according to the format
  8. Bw. write (
  9. "<?Xml Version= \ "1.0 \"Encoding= \ "UTF-8 \"Standalone= \ "Yes \"?>\ R \ n ");
  10. Bw. write ("<?Xml-stylesheet Type= \ "Text/xsl \"Href= \ "CSS/rss. xslt \"?>");
  11. Bw. write ("<Rss Version= \ "2.0 \"Xmlns: dc= \ "Http://purl.org/dc/elements/1.1 /\"
  12. Xmlns: trackback= \ "Http://madskills.com/public/xml/rss/module/trackback /\"
  13. Xmlns: wfw= \ "Http://wellformedweb.org/CommentAPI /\"
  14. Xmlns: slash= \ "Http://purl.org/rss/1.0/modules/slash /\">\ R \ n ");
  15. Bw. write ("<Channel>\ R \ n ");
  16. For (intI=0; I< RssArticle. size(); I ++ ){
  17. ArticleBeanArticle= (ArticleBean) rssArticle. get (I );
  18. Bw. write ("<Item>\ R \ n ");
  19. Bw. write ("<Title>"+ Article. getTitle () +"</Title>\ R \ n ");
  20. Bw. write ("<Author>"+ Article. getAuthorId () +"</Author>\ R \ n ");
  21. Bw. write ("<PubDate>"+ Article. getPostTime () +"</PubDate>\ R \ n ");
  22. Bw. write ("<Description>"+ Article. getIntro () +
  23. "</Description>\ R \ n ");
  24. Bw. write ("<Category>"+ Article. getCateId () +"</Category>\ R \ n ");
  25. Bw. write ("</Item>\ R \ n ");
  26. }
  27. Bw. write ("</Channel>\ R \ n ");
  28. Bw. write ("</Rss>");
  29. // Close the stream. RSS has been published.
  30. Bw. close ();
  31. Fw. close ();
  32. } Catch (IOException ex ){
  33. Ex. printStackTrace ();
  34. }
  35. }

In this way, RSS is published using JSP. After my test is enabled, an error is prompted. Open the generated source file with notepad. However, our RSS file is completely unavailable, how is it possible? Then I opened the XML file using JBuilder and fainted ~ All Chinese characters are garbled and immediately realize where the problem is. Where? It is actually a file encoding problem! Convert the original bw. write ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \" standalone = \ "yes \"?> \ R \ n "); The UTF-8 in the line of code to GBK, re-run the program, everything OK!


  1. Methods for constructing JSP and Javabean development and release Environments
  2. Analysis of JSP Design Mode
  3. What is JSP and its strong weakness?
  4. How Tomcat improves performance on JSP pages
  5. Use of scripts Based on JSP tutorials

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.