shoppingcartclient.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 249 行

JAVA
249
字号
package sfsbsample;

//package j2eebootcamp.developingEJB.chapter7.web.servlet;

/*
 * ShoppingCartClient.java
 */

import java.util.*;
import java.lang.reflect.*;
import java.io.*;
import java.util.Vector;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;

/*import j2eebootcamp.developingEJB.common.ScheduleVO;
import j2eebootcamp.developingEJB.common.ScheduleDAO;
import j2eebootcamp.developingEJB.common.ScheduleDAOException;
import j2eebootcamp.developingEJB.chapter7.shop.*;*/

//import j2eebootcamp.developingEJB.util.Logger;
/**
 * MVC controller...web tier entry point to ShoppingCart application.
 *
 **/

public class ShoppingCartClient
    extends HttpServlet {

  private ShoppingCart cart;

  private static String LOGIN_SCREEN = "/shop.html";

  ScheduleDAO scheduleDAO;

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    lookUpShoppingCart();
  }

  public void destroy() {}

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, java.io.IOException {
    processSearch(request, response);
  }

  protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException,
      java.io.IOException {
    processRequest(request, response);
  }

  public String getServletInfo() {
    return
        "This servlet is the main controller for the ShoppingCart Application";
  }

  /*************************************************************************/

  protected void processSearch(HttpServletRequest request,
                               HttpServletResponse response) throws
      ServletException, java.io.IOException {

    log(" == Entering controller.processRequest()");
    String url = null;

    // retrieve/initialize action and display results in output screen
    String currAction = (request.getParameter("requestAction") != null) ?
        request.getParameter("requestAction") : null;
    log(" == The current action is: " +
        ( (currAction != null) ? currAction : "null"));

    if (currAction == null) {
      // display first screen...login screen
      log(" == currAction is null");
      url = LOGIN_SCREEN;
    }
    else {
      // call the shopping cart functionality -- code goes here
      String scheduleList = "";

      //ScheduleDAO scheduleDAO = new ScheduleDAO();

      String action = request.getParameter("requestAction");
      if (action.equals("SearchByTitle")) {
        log(" == Calling SearchByTitle");

        String searchToken = request.getParameter("searchBy");
        //scheduleList = "SearchByTitle:"+searchToken;

        Vector schedList = new Vector(20);

        try {
          if (scheduleDAO == null) {
            scheduleDAO = new ScheduleDAO();

          }
          schedList = scheduleDAO.searchByCourseTitle(searchToken);
          log(" ==  \nafter calling scheduleDAO ");

        }
        catch (ScheduleDAOException se) {
          log(" == SearchByCourseTitle exception  =" + se.getMessage());
        }

        log(" ==  \n ****Servlet -- searchByCourseTitle returning Vector ");
        log(" ==  schedList size =>" + schedList.size());

        request.setAttribute("vec", schedList);
        // call the JSP

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
            "/ShowSearchResult.jsp");
        dispatcher.forward(request, response);
      }
    } //else
  } //end of processSearch

//*****************************************************

   protected void processRequest(HttpServletRequest request,
                                 HttpServletResponse response) throws
       ServletException, java.io.IOException {
     String scheduleList = "";

     if (cart == null) {
       lookUpShoppingCart();

     }
     String action = request.getParameter("requestAction");

     if (action.equals("AddASchedule")) {
       log(" ==  Servlet - Calling addASchedule");

       //int schedID = (new Integer(request.getParameter("ScheduleID"))).intValue();
       scheduleList = "AddASchedule";
       String schedID = request.getParameter("ScheduleID");
       log(" ==  Serlvet - selected sched id =>" + schedID);

       // retrieve the schedule from the ScheduleDAO and then send the scheduleVO to the EJB.
       try {
         if (scheduleDAO == null) {
           scheduleDAO = new ScheduleDAO();

         }
         ScheduleVO scheduleVO = scheduleDAO.searchByScheduleID(schedID);
         log(" ==  Servlet - after searchByScheduleID() ");

         cart.addASchedule(scheduleVO);

         log(" ==  \n ***Servlet added a schedule to the cart **** ");

         Vector schedList = cart.getMyScheduleList();
         log(" ==  **** Servlet schedulelist size =" + schedList.size());

         //String cost = (new Double(cart.getTotalCost())).toString();
         //log(" ==  **** servlet  cost ="+cost);

         request.setAttribute("vec", schedList);
         //request.setAttribute("cost", cost);

       }
       catch (ScheduleDAOException se) {
         log(" ==  Servlet - processRequest =" + se.getMessage());
       }
       catch (Exception e) {
         log(" ==  Servlet AddSchedule() Exception " + e.getMessage());
       }

       // call the JSP
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
           "/ShowShoppingCart.jsp");
       dispatcher.forward(request, response);

     }

     else if (action.equals("DeleteASchedule")) {
       log(" == Calling deleteASchedule");
       scheduleList = "deleteASchedule";
       String schedID = request.getParameter("ScheduleID");
       cart.deleteASchedule(schedID);

       Vector schedList = cart.getMyScheduleList();
       //String cost = (new Double(cart.getTotalCost())).toString();

       request.setAttribute("vec", schedList);
       //request.setAttribute("cost", cost);
       // call the JSP
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
           "/ShowShoppingCart.jsp");
       dispatcher.forward(request, response);

     }
     else if (action.equals("EmptyMyScheduleList")) {
       log(" == calling emptyMyScheduleList");
       scheduleList = "emtpyMyScheduleList";
       cart.emptyMyScheduleList();

       Vector schedList = cart.getMyScheduleList();
       //String cost = (new Double(cart.getTotalCost())).toString();
       request.setAttribute("vec", schedList);
       //request.setAttribute("cost", cost);
       // call the JSP
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
           "/ShowShoppingCart.jsp");
       dispatcher.forward(request, response);

     }
     else if (action.equals("CheckOut")) {
       log(" == \n --- Checking Out -- before jsp -- say Thank You --");
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
           "/ThankYou.jsp");
       dispatcher.forward(request, response);
       log(" ==  servlet  CheckOut after calling jsp ");

     }

     else {
       log(" == invalid request");
       scheduleList = "invalid request";
     }

   } // end of processRequest()

  private void lookUpShoppingCart() {
    log(" == Entering Controller.lookUpShoppingCart()");
    try {
      //create the context, do a lookup home object
      Context ctx = new InitialContext();
      Object result = ctx.lookup("ShoppingCart");
      ShoppingCartHome cartHome =
          (ShoppingCartHome) javax.rmi.PortableRemoteObject.
          narrow(result, ShoppingCartHome.class);

      //create an instance of the home.
      cart = cartHome.create();
    }
    catch (Exception e) {
      System.out.println(e);
    }
    log(" == Leaving Controller.lookUpShoppingCart()");
  }

} // end of controller

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?