打印本文 打印本文 关闭窗口 关闭窗口
EJB 客户端程序_
作者:武汉SEO闵涛  文章来源:敏韬网  点击数848  更新时间:2009/4/23 10:54:52  文章录入:mintao  责任编辑:mintao
  完成了EJB后我们需要再来完成使用EJB的客户端程序,我们写一个Servlet程序来完成这个工作,代码如下:

package net.chinacode.web;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import net.chinacode.hello.Hello;
import net.chinacode.hello.HelloHome;

public class HelloServlet extends HttpServlet {

 // 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;
  }

  out.println("");
  out.println("Time stamp: " + new Date().toString());
  out.println("
Hello type: " + _hello.getClass().getName());
  out.println("
Answer: " + answer);
  out.println("");
  out.println("");
 }

 // Displays a trace message to System.out
 private void trace(String methodName) {
  System.out.print(methodName);
  System.out.println("() called");
 }

}

  这我们在servlet中先使用context = new InitialContext();来得到环境上下文,通过这句话我们就可以来向JNDI数据树中进行查询了,而又使用Object boundObject = context.lookup("java:comp/env/ejb/HelloHome");来得到了我们在系统的web.xml中设置的参数,这个参数是在JNDI目录数中的节点名,再通过一个对以下操作来得到EJB的Home接口:helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class);而Home接口的create方法会返回Remote接口,这个Remote接口可以来直接调用Server上的实际方法。

打印本文 打印本文 关闭窗口 关闭窗口