ShoppingCartClient.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. package sfsbsample;
  2. //package j2eebootcamp.developingEJB.chapter7.web.servlet;
  3. /*
  4.  * ShoppingCartClient.java
  5.  */
  6. import java.util.*;
  7. import java.lang.reflect.*;
  8. import java.io.*;
  9. import java.util.Vector;
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12. import javax.rmi.PortableRemoteObject;
  13. import javax.naming.*;
  14. /*import j2eebootcamp.developingEJB.common.ScheduleVO;
  15. import j2eebootcamp.developingEJB.common.ScheduleDAO;
  16. import j2eebootcamp.developingEJB.common.ScheduleDAOException;
  17. import j2eebootcamp.developingEJB.chapter7.shop.*;*/
  18. //import j2eebootcamp.developingEJB.util.Logger;
  19. /**
  20.  * MVC controller...web tier entry point to ShoppingCart application.
  21.  *
  22.  **/
  23. public class ShoppingCartClient
  24.     extends HttpServlet {
  25.   private ShoppingCart cart;
  26.   private static String LOGIN_SCREEN = "/shop.html";
  27.   ScheduleDAO scheduleDAO;
  28.   public void init(ServletConfig config) throws ServletException {
  29.     super.init(config);
  30.     lookUpShoppingCart();
  31.   }
  32.   public void destroy() {}
  33.   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
  34.       ServletException, java.io.IOException {
  35.     processSearch(request, response);
  36.   }
  37.   protected void doPost(HttpServletRequest request,
  38.                         HttpServletResponse response) throws ServletException,
  39.       java.io.IOException {
  40.     processRequest(request, response);
  41.   }
  42.   public String getServletInfo() {
  43.     return
  44.         "This servlet is the main controller for the ShoppingCart Application";
  45.   }
  46.   /*************************************************************************/
  47.   protected void processSearch(HttpServletRequest request,
  48.                                HttpServletResponse response) throws
  49.       ServletException, java.io.IOException {
  50.     log(" == Entering controller.processRequest()");
  51.     String url = null;
  52.     // retrieve/initialize action and display results in output screen
  53.     String currAction = (request.getParameter("requestAction") != null) ?
  54.         request.getParameter("requestAction") : null;
  55.     log(" == The current action is: " +
  56.         ( (currAction != null) ? currAction : "null"));
  57.     if (currAction == null) {
  58.       // display first screen...login screen
  59.       log(" == currAction is null");
  60.       url = LOGIN_SCREEN;
  61.     }
  62.     else {
  63.       // call the shopping cart functionality -- code goes here
  64.       String scheduleList = "";
  65.       //ScheduleDAO scheduleDAO = new ScheduleDAO();
  66.       String action = request.getParameter("requestAction");
  67.       if (action.equals("SearchByTitle")) {
  68.         log(" == Calling SearchByTitle");
  69.         String searchToken = request.getParameter("searchBy");
  70.         //scheduleList = "SearchByTitle:"+searchToken;
  71.         Vector schedList = new Vector(20);
  72.         try {
  73.           if (scheduleDAO == null) {
  74.             scheduleDAO = new ScheduleDAO();
  75.           }
  76.           schedList = scheduleDAO.searchByCourseTitle(searchToken);
  77.           log(" ==  nafter calling scheduleDAO ");
  78.         }
  79.         catch (ScheduleDAOException se) {
  80.           log(" == SearchByCourseTitle exception  =" + se.getMessage());
  81.         }
  82.         log(" ==  n ****Servlet -- searchByCourseTitle returning Vector ");
  83.         log(" ==  schedList size =>" + schedList.size());
  84.         request.setAttribute("vec", schedList);
  85.         // call the JSP
  86.         RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  87.             "/ShowSearchResult.jsp");
  88.         dispatcher.forward(request, response);
  89.       }
  90.     } //else
  91.   } //end of processSearch
  92. //*****************************************************
  93.    protected void processRequest(HttpServletRequest request,
  94.                                  HttpServletResponse response) throws
  95.        ServletException, java.io.IOException {
  96.      String scheduleList = "";
  97.      if (cart == null) {
  98.        lookUpShoppingCart();
  99.      }
  100.      String action = request.getParameter("requestAction");
  101.      if (action.equals("AddASchedule")) {
  102.        log(" ==  Servlet - Calling addASchedule");
  103.        //int schedID = (new Integer(request.getParameter("ScheduleID"))).intValue();
  104.        scheduleList = "AddASchedule";
  105.        String schedID = request.getParameter("ScheduleID");
  106.        log(" ==  Serlvet - selected sched id =>" + schedID);
  107.        // retrieve the schedule from the ScheduleDAO and then send the scheduleVO to the EJB.
  108.        try {
  109.          if (scheduleDAO == null) {
  110.            scheduleDAO = new ScheduleDAO();
  111.          }
  112.          ScheduleVO scheduleVO = scheduleDAO.searchByScheduleID(schedID);
  113.          log(" ==  Servlet - after searchByScheduleID() ");
  114.          cart.addASchedule(scheduleVO);
  115.          log(" ==  n ***Servlet added a schedule to the cart **** ");
  116.          Vector schedList = cart.getMyScheduleList();
  117.          log(" ==  **** Servlet schedulelist size =" + schedList.size());
  118.          //String cost = (new Double(cart.getTotalCost())).toString();
  119.          //log(" ==  **** servlet  cost ="+cost);
  120.          request.setAttribute("vec", schedList);
  121.          //request.setAttribute("cost", cost);
  122.        }
  123.        catch (ScheduleDAOException se) {
  124.          log(" ==  Servlet - processRequest =" + se.getMessage());
  125.        }
  126.        catch (Exception e) {
  127.          log(" ==  Servlet AddSchedule() Exception " + e.getMessage());
  128.        }
  129.        // call the JSP
  130.        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  131.            "/ShowShoppingCart.jsp");
  132.        dispatcher.forward(request, response);
  133.      }
  134.      else if (action.equals("DeleteASchedule")) {
  135.        log(" == Calling deleteASchedule");
  136.        scheduleList = "deleteASchedule";
  137.        String schedID = request.getParameter("ScheduleID");
  138.        cart.deleteASchedule(schedID);
  139.        Vector schedList = cart.getMyScheduleList();
  140.        //String cost = (new Double(cart.getTotalCost())).toString();
  141.        request.setAttribute("vec", schedList);
  142.        //request.setAttribute("cost", cost);
  143.        // call the JSP
  144.        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  145.            "/ShowShoppingCart.jsp");
  146.        dispatcher.forward(request, response);
  147.      }
  148.      else if (action.equals("EmptyMyScheduleList")) {
  149.        log(" == calling emptyMyScheduleList");
  150.        scheduleList = "emtpyMyScheduleList";
  151.        cart.emptyMyScheduleList();
  152.        Vector schedList = cart.getMyScheduleList();
  153.        //String cost = (new Double(cart.getTotalCost())).toString();
  154.        request.setAttribute("vec", schedList);
  155.        //request.setAttribute("cost", cost);
  156.        // call the JSP
  157.        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  158.            "/ShowShoppingCart.jsp");
  159.        dispatcher.forward(request, response);
  160.      }
  161.      else if (action.equals("CheckOut")) {
  162.        log(" == n --- Checking Out -- before jsp -- say Thank You --");
  163.        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
  164.            "/ThankYou.jsp");
  165.        dispatcher.forward(request, response);
  166.        log(" ==  servlet  CheckOut after calling jsp ");
  167.      }
  168.      else {
  169.        log(" == invalid request");
  170.        scheduleList = "invalid request";
  171.      }
  172.    } // end of processRequest()
  173.   private void lookUpShoppingCart() {
  174.     log(" == Entering Controller.lookUpShoppingCart()");
  175.     try {
  176.       //create the context, do a lookup home object
  177.       Context ctx = new InitialContext();
  178.       Object result = ctx.lookup("ShoppingCart");
  179.       ShoppingCartHome cartHome =
  180.           (ShoppingCartHome) javax.rmi.PortableRemoteObject.
  181.           narrow(result, ShoppingCartHome.class);
  182.       //create an instance of the home.
  183.       cart = cartHome.create();
  184.     }
  185.     catch (Exception e) {
  186.       System.out.println(e);
  187.     }
  188.     log(" == Leaving Controller.lookUpShoppingCart()");
  189.   }
  190. } // end of controller