转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
EJB 客户端程序_         ★★★★

EJB 客户端程序_

作者:闵涛 文章来源:闵涛的学习笔记 点击数:846 更新时间:2009/4/23 10:54:52
  完成了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上的实际方法。


[网页制作]JavaBean与EJB有何不同之处  [网页制作][J2EE] 实战开发EJB
[Web开发]EJB Interface  [JAVA开发]前进:从EJB 2.1到EJB 3.0
[JAVA开发]EJB 3.0是Hibernate的克隆吗?  [JAVA开发]利用EJB 3.0的JPA设计企业应用程序
[JAVA开发]EJB 3.0+Beehive开发客户反馈系统_软件_EJB3.0  [JAVA开发]EJB 3.0+Aspect实现声明性编程初步
[JAVA开发]EJB 3.0 开发指南之实体Bean  [JAVA开发]EJB 3.0 开发指南之依赖值对象
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台