<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.rmi.PortableRemoteObject"%>
<%@ page import="test.*"%>
<html>
<head>
    <title>TestPage</title>
</head>
<body bgcolor="#ffffff">
<%
    Context context = null;
    app app1 = null;
    
    //获取Context
    try 
    {
        String url = "t3://192.168.1.163:7001";
        String user = null;
        String password = null;
        Properties properties = null;
        out.println("debug | ready to set property ... <br/>");        properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        properties.put(Context.PROVIDER_URL, url);
        if (user != null)
        {
            properties.put(Context.SECURITY_PRINCIPAL, user);
            properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
        }
        //
        out.println("debug | ready to create context ... <br/>");
        context = new InitialContext(properties);
    }
    catch (Exception ex) 
    {
        out.println("<br/>创建Context对象出错:" + ex);
        return;
    }    //查找Home接口,创建对象引用
    try
    {
        //look up jndi name
        Object ref = context.lookup("app");
        //look up jndi name and cast to Home interface
        appHome Home = (appHome) PortableRemoteObject.narrow(ref, appHome.class);        //
        app1 = Home.create();
    }
    catch (Exception ex)
    {
        out.println("<br/>查找Home接口出错:" + ex);
        return;
    }    //调用EJB对象
    try
    {
        if (app1 == null)
            throw new Exception("EJBObject is null.");
            String msg = app1.sayHello("hello world!");
        out.println(msg);
        
    }
    catch (Exception ex)
    {
        out.println("调用EJB对象出错:" + ex);
    }
%></body>
</html>

解决方案 »

  1.   

    给你一段用jsp访问ejb的例子是tomcat客户端访问另一台机子上的ejb
      

  2.   

    在编写servlet时,编译器找不到xxxHome的类,无法编译。在编译时是否一定要把EJBs的jar文件加入编译??如果此EJBs已部署到另外一台服务器上,无法下载此Jar文件,那应该如何处理??
      谢谢!!
      

  3.   

    用servlet和jsp访问ejbs真的一样吗
      

  4.   

    jsp和servelet运行都要编译成class,没有本质区别
      

  5.   

    把ejb所在的包放在客户端lib下把就weblogic。jar放在客户端lib下
      

  6.   

    http://expert.csdn.net/Expert/topic/2843/2843813.xml?temp=.1787073
      

  7.   

    最好用bean封装对ejb的访问,这样还可以缓存Home接口,不需要每次调用都查询jndi
    用static还可以在整个jsp/servlet中共享Home接口
      

  8.   

    用servlet和jsp访问ejbs一样