动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Jsp → 阅读文章

 EJB 客户端程序

作者来源: 
阅读 数 223 人次 , 2006-4-19 1:34:00 


  完成了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上的实际方法。   
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:在Linux上构建JSP环境
· 下一篇:JSP与EJB通信
· JAVA/JSP学习系列之四
· JSP Actions的使用
· JSP如何连接DB2数据库
· JSP单页面网站文件管理器
· Java代码编写的一般性指导


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077