KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > basic > CourseServlet


1 package example.cmp.basic;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import javax.naming.*;
7 import javax.transaction.*;
8 import javax.ejb.*;
9
10 import java.util.*;
11 import java.io.PrintWriter;
12
13 /**
14  * The basic CourseClient shows the basic flow of any Resin-CMP client.
15  */

16 public class CourseServlet extends HttpServlet {
17   /**
18    * Cached reference to the CourseHome interface. Because this object
19    * never changes, the client can look it up once in the <code>init()</code>
20    * method and avoid JNDI calls for each request.
21    */

22   private CourseHome _home = null;
23   /**
24    * Cached reference to the UserTransaction. Because this object
25    * never changes, the client can look it up once in the <code>init()</code>
26    * method and avoid JNDI calls for each request.
27    *
28    * <p>Normally, Resin-CMP clients will use business methods to
29    * encapsulate transactions. This example uses an explicit
30    * UserTransaction to explain more clearly what's going on.
31    */

32   private UserTransaction _userTrans = null;
33
34   /**
35    * Sets the course home.
36    */

37   public void setCourseHome(CourseHome home)
38   {
39     _home = home;
40   }
41
42   /**
43    * Initializes the reference to the CourseBean home interface.
44    */

45   public void init()
46     throws ServletException
47   {
48     try {
49       Context ic = new InitialContext();
50       
51       // The JNDI context containing local EJBs
52
Context cmp = (Context) ic.lookup("java:comp/env/cmp");
53
54       if (cmp == null)
55     throw new ServletException("ejb-server has not initialized properly.");
56
57       // Get the house stub.
58
// If you're familar with old-style EJB calls, the local EJB home
59
// doesn't need the PortableRemoteObject.narrow call because it's
60
// not a remote object.
61
if (_home == null)
62         _home = (CourseHome) cmp.lookup("basic_CourseBean");
63
64       // The JNDI context containing the user transaction
65
Object trans = ic.lookup("java:comp/UserTransaction");
66       _userTrans = (UserTransaction) trans;
67     } catch (NamingException e) {
68       throw new ServletException(e);
69     }
70   }
71
72   /**
73    * Illustrates how to interact with the Course EJB
74    */

75   public void service(HttpServletRequest req, HttpServletResponse res)
76     throws java.io.IOException, ServletException
77   {
78     PrintWriter out = res.getWriter();
79
80     res.setContentType("text/html");
81
82     //
83
// Each entity is mapped to one database row that is identified by
84
// its primary key.
85
// For all CMP beans, Resin-CMP implements a mandatory findByPrimaryKey
86
// method that we must define in the home interface (CourseHome.java).
87
// We know the primary keys of two Courses that have been preset in the
88
// database. We can now easily obtain references to two Course
89
// entities.
90
//
91
Course []course = new Course[2];
92
93     try {
94       course[0] = _home.findByPrimaryKey("Potions");
95       course[1] = _home.findByPrimaryKey("Transfiguration");
96
97       out.println("<h3>Course Details</h3>");
98
99       for (int i = 0; i < course.length; i++) {
100         out.println("course: " + course[i].getId() + "<br>");
101         out.println("instructor: " + course[i].getInstructor() + "<br>");
102         out.println("<br>");
103       }
104
105       out.println();
106       out.println("<p>Swap the instructors for the two courses." );
107       out.println("The change will be automatically and instantly reflected" );
108       out.println("in the database" );
109       out.println();
110
111       // Swap the instructors inside a transaction.
112
// The transaction acts like an intelligent synchronized lock.
113

114       course[0].swap(course[1]);
115     
116       out.println("<h3>New Course Details:</h3>");
117       for (int i = 0; i < course.length; i++) {
118         out.println("course: " + course[i].getId() + "<br>");
119         out.println("instructor: " + course[i].getInstructor() + "<br>");
120         out.println("<br>");
121       }
122
123       // The following is equivalent to the swap method above.
124
// Usually transactions are be encapsulated in an EJB business
125
// method like the swap method so Resin-CMP would take care of
126
// them automatically, but you can create your own transaction
127
// context, too.
128
try {
129         _userTrans.begin();
130         String temp = course[0].getInstructor();
131         course[0].setInstructor(course[1].getInstructor());
132         course[1].setInstructor(temp);
133       } finally {
134         _userTrans.commit();
135       }
136     }
137     catch (Exception e) {
138       throw new ServletException(e);
139     }
140   }
141 }
142
Popular Tags