URSControllerServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:9k
源码类别:
Java编程
开发平台:
Java
- package web;
- import java.io.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.naming.*;
- import day21ex.studentfacade.*;
- import day21ex.adminfacade.*;
- public class URSControllerServlet extends HttpServlet {
- private StudentFacadeHome studentFacadeHome;
- private AdminFacadeHome adminFacadeHome;
- public void init(ServletConfig config) throws ServletException {
- System.out.println("URS SERVLET INIT");
- super.init(config);
- try {
- Context ctx = new InitialContext();
- studentFacadeHome = (StudentFacadeHome)
- ctx.lookup("day21ex/StudentFacade");
- adminFacadeHome = (AdminFacadeHome)
- ctx.lookup("day21ex/AdminFacade");
- }
- catch (Exception e) {
- throw new ServletException(e.toString());
- }
- }
- public void service(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- HttpSession session = request.getSession(true);
- String pageName = request.getParameter("pageName");
- System.out.println("pageName:" + pageName);
- if ( pageName != null ) {
- if ( pageName.equals("loginPage" )) {
- processLoginPage(request, response, session);
- } else if ( pageName.equals("registrationPage" )) {
- processRegistrationPage(request, response, session);
- } else if ( pageName.equals("catalogPage" )) {
- processCatalogPage(request, response, session);
- } else if ( pageName.equals("orderPage" )) {
- processOrderPage(request, response, session);
- } else if ( pageName.equals("adminPage" )) {
- processAdminPage(request, response, session);
- } else if ( pageName.equals("enrollmentPage" )) {
- /* day 21 exercise */
- processEnrollmentPage(request, response, session);
- }
- }
- }
- void processLoginPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- String loginName = request.getParameter("loginName");
- String password = request.getParameter("password");
- System.out.println("LoginName:" + loginName + " Password:" + password);
- StudentFacade studentFacade = null;
- if ( loginName != null ) {
- try {
- System.out.println("Login name:" + loginName);
- studentFacade = (StudentFacade) studentFacadeHome.create();
- if ( studentFacade.validateUser(loginName, password) == true ) {
- session.putValue("StudentFacade", studentFacade);
- session.setAttribute("courseItemList",
- studentFacade.getCourseItemList());
- forwardToPage(request, response, "/catalog.jsp");
- }
- else {
- studentFacade.remove();
- request.setAttribute("InvalidLogin", "1");
- forwardToPage(request, response, "/login.jsp");
- }
- } catch ( Exception e) {
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- else {
- forwardToPage(request, response, "/login.jsp");
- }
- }
- void processRegistrationPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- String loginName = request.getParameter("loginName");
- String password = request.getParameter("password");
- String firstName = request.getParameter("firstName");
- String lastName = request.getParameter("lastName");
- String address = request.getParameter("address");
- String emailAddress = request.getParameter("emailAddress");
- System.out.println("LoginName:" + loginName + " Password:" + password);
- System.out.println("firstName:" + firstName + " lastName:" + lastName);
- System.out.println("address:" + address + " emailAddress:" + emailAddress);
- StudentFacade studentFacade = null;
- if ( loginName != null ) {
- try {
- studentFacade = (StudentFacade) studentFacadeHome.create();
- studentFacade.addStudent(loginName, password, firstName,
- lastName, address, emailAddress);
- session.putValue("StudentFacade", studentFacade);
- session.setAttribute("courseItemList",
- studentFacade.getCourseItemList());
- forwardToPage(request, response, "/catalog.jsp");
- } catch ( Exception e) {
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- else {
- forwardToPage(request, response, "/registration.jsp");
- }
- }
- void processCatalogPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- try {
- StudentFacade studentFacade =
- (StudentFacade)session.getValue("StudentFacade");
- // correct the following
- String courseId = request.getParameter("buy");
- if ( courseId != null ) {
- String[] courseIds = new String[1];
- courseIds[0] = courseId;
- studentFacade.addCourses(courseIds);
- session.setAttribute("cartItemList",
- studentFacade.getCartItems());
- }
- forwardToPage(request, response, "/catalog.jsp");
- } catch ( Exception e) {
- e.printStackTrace();
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- void processOrderPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- try {
- StudentFacade studentFacade =
- (StudentFacade)session.getValue("StudentFacade");
- studentFacade.placeOrder();
- forwardToPage(request, response, "/order.jsp");
- } catch ( Exception e) {
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- void processAdminPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- try {
- AdminFacade adminFacade =
- (AdminFacade)session.getValue("AdminFacade");
- if ( adminFacade == null ) {
- adminFacade = (AdminFacade)adminFacadeHome.create();
- session.putValue("AdminFacade", adminFacade);
- }
- String orderId = request.getParameter("orderId");
- if ( orderId != null ) {
- adminFacade.approveOrder(orderId);
- }
- /* get pending orders */
- session.setAttribute("orderToApproveList",
- adminFacade.getOrdersByStatus("Verified"));
- forwardToPage(request, response, "/admin.jsp");
- } catch ( Exception e) {
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- /* day 21 exercise */
- void processEnrollmentPage(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session
- )
- throws ServletException, IOException {
- try {
- StudentFacade studentFacade =
- (StudentFacade)session.getValue("StudentFacade");
- session.setAttribute("enrollmentsList",
- studentFacade.getStudentEnrollments());
- forwardToPage(request, response, "/enrollment.jsp");
- } catch ( Exception e) {
- e.printStackTrace();
- System.err.println(e.toString());
- throw new ServletException(e.toString());
- }
- }
- void forwardToPage(HttpServletRequest request,
- HttpServletResponse response, String page)
- throws ServletException, IOException {
- RequestDispatcher disp =
- this.getServletContext().getRequestDispatcher(page);
- disp.forward(request, response);
- }
- }