Java Publishing RSS Feeds

In this article let's discuss about how to publish an RSS feed in java using Rome API.

What is RSS?

RSS(Really Simple Syndication) is a xml format that lets you pull content from another Web site and display it using an RSS feed reader in a standard format.
An RSS document includes full or summarized text, metadata such as publishing dates usually in an xml format.
The readers can subscribe to timely updates from there favorite websites using RSS. RSS feeds can be read using software called RSS reader. The feed reader executes the XML code, displaying the content in a readable format with a list of titles and links. Browsers such as Mozilla have plug-in for reading RSS feeds.

The following Servlet is used to publish an RSS feeds for techdive.in using Rome API in java.
RSSPublisherServlet.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSES ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

public class RSSPublisherServlet extends HttpServlet
{

       
        public RSSPublisherServlet()
       
        /*
         * (non-Javadoc)
         * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
         * javax.servlet.http.HttpServletResponse)
         */

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {

                SyndFeed feed = new SyndFeedImpl();
                feed.setFeedType("rss_2.0");
                feed.setTitle("Techdive New Articles");
                feed.setLink("http://techdive.in/");
                feed.setDescription("Articles Uploaded in Techdive");

                SyndEntry entry = new SyndEntryImpl();
                entry.setTitle("Articles");
                entry.setLink("http://techdive.in/technology/java");
                entry.setPublishedDate(new Date());

                SyndContent description = new SyndContentImpl();
                description.setType("text/html");
                description.setValue("Publish Rss Feeds Using Java!");
                entry.setDescription(description);

                List<SyndCategory> categories = new ArrayList<SyndCategory>();
                SyndCategory category = new SyndCategoryImpl();
                category.setName("TechDive");
                categories.add(category);
                entry.setCategories(categories);

                List lstEntry = new ArrayList();
                lstEntry.add(entry);
                feed.setEntries(lstEntry);

                Writer writer = resp.getWriter();
                resp.setContentType("application/xml; charset=UTF-8");
                SyndFeedOutput output = new SyndFeedOutput();
                try
                {
                        output.output(feed, writer);
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                writer.flush();
                writer.close();
        }

        /*
         * (non-Javadoc)
         * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
         * javax.servlet.http.HttpServletResponse)
         */

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
                super.doPost(req, resp);
        }

}

The deployment descriptor is shown below for your reference

web.xml

<servlet>
  <servlet-name>rss</servlet-name>
  <servlet-class>
    in.techdive.servlet.RSSPublisherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>rss</servlet-name>
  <url-pattern>/rss</url-pattern>
</servlet-mapping>

Technology: 

Search