KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > entity > home > HomeServlet


1 package example.entity.home;
2
3 import java.io.*;
4
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8 import javax.ejb.*;
9 import javax.naming.*;
10
11 /**
12  * Client using the entity home call for a <code>hello</code> and
13  * an <code>add</code> call.
14  */

15 public class HomeServlet extends GenericServlet {
16   /**
17    * Caches the home interface from the JNDI lookup.
18    */

19   private Home home;
20
21   /**
22    * On init, load the Home from the JNDI context.
23    */

24   public void init()
25     throws ServletException
26   {
27     try {
28       /**
29        * Because we're looking up the remote interface, the JNDI path
30        * is java:comp/env/ejb.
31        */

32       Context ejb = (Context) new InitialContext().lookup("java:comp/env/ejb");
33
34       /*
35        * Find the home from JNDI. "home" matches the ejb-name in the
36        * home.ejb deployment descriptor.
37        */

38       home = (Home) ejb.lookup("home");
39     } catch (Exception e) {
40       throw new ServletException(e);
41     }
42
43     if (home == null)
44       throw new ServletException("can't find home");
45   }
46
47   /**
48    * Prints the result of the hello call and the add call.
49    */

50   public void service(ServletRequest request, ServletResponse response)
51     throws IOException, ServletException
52   {
53     PrintWriter pw = response.getWriter();
54
55     response.setContentType("text/html");
56     
57     try {
58       pw.println("message: " + home.hello() + "<br>");
59       
60       pw.println("1 + 3 = " + home.add(1, 3) + "<br>");
61       pw.println("7 + 1 = " + home.add(7, 1) + "<br>");
62     } catch (Exception e) {
63       throw new ServletException(e);
64     }
65   }
66 }
67
Popular Tags