KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > FindServlet


1 package example;
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 /**
14  * A client to illustrate Course bean's finder methods. The example uses
15  * three finder methods from the CourseHome interface:
16  * <ul>
17  * <li>findByPrimaryKey - the standard find method available in every bean.
18  * <li>findAll - a collection finder returning all the courses.
19  * <li>findByInstructor - a single-bean finder returning the course taught
20  * by an instructor.
21  * </ul>
22  *
23  *
24  */

25 public class FindServlet extends HttpServlet {
26   /**
27    * The course local home interface. It is set in the servlet's
28    * init() method.
29    */

30   private CourseHome _home = null;
31
32   /**
33    * Sets the course home as a bean setter.
34    */

35   public void setCourseHome(CourseHome home)
36   {
37     _home = home;
38   }
39
40   /**
41    * Finds the course's home interface using JNDI. Since the home interface
42    * never changes, looking it up in the <code>init()</code> interface
43    * avoids the relatively slow JNDI call for each request.
44    *
45    * <p>The course bean is located at java:comp/env/cmp/finder_CourseBean.
46    *
47    * @exception ServletException if the JNDI lookup fails.
48    */

49   public void init()
50     throws ServletException
51   {
52     if (_home == null) {
53       try {
54         Context ic = new InitialContext();
55         
56         // get the house stub
57
_home = (CourseHome) ic.lookup("java:comp/env/cmp/find");
58       } catch (NamingException e) {
59         throw new ServletException(e);
60       }
61     }
62   }
63
64   /**
65    *
66    */

67   public void service(HttpServletRequest req, HttpServletResponse res)
68     throws java.io.IOException, ServletException
69   {
70     PrintWriter out = res.getWriter();
71
72     res.setContentType("text/html");
73
74     out.println("<h3>Find by Primary Key</h3>");
75
76     // We want to find the course "Potions"
77
String courseName = "Potions";
78
79     try {
80       // findByPrimaryKey() is a finder method that must be present in
81
// every CMP bean's home interface. It returns the entity that
82
// corresponds to the primary key passed. We have configured
83
// courseId to be the primary key in cmp-find.ejb
84

85       Course course = _home.findByPrimaryKey(courseName);
86
87       out.println(course.getId() + " is taught by " +
88                   course.getInstructor());
89     } catch (ObjectNotFoundException e) {
90       out.println("There is no course '" + courseName + "'");
91     } catch (FinderException e) {
92       throw new ServletException(e);
93     }
94
95     out.println("<h3>Find All Courses</h3>");
96
97     try {
98       // We have defined a finder method in cmp-find.ejb that returns
99
// all courses. Resin-CMP has implemented this method for us.
100

101       Collection c = _home.findAll();
102       Iterator iter = c.iterator();
103
104       // Because findByInstructor returns a Collection, we will not catch
105
// an ObjectNotFoundException if there are no courses.
106
// Instead we will receive an empty Collection.
107

108       if (! iter.hasNext())
109         out.println("No classes are being taught!");
110
111       // display all courses
112
while (iter.hasNext()) {
113         Course course = (Course) iter.next();
114
115         out.println(course.getId() + " is taught by " +
116                     course.getInstructor() + "<br>");
117       }
118     } catch (ObjectNotFoundException e) {
119       throw new ServletException(e);
120     } catch (FinderException e) {
121       throw new ServletException(e);
122     }
123
124     out.println("<h3>Find the course taught by an instructor</h3>");
125
126     String teacher = "Remus Lupin";
127     try {
128       // We have defined a finder method in cmp-find.ejb that returns
129
// the course taught by a teacher. Resin-CMP has implemented
130
// this method for us.
131

132       Course course = _home.findByInstructor(teacher);
133
134       // Because findByInstructor returns a single Course, it will
135
// throw an ObjectNotFoundException if the teacher has no course.
136

137       // display the course
138
out.println(course.getId() + " is taught by " +
139                   course.getInstructor() + "<br>");
140     } catch (ObjectNotFoundException e) {
141       out.println(teacher + " is not teaching any courses.");
142     } catch (FinderException e) {
143       throw new ServletException(e);
144     }
145   }
146 }
147
Popular Tags