KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > one2many > ClientServlet


1 package example.cmp.one2many;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import javax.ejb.*;
7
8 import javax.naming.*;
9
10 import java.util.Collection;
11 import java.util.Iterator;
12
13 import java.io.PrintWriter;
14
15 /**
16  * A client to illustrate EJB managed relations in a one-to-many context.
17  */

18 public class ClientServlet extends HttpServlet {
19   StudentHome studentHome = null;
20   HouseHome houseHome = null;
21
22
23   /**
24    *
25    */

26   public void init()
27     throws ServletException
28   {
29     try {
30       Context ejb = (Context)new InitialContext().lookup("java:comp/env/cmp");
31
32       // get the Student CMP-Bean home interface
33
studentHome = (StudentHome)ejb.lookup("one2many_StudentBean");
34
35       // get the House CMP-Bean home interface
36
houseHome = (HouseHome)ejb.lookup("one2many_HouseBean");
37
38     } catch (NamingException e) {
39       throw new ServletException(e);
40     }
41   }
42
43   /**
44    *
45    */

46   public void service(HttpServletRequest req, HttpServletResponse res)
47     throws java.io.IOException, ServletException
48   {
49     res.setContentType("text/html");
50
51     PrintWriter out = res.getWriter();
52
53     // for all students, print in which House they live
54
Collection students = null;
55     try {
56       students = studentHome.findAll();
57     } catch(javax.ejb.FinderException e) {
58       throw new ServletException(e);
59     }
60
61     out.println("<h3>Student House assignments</h3>");
62
63     Iterator iter = students.iterator();
64     while (iter.hasNext()) {
65       Student student = (Student) iter.next();
66       String name = student.getName();
67       String houseName = student.getHouse().getName();
68       out.println(name + " lives in " + houseName + "<br>");
69     }
70
71     // for each House, print the list of students that live there:
72
out.println("<h3>Residents of all Houses</h3>");
73     Collection houses = null;
74     try {
75       houses = houseHome.findAll();
76     } catch(javax.ejb.FinderException e) {
77       throw new ServletException(e);
78     }
79
80     // iterate through the Houses
81
iter = houses.iterator();
82     while (iter.hasNext()) {
83       House house = (House) iter.next();
84       out.println("<h4>" + house.getName() + "</h4>");
85
86       students = (Collection) house.getStudentList();
87
88       // iterate through the students for that House
89
Iterator studentIter = students.iterator();
90       if (! studentIter.hasNext())
91         out.println("No students.<br>");
92
93       while (studentIter.hasNext()) {
94         Student student = (Student) studentIter.next();
95         out.println("<li>" + student.getName());
96       }
97     }
98
99     /*
100     // so far, the only point where we have seen EJB's relation feature in
101     // action was the getStudentList() method.
102     //
103     // The code below adds a new student to house Gryffindor. If you're using
104     // the examples schema supplied with Resin-EJB, Harry Potter is already
105     // living in Slytherin Hall. Resin-EJB automatically updates the
106     // "Harry Potter" entity to reflect the fact that he moved to
107     // Gryffindor.
108     try {
109
110       House gryffindor = houseHome.findByPrimaryKey("Gryffindor");
111       House slytherin = houseHome.findByPrimaryKey("Slytherin");
112       Student harry = studentHome.findByPrimaryKey("Harry Potter");
113
114       // for the example, make sure Harry lives somewhere other than
115       // Gryffindor. <code>setHouse</code> is a helper method
116       // defined in the local interface, <code>Student</code>.
117       harry.setHouse(slytherin);
118
119       println(out,harry.getName() + " currently lives in " +
120         harry.getHouse().getName());
121
122       println(out,"Moving " + harry.getName() + " from " +
123         harry.getHouse().getName() + " to " + gryffindor.getName() );
124
125       // automatically updates <code>harry</code> entity
126       gryffindor.addStudent(harry);
127
128       println(out,harry.getName() + " now lives in " +
129         harry.getHouse().getName());
130
131     } catch( FinderException fe ) {
132     }
133     */

134   }
135 }
136
Popular Tags