1 package example.cmp.transaction; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 6 import javax.naming.*; 7 import javax.ejb.*; 8 import java.util.*; 9 import java.io.PrintWriter; 10 11 import com.caucho.server.security.ServletAuthenticator; 12 13 17 public class RegistrationServlet extends HttpServlet { 18 19 public static final String NEXT_PAGE = "/examples/cmp-courses.xtp"; 21 22 public static final String REGISTRATION_SESSION_NAME = "reg_session"; 24 25 private RegistrationSessionHome _registrationSessionHome = null; 27 28 private CourseHome _courseHome = null; 30 31 private StudentHome _studentHome = null; 33 34 37 public void setCourseHome(CourseHome courseHome) 38 { 39 _courseHome = courseHome; 40 } 41 42 45 public void setStudentHome(StudentHome studentHome) 46 { 47 _studentHome = studentHome; 48 } 49 50 53 public void setSessionHome(RegistrationSessionHome registrationHome) 54 { 55 _registrationSessionHome = registrationHome; 56 } 57 58 61 public void init() 62 throws ServletException 63 { 64 try { 65 Context ic = new InitialContext(); 66 67 Context cmp = (Context) ic.lookup("java:comp/env/cmp"); 69 70 if (_registrationSessionHome == null) { 71 _registrationSessionHome = (RegistrationSessionHome) 73 cmp.lookup("transaction_registration_session"); 74 } 75 76 if (_courseHome == null) { 77 _courseHome = (CourseHome)cmp.lookup("transaction_course"); 79 } 80 } catch (NamingException e) { 81 throw new ServletException(e); 82 } 83 } 84 85 public void service(HttpServletRequest req, HttpServletResponse res) 86 throws java.io.IOException, ServletException 87 { 88 HttpSession session = req.getSession(); 89 PrintWriter out = res.getWriter(); 90 91 String cmd = null; 92 int courseId = 0; 93 Course course = null; 94 95 cmd = req.getParameter("cmd")==null ? "" : 97 (String)req.getParameter("cmd"); 98 courseId = req.getParameter("course_id")==null ? 0 : 99 Integer.parseInt((String)req.getParameter("course_id")); 100 101 if (courseId != 0) { 102 try { 103 course = _courseHome.findByPrimaryKey(courseId); 104 } catch (FinderException fe) { 105 throw new ServletException("There is no course with ID " + courseId, fe); 106 } 107 } 108 109 RegistrationSession regSession = 112 (RegistrationSession)session.getAttribute(REGISTRATION_SESSION_NAME); 113 114 if (regSession == null) { 117 try { 118 regSession = _registrationSessionHome.create(); 119 session.setAttribute(REGISTRATION_SESSION_NAME, regSession); 120 } catch (CreateException ce) { 121 throw new ServletException(ce); 122 } 123 } 124 125 if (cmd.equals("select")) { 127 try { 128 regSession.addCourse(course); 129 } catch (FinderException fe) { 130 throw new ServletException(fe); 131 } 132 } 133 else if (cmd.equals("deselect")) { 134 try { 135 regSession.removeCourse(course); 136 } catch (FinderException fe) { 137 throw new ServletException(fe); 138 } 139 } 140 else if (cmd.equals("finalize")) { 141 try { 142 regSession.finalizeRegistration(); 143 req.setAttribute("transaction_status", 144 new Integer(RegistrationSessionBean.TRANSACTION_COMMITTED)); 145 } catch (RegistrationDeniedException rde) { 146 req.setAttribute("transaction_status", 147 new Integer(RegistrationSessionBean.TRANSACTION_ROLLEDBACK)); 148 req.setAttribute("rollback_exception", rde); 149 } 150 } 151 152 this.getServletContext().getRequestDispatcher(NEXT_PAGE).forward(req,res); 154 } 155 } 156 | Popular Tags |