My FAQ,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > EJB
EJB 客户端程序
作者:未知 时间:2001-03-30 12:12 出处:Yesky 责编:MyFAQ
              摘要:暂无
  完成了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上的实际方法。
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有