KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > xdoclet > FindServlet


1 package example.cmp.xdoclet;
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 many-to-many context.
17  *
18  * @web:servlet name="findServlet"
19  * @web:servlet-mapping url-pattern="/find"
20  */

21 public class FindServlet extends HttpServlet
22 {
23   StudentHome studentHome;
24   CourseHome courseHome;
25
26   public void init()
27     throws ServletException
28   {
29     // obtain references to the home interfaces
30
try {
31       Context ejb = (Context)new InitialContext().lookup("java:comp/env/cmp");
32
33       // get the Student CMP-Bean home interface
34
studentHome = (StudentHome) ejb.lookup("xdoclet_StudentBean");
35
36       // get the Course CMP-Bean home interface
37
courseHome = (CourseHome) ejb.lookup("xdoclet_CourseBean");
38
39     } catch (NamingException e) {
40       throw new ServletException(e);
41     }
42   }
43
44   public void service(HttpServletRequest req, HttpServletResponse res)
45     throws java.io.IOException, ServletException
46   {
47     res.setContentType("text/html");
48
49     PrintWriter out = res.getWriter();
50     
51     try {
52       Course divination = courseHome.findByPrimaryKey("Divination");
53       Student hermione = studentHome.findByPrimaryKey("Hermione Granger");
54       Iterator i;
55       
56       out.println("<h3>Students By Class</h3>");
57       // iterate over all courses
58
i = courseHome.findAll().iterator();
59       out.println("<ul>");
60       while (i.hasNext()) {
61         Course course = (Course) i.next();
62         out.println("<li><b>" + course.getName() + ":</b>");
63         
64         out.println("<ul>");
65         // Students in the Course
66
Iterator students = course.getStudentList().iterator();
67         if (! students.hasNext())
68           out.println("<li>No students</li>");
69         while (students.hasNext())
70           out.println("<li>" + ((Student) students.next()).getName() + "</li>");
71         out.println("</ul></li>");
72       }
73       out.println("</ul>");
74       
75       // add a student to Divination
76
out.println("Enrolling Hermione in Divination<br>");
77       divination.addStudent(hermione);
78       out.println("<h4>Divination's new student list:</h4>");
79       out.println("<ul>");
80       i = divination.getStudentList().iterator();
81       while (i.hasNext())
82           out.println("<li>" + ((Student) i.next()).getName() + "</li>");
83       out.println("</ul>");
84       divination.removeStudent(hermione);
85       
86       // list all instructors
87
i = courseHome.listAllInstructors().iterator();
88       out.println("<h3>Teachers</h3>");
89       if (! i.hasNext())
90         out.println("No teachers<br>");
91       while (i.hasNext())
92         out.println(((String) i.next()) + "<br>");
93       
94       
95       out.println("<h3>Classes by Student</h3>");
96       // iterate over all students
97
i = studentHome.findAll().iterator();
98       out.println("<ul>");
99       while (i.hasNext()) {
100         Student student = (Student) i.next();
101         out.println("<li><b>" + student.getName() + ":</b>");
102         
103         out.println("<ul>");
104         // Courses taken by the Student
105
Iterator courses = student.getCourseList().iterator();
106         if (! courses.hasNext())
107           out.println("<li>No classes?!</li>");
108         while (courses.hasNext())
109           out.println("<li>" + ((Course) courses.next()).getName() + "</li>");
110         out.println("</ul></li>");
111       }
112       out.println("</ul>");
113       
114       // add a student to Divination
115
out.println("Enrolling Hermione in Divination<br>");
116       hermione.addCourse(divination);
117       out.println("<h4>Hermione's new class list:</h4>");
118       out.println("<ul>");
119       i = hermione.getCourseList().iterator();
120       while (i.hasNext())
121           out.println("<li>" + ((Course) i.next()).getName() + "</li>");
122       out.println("</ul>");
123       hermione.removeCourse(divination);
124     } catch (FinderException e) {
125       throw new ServletException(e);
126     }
127   }
128 }
129
Popular Tags