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

Java编程

开发平台:

Java

  1. package web;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import javax.naming.*;
  7. import day21ex.studentfacade.*;
  8. import day21ex.adminfacade.*;
  9. public class URSControllerServlet extends HttpServlet {
  10.    private StudentFacadeHome studentFacadeHome;
  11.    private AdminFacadeHome   adminFacadeHome;
  12.    public void init(ServletConfig config) throws ServletException {
  13.       System.out.println("URS SERVLET INIT");
  14.       super.init(config);
  15.       try {
  16.          Context ctx = new InitialContext(); 
  17.          studentFacadeHome = (StudentFacadeHome) 
  18.             ctx.lookup("day21ex/StudentFacade");
  19.          adminFacadeHome = (AdminFacadeHome) 
  20.             ctx.lookup("day21ex/AdminFacade");
  21.       }
  22.       catch (Exception e) {
  23.          throw new ServletException(e.toString());
  24.       }
  25.    }
  26.    public void service(HttpServletRequest request,
  27.                        HttpServletResponse response)
  28.       throws ServletException, IOException {
  29.       HttpSession session = request.getSession(true);
  30.       String pageName = request.getParameter("pageName");
  31.       System.out.println("pageName:" + pageName);
  32.       if ( pageName != null ) {
  33.          if ( pageName.equals("loginPage" )) {
  34.             processLoginPage(request, response, session);
  35.          } else if ( pageName.equals("registrationPage" )) {
  36.             processRegistrationPage(request, response, session);
  37.          } else if ( pageName.equals("catalogPage" )) {
  38.             processCatalogPage(request, response, session);
  39.          } else if ( pageName.equals("orderPage" )) {
  40.             processOrderPage(request, response, session);
  41.          }  else if ( pageName.equals("adminPage" )) {
  42.             processAdminPage(request, response, session);
  43.          }  else if ( pageName.equals("enrollmentPage" )) {
  44.             /* day 21 exercise */
  45.             processEnrollmentPage(request, response, session);
  46.          }
  47.       }
  48.    }
  49.    void processLoginPage(HttpServletRequest request,
  50.                          HttpServletResponse response,
  51.                          HttpSession session
  52.       ) 
  53.       throws ServletException, IOException {
  54.       String loginName = request.getParameter("loginName");
  55.       String password = request.getParameter("password");
  56.       System.out.println("LoginName:" + loginName + " Password:" + password);
  57.       StudentFacade studentFacade = null;
  58.       if ( loginName != null ) {    
  59.          try {
  60.             System.out.println("Login name:" + loginName);
  61.             studentFacade = (StudentFacade) studentFacadeHome.create();
  62.             if ( studentFacade.validateUser(loginName, password) == true ) {
  63.                session.putValue("StudentFacade", studentFacade);
  64.                session.setAttribute("courseItemList", 
  65.                                     studentFacade.getCourseItemList());
  66.                forwardToPage(request, response, "/catalog.jsp");
  67.             }
  68.             else {
  69.                studentFacade.remove();
  70.                request.setAttribute("InvalidLogin", "1");
  71.                forwardToPage(request, response, "/login.jsp");
  72.             }
  73.          } catch ( Exception e) {
  74.             System.err.println(e.toString());
  75.             throw new ServletException(e.toString());
  76.          }
  77.       }
  78.       else {
  79.          forwardToPage(request, response, "/login.jsp");
  80.       }
  81.    }
  82.    void processRegistrationPage(HttpServletRequest request,
  83.                          HttpServletResponse response,
  84.                          HttpSession session
  85.       ) 
  86.       throws ServletException, IOException {
  87.       String loginName = request.getParameter("loginName");
  88.       String password = request.getParameter("password");
  89.       String firstName = request.getParameter("firstName");
  90.       String lastName = request.getParameter("lastName");
  91.       String address = request.getParameter("address");
  92.       String emailAddress = request.getParameter("emailAddress");
  93.       System.out.println("LoginName:" + loginName + " Password:" + password);
  94.       System.out.println("firstName:" + firstName + " lastName:" + lastName);
  95.       System.out.println("address:" + address + " emailAddress:" + emailAddress);
  96.       StudentFacade studentFacade = null;
  97.       if ( loginName != null ) {    
  98.          try {
  99.             studentFacade = (StudentFacade) studentFacadeHome.create();
  100.             studentFacade.addStudent(loginName, password, firstName,
  101.                                      lastName, address, emailAddress);
  102.             session.putValue("StudentFacade", studentFacade);
  103.             session.setAttribute("courseItemList", 
  104.                                  studentFacade.getCourseItemList());
  105.             forwardToPage(request, response, "/catalog.jsp");
  106.          } catch ( Exception e) {
  107.             System.err.println(e.toString());
  108.             throw new ServletException(e.toString());
  109.          }
  110.       }
  111.       else {
  112.          forwardToPage(request, response, "/registration.jsp");
  113.       }
  114.    }
  115.    void processCatalogPage(HttpServletRequest request,
  116.                            HttpServletResponse response,
  117.                            HttpSession session
  118.       ) 
  119.       throws ServletException, IOException {
  120.       try {
  121.          StudentFacade studentFacade = 
  122.             (StudentFacade)session.getValue("StudentFacade");
  123.          // correct the following
  124.          String courseId = request.getParameter("buy");
  125.          if ( courseId != null ) {
  126.             String[] courseIds = new String[1];
  127.             courseIds[0] =  courseId;
  128.             studentFacade.addCourses(courseIds);
  129.             session.setAttribute("cartItemList", 
  130.                                  studentFacade.getCartItems());
  131.          }
  132.        
  133.          forwardToPage(request, response, "/catalog.jsp");           
  134.          
  135.       } catch ( Exception e) {
  136.          e.printStackTrace();
  137.          System.err.println(e.toString());
  138.          throw new ServletException(e.toString());
  139.       }
  140.    }
  141.    void processOrderPage(HttpServletRequest request,
  142.                          HttpServletResponse response,
  143.                          HttpSession session
  144.       ) 
  145.       throws ServletException, IOException {
  146.       try {
  147.          StudentFacade studentFacade = 
  148.             (StudentFacade)session.getValue("StudentFacade");
  149.          studentFacade.placeOrder();
  150.          forwardToPage(request, response, "/order.jsp");           
  151.          
  152.       } catch ( Exception e) {
  153.          System.err.println(e.toString());
  154.          throw new ServletException(e.toString());
  155.       }
  156.    }
  157.    void processAdminPage(HttpServletRequest request,
  158.                          HttpServletResponse response,
  159.                          HttpSession session
  160.       ) 
  161.       throws ServletException, IOException {
  162.       try {
  163.          AdminFacade adminFacade = 
  164.             (AdminFacade)session.getValue("AdminFacade");
  165.          if ( adminFacade == null ) {
  166.             adminFacade = (AdminFacade)adminFacadeHome.create();
  167.             session.putValue("AdminFacade", adminFacade);
  168.          }
  169.          String orderId = request.getParameter("orderId");      
  170.          if ( orderId != null ) {
  171.             adminFacade.approveOrder(orderId);
  172.          }
  173.          /* get pending orders */
  174.          session.setAttribute("orderToApproveList", 
  175.                               adminFacade.getOrdersByStatus("Verified"));
  176.          forwardToPage(request, response, "/admin.jsp");           
  177.          
  178.       } catch ( Exception e) {
  179.          System.err.println(e.toString());
  180.          throw new ServletException(e.toString());
  181.       }
  182.    }
  183.    /* day 21 exercise */
  184.    void processEnrollmentPage(HttpServletRequest request,
  185.                          HttpServletResponse response,
  186.                          HttpSession session
  187.       ) 
  188.       throws ServletException, IOException {
  189.       try {
  190.          StudentFacade studentFacade = 
  191.             (StudentFacade)session.getValue("StudentFacade");
  192.          session.setAttribute("enrollmentsList", 
  193.                               studentFacade.getStudentEnrollments());
  194.          forwardToPage(request, response, "/enrollment.jsp");           
  195.          
  196.       } catch ( Exception e) {
  197.          e.printStackTrace();
  198.          System.err.println(e.toString());
  199.          throw new ServletException(e.toString());
  200.       }
  201.    }
  202.    void forwardToPage(HttpServletRequest request,
  203.                       HttpServletResponse response, String page) 
  204.       throws ServletException, IOException {
  205.       RequestDispatcher disp =
  206.          this.getServletContext().getRequestDispatcher(page);
  207.       disp.forward(request, response);
  208.    }
  209. }