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:
- <?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>
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:
- <?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>Implementation of JSP website RSS</Title>
- <Author>Neeke</Author>
- <PubDate>2008-10-04</PubDate>
- <Description>This is an RSS feed from Nick's technical blog.</Description>
- <Category>J2EE Technology</Category>
- </Item>
- </Channel>
- </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.
- Public static void publishRss (String rssPath)
- {
- // Obtain the RSS data set to be published
- ArrayListRssArticle=ArticleManager. GetArticlesAll ();
- Try {// create an input/output stream
- FileWriterFw=NewFileWriter (rssPath );
- BufferedWriterBw=NewBufferedWriter (fw); // starts writing data according to the 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 (intI=0; I< RssArticle. size(); I ++ ){
- ArticleBeanArticle= (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 has been published.
- Bw. close ();
- Fw. close ();
- } Catch (IOException ex ){
- Ex. printStackTrace ();
- }
- }
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!
- Methods for constructing JSP and Javabean development and release Environments
- Analysis of JSP Design Mode
- What is JSP and its strong weakness?
- How Tomcat improves performance on JSP pages
- Use of scripts Based on JSP tutorials