KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > transaction > RegistrationServlet


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 /**
14  * Manages the registration process by invoking business logic and dispatching
15  * the results to Resin XTP (Serif) views.
16  */

17 public class RegistrationServlet extends HttpServlet {
18
19   // XTP view page
20
public static final String NEXT_PAGE = "/examples/cmp-courses.xtp";
21
22   // session attribute name for the RegistrationSessionBean
23
public static final String REGISTRATION_SESSION_NAME = "reg_session";
24
25   // home interface of the Registration Stateful Session Bean
26
private RegistrationSessionHome _registrationSessionHome = null;
27
28   // home interface of the Course CMP entity bean
29
private CourseHome _courseHome = null;
30
31   // home interface of the Student CMP entity bean
32
private StudentHome _studentHome = null;
33
34   /**
35    * Sets the course home.
36    */

37   public void setCourseHome(CourseHome courseHome)
38   {
39     _courseHome = courseHome;
40   }
41
42   /**
43    * Sets the student home.
44    */

45   public void setStudentHome(StudentHome studentHome)
46   {
47     _studentHome = studentHome;
48   }
49
50   /**
51    * Sets the registration home.
52    */

53   public void setSessionHome(RegistrationSessionHome registrationHome)
54   {
55     _registrationSessionHome = registrationHome;
56   }
57
58   /**
59    * resolves all required Home Interfaces
60    */

61   public void init()
62     throws ServletException
63   {
64     try {
65       Context ic = new InitialContext();
66       
67       // The JNDI context containing EJBs
68
Context cmp = (Context) ic.lookup("java:comp/env/cmp");
69
70       if (_registrationSessionHome == null) {
71     // get the registration session bean stub
72
_registrationSessionHome = (RegistrationSessionHome)
73       cmp.lookup("transaction_registration_session");
74       }
75
76       if (_courseHome == null) {
77     // get the course bean stub
78
_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     // parse parameters
96
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     // Obtain our Registration bean that we stored in the Servlet's
110
// HttpSession during a previous request.
111
RegistrationSession regSession =
112       (RegistrationSession)session.getAttribute(REGISTRATION_SESSION_NAME);
113
114     // If this is a new Servlet Session, obtain a Resgistration Session Bean
115
// and store it in the servlet's HttpSession
116
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     // execute cmd
126
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     // dispatch to the XTP view
153
this.getServletContext().getRequestDispatcher(NEXT_PAGE).forward(req,res);
154   }
155 }
156
Popular Tags