1 package example.cmp.id; 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 18 public class ClientServlet extends HttpServlet { 19 StudentHome _studentHome = null; 20 QuidditchHome _quidditchHome = null; 21 22 25 public void setStudentHome(StudentHome home) 26 { 27 _studentHome = home; 28 } 29 30 33 public void setQuidditchHome(QuidditchHome home) 34 { 35 _quidditchHome = home; 36 } 37 38 41 public void init() 42 throws ServletException 43 { 44 try { 45 Context ic = new InitialContext(); 46 Context cmp = (Context) ic.lookup("java:comp/env/cmp"); 47 48 if (_studentHome == null) 50 _studentHome = (StudentHome) cmp.lookup("id_StudentBean"); 51 52 if (_quidditchHome == null) 54 _quidditchHome = (QuidditchHome) cmp.lookup("id_QuidditchBean"); 55 } catch (NamingException e) { 56 throw new ServletException(e); 57 } 58 } 59 60 63 public void service(HttpServletRequest req, HttpServletResponse res) 64 throws java.io.IOException, ServletException 65 { 66 res.setContentType("text/html"); 67 68 PrintWriter out = res.getWriter(); 69 70 Collection students = null; 72 try { 73 students = _studentHome.findAll(); 74 } catch(javax.ejb.FinderException e) { 75 throw new ServletException(e); 76 } 77 78 out.println("<h3>Student Quidditch statistics</h3>"); 79 80 out.println("<table>"); 81 out.println("<tr><th>Student<th>Position<th>Points"); 82 83 Iterator iter = students.iterator(); 84 while (iter.hasNext()) { 85 Student student = (Student) iter.next(); 86 87 out.print("<tr><td>" + student.getName()); 88 89 Quidditch quidditch = student.getQuidditch(); 90 91 if (quidditch != null) { 92 out.print("<td>" + quidditch.getPosition()); 93 out.println("<td>" + quidditch.getPoints()); 94 } 95 else { 96 out.println("<td>none<td>n/a"); 97 } 98 } 99 100 out.println("</table>"); 101 } 102 } 103 | Popular Tags |