Feed Icon Bl-Or

Rome framework is one of the most popular APIs for creating and reading rss feeds.In this tutorial we will see how to implement an rss feed stream with feeds created from database entries. As an example for this tutorial the rss stream will consist of the tutorials of java-only.comThe first step will be to create a servlet where the RSS stream and the feed entries will be created and initialized.In this case the feed entries will the result set from the database.

package com.javaonly.rss;

import com.javaonly.Daos.TutorialDao;
import com.javaonly.hibernate.HibernateUtil;
import com.javaonly.model.Tutorial;
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.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RssServlet extends HttpServlet {

 private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");
    private String defaultType;

    public void init() {
        defaultType = getServletConfig().getInitParameter(default.feed.type);
        defaultType = (defaultType != null) ? defaultType : "atom_0.3";
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        try {
            SyndFeed feed = getFeed(request);

            String feedType = request.getParameter("type");
            feedType = (feedType!=null) ? feedType : _defaultFeedType;

            response.setContentType("application/xml; charset=UTF-8");
            SyndFeedOutput output = new SyndFeedOutput();
        catch (FeedException ex) {
            String msg ="Could not generate feed";

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    protected void doPost(HttpServletRequest request,
 HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    public String getServletInfo() {
        return "Short description";

    protected SyndFeed getFeed(HttpServletRequest request) {
        TutorialDao tutorialDao = new TutorialDao();
        List<Tutorial> tutorials = tutorialDao.getAll();
    //create the rss feed and set the Title the link to the site and a small descreption
        SyndFeed feed = new SyndFeedImpl();
        feed.setTitle("JAVA ONLY: tutorials and resources for Java");
        feed.setDescription("Tutorials and resources for Java");

        //create the feeds.Each tutorial will be a feed entry
        List entries = new ArrayList();
        for (Tutorial tutorial : tutorials) {
            SyndEntry entry = entry = new SyndEntryImpl();
            SyndContent description;
            String title = tutorial.getTitle();
 String link ="";
 link ="http://http://latest-tutorial.pakifreelancer.com/?p=" + tutorial.getId();
            try {
                //set published Date
            } catch (ParseException ex) {
            //Create the description of the feed entry
            description = new SyndContentImpl();
        return feed;

Finally we have to configure Rome in the web.xml of the application.So in the web xml we will add the following snippet:



One thought on “How to make RSS Feed in Java

Leave a Reply

Your email address will not be published. Required fields are marked *