UpdateNewsItemsServlet.java
上传用户:shen332233
上传日期:2021-09-03
资源大小:7478k
文件大小:2k
源码类别:

Ajax

开发平台:

Java

  1. package ajaxdashboard.servlet;
  2. import ajaxdashboard.domain.NewsItem;
  3. import ajaxdashboard.service.NewsService;
  4. import java.io.*;
  5. import java.util.Collection;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import javax.servlet.*;
  9. import javax.servlet.http.*;
  10. public class UpdateNewsItemsServlet extends HttpServlet {
  11.     private static final String AJAX_CONTENT = "/jsp/news/newsItemsAjax.jsp";
  12.     
  13.     protected void processRequest(HttpServletRequest request
  14.                                             , HttpServletResponse response)
  15.                                             throws ServletException, IOException {
  16.         Collection newsItems = new NewsService().getNewsItems();
  17.         
  18.         cleanLinks(newsItems);
  19.         
  20.         request.setAttribute("newsItems", newsItems);
  21.         
  22.         System.out.println("nn========nUpdating news items at: " 
  23.                                                         + new Date().toString());
  24.         
  25.         request.getRequestDispatcher(AJAX_CONTENT).forward(request, response);
  26.     }
  27.     
  28.     private void cleanLinks(Collection newsItems) {
  29.         NewsItem newsItem = null;
  30.         for(Iterator it = newsItems.iterator(); it.hasNext();) {
  31.             newsItem = (NewsItem)it.next();
  32.             newsItem.setLink(newsItem.getLink().replaceAll("&", "&"));
  33. //            System.out.println("Link: " + newsItem.getLink());
  34.         }
  35.     }
  36.     
  37.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  38.     throws ServletException, IOException {
  39.         processRequest(request, response);
  40.     }
  41.     
  42.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  43.     throws ServletException, IOException {
  44.         processRequest(request, response);
  45.     }
  46. }