1 package example.cmp.ejbql; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 6 import javax.naming.*; 7 import javax.ejb.*; 8 9 import java.util.*; 10 11 import java.io.PrintWriter; 12 13 16 public class FindServlet extends HttpServlet { 17 private CourseHome _courseHome = null; 18 private TeacherHome _teacherHome = null; 19 private StudentHome _studentHome = null; 20 private HouseHome _houseHome = null; 21 22 25 public void setCourseHome(CourseHome home) 26 { 27 _courseHome = home; 28 } 29 30 33 public void setTeacherHome(TeacherHome home) 34 { 35 _teacherHome = home; 36 } 37 38 41 public void setStudentHome(StudentHome home) 42 { 43 _studentHome = home; 44 } 45 46 49 public void setHouseHome(HouseHome home) 50 { 51 _houseHome = home; 52 } 53 54 public void init() 55 throws ServletException 56 { 57 try { 58 Context ic = new InitialContext(); 59 Context cmp = (Context) ic.lookup("java:comp/env/cmp"); 61 62 if (_courseHome == null) 64 _courseHome = (CourseHome) cmp.lookup("ejbql_course"); 65 if (_teacherHome == null) 66 _teacherHome = (TeacherHome) cmp.lookup("ejbql_teacher"); 67 if (_studentHome == null) 68 _studentHome = (StudentHome) cmp.lookup("ejbql_student"); 69 if (_houseHome == null) 70 _houseHome = (HouseHome) cmp.lookup("ejbql_house"); 71 72 } catch (NamingException e) { 73 throw new ServletException(e); 74 } 75 } 76 77 public void service(HttpServletRequest req, HttpServletResponse res) 78 throws java.io.IOException, ServletException 79 { 80 PrintWriter out = res.getWriter(); 81 res.setContentType("text/html"); 82 83 try { 84 out.println("<H3>Students enrolled at Hogwarts</H3>"); 85 Collection allStudents = _studentHome.findAll(); 86 Iterator studentIter = allStudents.iterator(); 87 88 while (studentIter.hasNext()) { 89 out.println(((Student)studentIter.next()).getName() + "<BR>"); 90 } 91 92 out.println("<h3>Students are taking classes from these teachers:</h3>"); 94 studentIter = allStudents.iterator(); 95 96 while (studentIter.hasNext()) { 97 Student student = (Student)studentIter.next(); 98 String studentName = student.getName(); 99 Iterator teacherIter = 100 _teacherHome.findByStudent(studentName).iterator(); 101 out.println("<b>"+studentName+"'s teachers</b><BR>"); 102 103 if (! teacherIter.hasNext()) 104 out.println(studentName + " is not taking any classes<BR>"); 105 106 while (teacherIter.hasNext()) 107 out.println(((Teacher)teacherIter.next()).getName()+"<BR>"); 108 109 out.println("<BR>"); 110 } 111 112 String houseName = "Gryffindor"; 114 out.println("<h3>Students in " + houseName + 115 " are taking these classes:</h3>"); 116 Collection courses = _courseHome.findByHouse("Gryffindor"); 117 Iterator courseIter = courses.iterator(); 118 119 if (! courseIter.hasNext()) 120 out.println("Students in " + houseName + " are lazy. " + 121 "Nobody is taking any courses.<BR>"); 122 123 while (courseIter.hasNext()) { 124 Course course = (Course)courseIter.next(); 125 out.println(course.getName() + " (taught by Professor " + 126 course.getTeacher().getName() + ")<BR>"); 127 } 128 129 out.println("<BR>"); 130 } catch (FinderException e) { 131 throw new ServletException(e); 132 } 133 } 134 } 135 | Popular Tags |