rome.properties(和上面的两个类在一个路径下)
datetime.extra.masks=yyyy-MM-dd HH:mm:ss|yyyy-MM-dd HH:mm
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>RssModule</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<display-name>FeedServlet</display-name>
<servlet-name>FeedServlet</servlet-name>
<servlet-class>net.blogjava.rss.FeedServlet</servlet-class>
<init-param>
<param-name>default.feed.type</param-name>
<!--   <param-value>rss_2.0</param-value>
-->
<param-value>atom_1.0</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FeedServlet</servlet-name>
<url-pattern>/feed</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Refresh" content="0; url=feed.jsp"> 
<title>Feed</title>
</head>
<body></body>
</html>
feed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page
import="java.net.*,com.sun.syndication.feed.synd.*,com.sun.syndication.io.*,java.text.SimpleDateFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RSS订阅</title>
</head>
<body>
<table border=1>
<caption>
<h2>RSS</h2>
</caption>
<tbody>
<%
URLConnection feedUrl = new URL(
"http://news.baidu.com/n?cmd=4&class=netgames&tn=rss")
.openConnection();
feedUrl.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
//System.out.println(feed.getEntries());
int entriesSize = feed.getEntries().size();
//System.out.println("News:" + entriesSize + " FeedType:"
// + feed.getFeedType());
SyndEntry entry;
SyndContent description;
out.print("<TR><TD>" +feed.getTitle() +"</TD></TR>");
for (int i = 0; i < entriesSize; i++) {
entry = new SyndEntryImpl();
entry = (SyndEntry) (feed.getEntries()).get(i);
description = new SyndContentImpl();
description = entry.getDescription();
out.print("<TR><TABLE BORDER=1><TR>");
out.print("<TD>" + (i + 1) + ". " + "<a href=\""
+ entry.getLink() + "\" />" + entry.getTitle().trim()
+ "</a>   作者:" + entry.getAuthor().trim());
out.print(entry.getPublishedDate() != null ? "   日期:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(entry.getPublishedDate())
+ "</TD></TR></TABLE></TR>"
: "</TD></TR></TABLE></TR>");
out.print("<TR><TD>" + description.getValue().trim()
+ "</TD></TR>");
}

%>
</tbody>
</table>
</body>
</html>

解决方案 »

  1.   

    package cn.com.augmentum;import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import cn.com.augmentum.util.db.ConnectionMySql;import com.sun.syndication.feed.atom.Content;
    import com.sun.syndication.feed.atom.Entry;
    import com.sun.syndication.feed.atom.Feed;
    import com.sun.syndication.feed.atom.Link;
    import com.sun.syndication.io.FeedException;
    import com.sun.syndication.io.WireFeedOutput;/**
     * Servlet implementation class FeedServlet
     */
    public class FeedServlet extends HttpServlet {
    private static final long serialVersionUID = 1L; private static final String DEFAULT_FEED_TYPE = "default.feed.type"; private static final String FEED_TYPE = "type"; private static final String MIME_TYPE = "application/xml; charset=UTF-8"; private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed error!"; /*private static final DateFormat DATE_PARSER = new SimpleDateFormat(
    "yyyy-MM-dd");*/ private String _defaultFeedType;
    private Connection conn = null;

    private Statement stmt = null;

    private ResultSet rs = null;
    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
    _defaultFeedType = config.getInitParameter(DEFAULT_FEED_TYPE);
    _defaultFeedType = (_defaultFeedType != null) ? _defaultFeedType
    : "atom_1.0";
    } /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws IOException {
    try {
    Feed feed = getFeed(request);
    String feedType = request.getParameter(FEED_TYPE);
    feedType = (feedType != null) ? (checkFeedType(feedType) ? feedType
    : _defaultFeedType) : _defaultFeedType;
    feed.setFeedType(feedType);
    response.setContentType(MIME_TYPE);
    WireFeedOutput output = new WireFeedOutput();
    output.output(feed, response.getWriter());
    System.out.println(output.outputString(feed));
    } catch (FeedException e) {
    String msg = COULD_NOT_GENERATE_FEED_ERROR;
    log(msg, e);
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
    msg);
    }
    } protected Feed getFeed(HttpServletRequest req) throws IOException,
    FeedException {
    Feed feed = new Feed();
    String queryString = "select * from job";
    conn = ConnectionMySql.connection();

    //add basci infomation
    feed.setTitle("RSS订阅内容");
    List<Link> links = null;
    links = new ArrayList<Link>();
    Link link = new Link();
    link.setHref("http://www.baidu.com");
    links.add(link);
    feed.setAlternateLinks(links); //add content
    Content content = null;
    content = new Content();
    content.setType("text/html");
    content.setValue("百度RSS订阅中心");
    feed.setInfo(content);

    //add Category
    /*List<Category> categoryList = new ArrayList<Category>();
    Category category = null;
    category = new Category();
    category.setLabel("11111");
    categoryList.add(category);
    feed.setCategories(categoryList);*/

    List<Entry> entries = new ArrayList<Entry>();
    Entry entry = null;

    try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(queryString);
    List<Content> contentList = null;
    Link entryLink = null;
    while(rs.next()){
    //add Entry(Item)
    entry = new Entry();
    entry.setId(rs.getString("job_up_date"));
    entry.setTitle(rs.getString("job_title"));

    links = new ArrayList<Link>();
    entryLink = new Link();
    entryLink.setHref("http://wiki.java.net/bin/view/Javawsxml/rome01");
    links.add(entryLink);
    entry.setAlternateLinks(links);
    entry.setPublished(new Date());

    contentList = new ArrayList<Content>();
    content = new Content();
    content.setType("text/html");
    content.setValue("相关描述:"+rs.getString("job_compny_name"));
    contentList.add(content);
    entry.setContents(contentList);

    entries.add(entry);
    }
    feed.setEntries(entries);
    } catch (SQLException e) {
    e.printStackTrace();
    try {
    rs.close();
    stmt.close();
    conn.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
    }
    return feed;
    } private boolean checkFeedType(String feedType) {
    boolean isFeedType = false;
    if (feedType.equals("rss_2.0") || feedType.equals("atom_1.0")) {
    isFeedType = true;
    }
    return isFeedType;
    }
    }