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 21 public class FindServlet extends HttpServlet 22 { 23 StudentHome studentHome; 24 CourseHome courseHome; 25 26 public void init() 27 throws ServletException 28 { 29 try { 31 Context ejb = (Context)new InitialContext().lookup("java:comp/env/cmp"); 32 33 studentHome = (StudentHome) ejb.lookup("xdoclet_StudentBean"); 35 36 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 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 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 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 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 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 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 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 |