========= 客户访问程序 ===========
package j2eetest.zxadvice;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;public class ZxadviceServlet extends HttpServlet {
    private Context ctx = null;
    static final private String CONTENT_TYPE = "text/html; charset=GBK";
    //Initialize global variables
    public void init() throws ServletException {
    }
    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String str = "初始值";
        try{
            ctx = new InitialContext();
            Object h = ctx.lookup("ZxadviceSession");
            ZxadviceSessionHome home = (ZxadviceSessionHome)javax.rmi.PortableRemoteObject.narrow(h , ZxadviceSessionHome.class);
            ZxadviceSession hw = home.create();
            str ="from EJB : " + hw.getContent("abian");
        }catch(Exception e){
            e.printStackTrace();
        }
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>ZxadviceServlet</title></head>");
        out.println("<body>");
        out.println("<p> " + str + "</p>");
        out.println("</body></html>");
    }    private Context getInitialContext() throws Exception {
        String url = "t3://localhost:7001";
        String user = null;
        String password = null;
        Properties properties = null;
        try {
            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);
            }            return new InitialContext(properties);
        }
        catch(Exception e) {
            log("Unable to connect to WebLogic server at " + url);
            log("Please make sure that the server is running.");
            throw e;
        }
    }
    //Clean up resources
    public void destroy() {
    }
}

解决方案 »

  1.   

    你的问题可能是想把CMP(Local)的引用传给Client,这是不行的,因为它
    是本地的。
    一般是通过一个SessionBean来包裹本地的实体Bean,client只能访问SessionBean,建议看看Facade模式。
      

  2.   

    RE:任我行:
        我并没有把CMP(Local)的引用传给Client,是在SessionBean里面引用的上面代码显示:
    ---------- SessionBean  -------------
    private ZxadviceHome getHome() throws Exception {
        Context ic = new InitialContext();
        Object ref = ic.lookup("Zxadvice");
        System.out.println("obj = "+ref);
        ZxadviceHome imh = (ZxadviceHome)PortableRemoteObject.narrow(ref , ZxadviceHome.class);
        return imh;
        }
    -------------  over  -----------------
      

  3.   

    报错代码为:
    javax.ejb.EJBException: Attempt to pass a reference to an EJBLocalObject to a remote client.  A local EJB component may only be accessed by clients co-located in the same ear or standalone jar file. <<no stack trace available>>
      

  4.   

    检查一下Sessionbean,可能在调用Entitybean的Home接口时通过了远程调用,而不是 Local。
      

  5.   

    没有问题,是服务器过多的临时文件造成了。我删掉META-INF文件夹下的文件再重启服务器就没事了。
    多谢各位!!