1 package example.cmp.map; 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.Map; 12 import java.util.Iterator; 13 14 import java.io.PrintWriter; 15 16 20 public class ClientServlet extends HttpServlet { 21 StudentHome studentHome = null; 22 CourseHome courseHome = null; 23 GradeHome gradeHome = null; 24 25 28 public void init() 29 throws ServletException 30 { 31 try { 32 Context ejb = (Context)new InitialContext().lookup("java:comp/env/cmp"); 33 34 studentHome = (StudentHome) ejb.lookup("map_StudentBean"); 36 37 courseHome = (CourseHome) ejb.lookup("map_CourseBean"); 39 40 gradeHome = (GradeHome) ejb.lookup("map_GradeBean"); 42 } catch (NamingException e) { 43 throw new ServletException(e); 44 } 45 } 46 47 50 public void service(HttpServletRequest req, HttpServletResponse res) 51 throws java.io.IOException, ServletException 52 { 53 res.setContentType("text/html"); 54 55 PrintWriter out = res.getWriter(); 56 57 Collection students = null; 59 try { 60 students = studentHome.findAll(); 61 } catch(javax.ejb.FinderException e) { 62 throw new ServletException(e); 63 } 64 65 out.println("<title>Student Grades</title>"); 66 out.println("<h1>Student Grades</h1>"); 67 68 Iterator iter = students.iterator(); 69 while (iter.hasNext()) { 70 Student student = (Student) iter.next(); 71 72 out.println("<h3>" + student.getName() + "</h3>"); 73 74 Map grades = student.getGrades(); 75 76 out.println("<table>"); 77 out.println("<tr><th>Course<th>Grade"); 78 79 Iterator courseIter = grades.keySet().iterator(); 80 while (courseIter.hasNext()) { 81 Course course = (Course) courseIter.next(); 82 83 Grade grade = (Grade) grades.get(course); 84 85 out.print("<tr><td>" + course.getName() + "<td>" + grade.getGrade()); 86 } 87 out.println("</table>"); 88 } 89 } 90 } 91 | Popular Tags |