We will be using RSS for achieving the simple news feed application. People interpret the acronym RSS in several ways.
For example, you may hear that RSS stands for Rich Site Summary,
or Really Simple Syndication, or even RDF Site Summary.
Most people simply refer to the technology as RSS. RSS is an XML-based format that allows the syndication
of lists of hyperlinks, along with other information, or metadata,
that helps viewers decide whether they want to follow the link.
RSS allows peoples’ computers to fetch and understand the information, so that all of the lists they’re interested in can be tracked and personalized for them. It is a format that’s intended for use by computers on behalf of people, rather than being directly presented to them (like HTML). To enable this, a Web site will make a feed, or channel, available, just like any other file or resource on the server. Once a feed is available, computers can regularly fetch the file to get the most recent items on the list.
FEED
A feed contains a list of items or entries, each of which is identified by a link. Each item can have any amount of other metadata associated with it as well. A simple example of feed
RSS allows peoples’ computers to fetch and understand the information, so that all of the lists they’re interested in can be tracked and personalized for them. It is a format that’s intended for use by computers on behalf of people, rather than being directly presented to them (like HTML). To enable this, a Web site will make a feed, or channel, available, just like any other file or resource on the server. Once a feed is available, computers can regularly fetch the file to get the most recent items on the list.
FEED
A feed contains a list of items or entries, each of which is identified by a link. Each item can have any amount of other metadata associated with it as well. A simple example of feed
<item>
<title>test</title>
<link>any site which provide data in RSS</link>
<description>description goes here</description>
</item>
---------------------------------------------------------------
Flow of information for RSS
---------------------------------------------------------------
---------------------------------------------------------------
Create the simple news feed application
-----------------------------------------
Create a simple dynamic web project using the eclipse. Now create a new servlet by name RSSServlet
RSSServlet
package com.lnt.web;
import java.io.IOException;
import java.net.URL;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class RSSServlet extends HttpServlet {
private Logger logger = Logger.getLogger(this.getClass());
private RequestDispatcher homeJsp;
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
homeJsp = context.getRequestDispatcher("/WEB-INF/jsp/home.jsp");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
logger.debug("Retrieving yahoo news feed");
URL url = new URL("http://rss.news.yahoo.com/rss/tech");
SyndFeedInput syndFeedInput = new SyndFeedInput();
SyndFeed syndFeed = null;
XmlReader xmlReader = new XmlReader(url);
try {
syndFeed = syndFeedInput.build(xmlReader);
} catch (IllegalArgumentException e) {
logger.error("", e);
} catch (FeedException e) {
logger.error("", e);
}
logger.debug("Forwarding to home.jsp");
req.setAttribute("syndFeed", syndFeed);
homeJsp.forward(req, resp);
}
}
you need to add the below mentioned library to your project.
1. jdom-1.1.3.jar
2. log4j-1.2.14.jar
3. rome-1.0.jar
---------------------------------------------------------------
Create a jsp inside the WEB-INF/jsp folder of the project.
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.sun.syndication.feed.synd.SyndFeed" %>
<%@ page import="com.sun.syndication.feed.synd.SyndEntry" %>
<%@ page import="java.util.Iterator" %>
<jsp:useBean id="syndFeed" scope="request" type="SyndFeed" />
<html>
<head>
<title>website</title>
</head>
<body>
<h1>Home</h1>
<h2><%=syndFeed.getTitle()%></h2>
<ul>
<%
Iterator it = syndFeed.getEntries().iterator();
while (it.hasNext())
{
SyndEntry entry = (SyndEntry) it.next();
%>
<li>
<a href="<%=entry.getLink()%>"><%=entry.getTitle()%></a>
</li>
<% } %>
</ul>
</body>
</html>
Some times the above jsp may show error as undefined type Syndfeed, please ignore the same.
In the deployment Descriptor add the mapping as below.
---------------------------------------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RSStest</display-name>
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>com.lnt.web.RSSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>
---------------------------------------------------------------
Required library
1. jdom-1.1.3.jar
2. log4j-1.2.14.jar
3. rome-1.0.jar
---------------------------------------------------------------
now run the project you should be able to see the news links in your page.
it is not compiling still giving errors or bugs
ReplyDeleteit is not compiling still giving errors or bugs
ReplyDeletei need help
ReplyDeletei need help
ReplyDelete