// constructor public HelloServlet() { super(); trace(""); }
// A reference to the remote `Hello' object protected Hello _hello;
// Initializes this servlet public void init(ServletConfig config) throws ServletException { super.init(config); trace("init");
// Get the initial JNDI context using our settings Context context; try { context = new InitialContext(); } catch (Throwable exception) { throw new ServletException( "Unable to get initial JNDI context: " + exception.toString()); }
// Get a reference to the Hello home interface HelloHome helloHome; try { Object boundObject = context.lookup("java:comp/env/ejb/HelloHome"); helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class); } catch (Throwable exception) { throw new ServletException( "Unable to get home interface: " + exception.toString()); }
// Get a reference to a Hello instance try { _ hello = helloHome.create(); } catch (Throwable exception) { throw new ServletException( "Unable to create Hello instance: " + exception.toString()); }
// Insanity check: Make sure we have a valid reference if (_hello == null) { throw new ServletException( "Unable to create Hello instance, create() returned null"); }
}
// Handles the HTTP GET request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { trace("doGet");
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
// Get the answer from the bean String answer; try { answer = _hello.sayHello("HD"); } catch (Throwable exception) { out.println(""); out.println("Time stamp: " + new Date().toString()); out.println(" Hello type: " + _hello.getClass().getName()); out.println("Error calling the Hello bean"); out.println(exception.toString()); out.println(""); out.println(""); return; }