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

Ajax

开发平台:

Java

  1. package ajaxdashboard.servlet;
  2. import ajaxdashboard.Constants;
  3. import ajaxdashboard.service.NewsService;
  4. import ajaxdashboard.service.StockQuoteService;
  5. import ajaxdashboard.service.WeatherForecastService;
  6. import ajaxdashboard.ws.stockquote.GetQuotesResponse;
  7. import ajaxdashboard.ws.stockquote.Quote;
  8. import java.io.*;
  9. import java.util.Collection;
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12. public class DashboardServlet extends HttpServlet {
  13.     
  14.     protected void processRequest(HttpServletRequest request
  15.                                                 , HttpServletResponse response)
  16.     throws ServletException, IOException {
  17.         response.setContentType("text/html;charset=UTF-8");
  18.         getWeatherForecast(request, response);
  19.         
  20.         getStockQuotes(request, response);
  21.         
  22.         getNewsItems(request, response);
  23.         
  24.         request.getRequestDispatcher("/jsp/ajaxDashboard.jsp")
  25.                                                 .forward(request, response);
  26.     }
  27.     
  28.     private void getWeatherForecast(HttpServletRequest request
  29.             , HttpServletResponse response) throws ServletException, IOException {
  30.         
  31.         WeatherForecastService forecastService = new WeatherForecastService();
  32.         request.setAttribute("forecastData"
  33.             , forecastService.getForecastFor(Constants.DEFAULT_WEATHER_ZIP_CODE));
  34.         
  35.     }
  36.     
  37.     private void getStockQuotes(HttpServletRequest request
  38.             , HttpServletResponse response) throws ServletException, IOException {
  39.         GetQuotesResponse quotesResponse = 
  40.                 new StockQuoteService()
  41.                     .getStockQuotesFor(Constants.DEFAULT_STOCK_TICKERS);
  42.         
  43.         Quote[] quotesArray = quotesResponse.getGetQuotesResult().getQuote();
  44.         
  45.         request.setAttribute("quotesArray", quotesArray);
  46.     }
  47.     
  48.     private void getNewsItems(HttpServletRequest request
  49.             , HttpServletResponse response) throws ServletException, IOException {
  50.         
  51.         Collection newsItems = new NewsService().getNewsItems();
  52.         
  53.         request.setAttribute("newsItems", newsItems);
  54.     }
  55.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  56.     throws ServletException, IOException {
  57.         processRequest(request, response);
  58.     }
  59.     
  60.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  61.     throws ServletException, IOException {
  62.         processRequest(request, response);
  63.     }
  64. }