我不明白你的意思!
EJB Test Client实际上就是一个Application啊!!

解决方案 »

  1.   

    EJB Test Client是包含在EJB包中的。
    我说的是独立采用Applet或者Application来调用这个EJB.
    我是新手,可能意思表达不清楚:
    EJB已经发布了,能不能用Applet在另外一抬机器上调用这个EJB呢?
    在Applet中怎么调用?
      

  2.   

    ”EJB Test Client是包含在EJB包中的“???谁告诉你的!!!EJB Test Client不包含在包里!!
    并且EJB Test Client可以在另外一抬机器上调用这个EJB!!(EJB必须实现remote接口)
      

  3.   

    不好意思,看错了。
    EJB我已实现了remote接口。
    但不知道怎么使用EJB Test Client在另外一抬机器上调用这个EJB
      

  4.   

    上面没说明白。
    只要EJB实现remote接口,那EJB Test Client就可以远程调用了。
    我想知道的是,如果菜用Applet的话,程序里怎么调用这个EJB呢?
      

  5.   

    将EJB Test Client.java在另一台机器上编译运行
      

  6.   

    package com.todaytech.servlet;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;import com.todaytech.sb.testdb.*;public class TestSvlt extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=EUC_CN";
      //Initialize global variables
      public void init() throws ServletException {
      }
      //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
      }
      //Process the HTTP Post request
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/octest-stream");
        request.setCharacterEncoding("gb2312");
        ServletInputStream input = request.getInputStream();
        DataInputStream dis = new DataInputStream(input);
        String sTmp = dis.readUTF();
        System.out.println("È¡µÃµÄ×Ö·û´®Î¢£º " + sTmp);
        StringTokenizer st = new StringTokenizer(sTmp,"|");
        int length = st.countTokens();
        String sCmd = st.nextToken();
        String rtnStr = "init is ok!";
        TSB tsb = getSb();
        if ("add".equals(sCmd)){
          int id = 1;
          if (length > 1){
            String sid = st.nextToken();
            if (!(sid == null || "".equals(sid))){
              try
              {
                id = Integer.parseInt(sid);
              }
              catch (Exception ex)
              {
                id = 1;
              }
            }
          }
          tsb.add(id);
          rtnStr = "ok ! ³É¹¦£¡";
        }
        else if ("set".equals(sCmd)){
          String s50 = "Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þÈýÈýÈýÈýÈýÈýÈýÈýÈýÈýËÄËÄËÄËÄËÄËÄËÄËÄËÄËÄÎåÎåÎåÎåÎåÎåÎåÎåÎåÎå";
          if (length > 1){
            String s50Tmp = st.nextToken();
            if (!(s50Tmp == null || "".equals(s50Tmp))){
              s50 = s50Tmp;
            }
          }
          int id = 1;
          if (length > 2) {
            String sid = st.nextToken();
            if (! (sid == null || "".equals(sid))) {
              try {
                id = Integer.parseInt(sid);
              }
              catch (Exception ex) {
                id = 1;
              }
            }
          }
          tsb.setTest50(s50, id);
          rtnStr = "ok ! ³É¹¦£¡";
        }
        else if ("get".equals(sCmd)){
          int id = 1;
          if (length > 1) {
            String sid = st.nextToken();
            if (! (sid == null || "".equals(sid))) {
              try {
                id = Integer.parseInt(sid);
              }
              catch (Exception ex) {
                id = 1;
              }
            }
          }
         rtnStr = tsb.getTest50(id);
        }
        else {
          try{
            tsb.setTest50(tsb.getTest50(1), 2);
            rtnStr = "ok ! ³É¹¦!";
          }
          catch(Exception ex){
            ex.printStackTrace();
            rtnStr = ex.getMessage();
          }
        }
        DataOutputStream dos = new DataOutputStream(response.getOutputStream());
        dos.writeUTF(rtnStr);
        dos.close();
      }
      //Clean up resources
      public void destroy() {
      }  private TSB getSb(){
        try
        {
          TSBHome tsbh = null;
          InitialContext jndiContext = new InitialContext();
          Object ref  = jndiContext.lookup("TSB");
          tsbh = (TSBHome) PortableRemoteObject.narrow(ref, TSBHome.class);
          return tsbh.create();
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
          return null;
        }  }
    }
      

  7.   

    package com.todaytech.applet;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;import java.io.*;
    import java.net.*;
    import java.util.*;
    public class mainFram extends JFrame {
      JPanel contentPane;
      JButton call = new JButton();
      JTextField cad = new JTextField();  //Construct the frame
      public mainFram() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        call.setBounds(new Rectangle(109, 227, 225, 59));
        call.setText("jButton1");
        call.addActionListener(new mainFram_call_actionAdapter(this));
        contentPane.setLayout(null);
        this.setSize(new Dimension(455, 379));
        this.setTitle("²âÊÔ");
        cad.setText("jTextField1");
        cad.setBounds(new Rectangle(11, 27, 429, 36));
        contentPane.add(call, null);
        contentPane.add(cad, null);
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  private void callCmd(){
        try{
          URL url = new URL("http://localhost:7001/twebapp/testsvlt");
          URLConnection urlcon = url.openConnection();
          urlcon.setUseCaches(true);
          urlcon.setDoInput(true);
          urlcon.setDoOutput(true);
          //urlcon.connect();
          urlcon.setRequestProperty("Content-type", "application/octest-stream");
          DataOutputStream dos = new DataOutputStream(urlcon.getOutputStream());
          dos.writeUTF(cad.getText());
          dos.flush();
          dos.close();
          DataInputStream dis=new DataInputStream(urlcon.getInputStream());
          String rtnCode = dis.readUTF();
          dis.close();
          System.out.println(rtnCode);
        }
        catch(Exception ex){
          ex.printStackTrace();
        }  }  void call_actionPerformed(ActionEvent e) {
        System.out.println(cad.getText());
        callCmd();
      }}class mainFram_call_actionAdapter implements java.awt.event.ActionListener {
      mainFram adaptee;  mainFram_call_actionAdapter(mainFram adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.call_actionPerformed(e);
      }
    }
      

  8.   

    上面分别事servlet和app的代码 你看的有&符号的是中文码 你自己转换一下  呵呵记得给分呀
      

  9.   


    一下是客户端代码,调用weblogic7.0应用服务器EJB对象方法..
    package examples;import javax.ejb.*;
    import javax.naming.*;
    import java.rmi.*;
    import java.util.*;/**
     * Client test application on a Container-Managed Entity Bean, Product.
     */
    public class ProductClient { public static void main(String[] args) throws Exception { ProductHome home = null; try {
    /*
     * Get a reference to the Product Home Object - the
     * factory for Product EJB Objects
     */
    Context ctx = new InitialContext(System.getProperties());
    home = (ProductHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("RemoteProductHome"), ProductHome.class); /*
     * Use the factory to create the Product EJB Object
     */
    home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);
    home.create("123-456-7891", "P5-400", "400 Mhz Pentium", 300);
    home.create("123-456-7892", "P5-450", "450 Mhz Pentium", 400);
    home.create("123-456-7893", "SD-64", "64 MB SDRAM", 50);
    home.create("123-456-7894", "SD-128", "128 MB SDRAM", 100);
    home.create("123-456-7895", "SD-256", "256 MB SDRAM", 200); /*
     * Find a Product, and print out it's description
     */
    Iterator i = home.findByName("SD-64").iterator();
    System.out.println("The following product descriptions match the product name SD-64:");
    while (i.hasNext()) {
    Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
    System.out.println(prod.getDescription());
    } /*
     * Find all products that cost $200
     */
    System.out.println("Calling finder to find all products that cost $200");
    i = home.findByBasePrice(200).iterator(); while (i.hasNext()) {
    Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
    System.out.println(prod.getDescription());
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    if (home != null) {
    System.out.println("Destroying products.."); /*
     * Find all the products
     */
    Iterator i = home.findAllProducts().iterator();
    while (i.hasNext()) {
    try {
    Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
    if (prod.getProductID().startsWith("123")) {
    prod.remove();
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    }
      

  10.   

    下面是BEAN 代码package examples;import javax.ejb.*;/**
     * Entity Bean which demonstrates Container-Managed persistence.
     *
     * This is a product that's persistent.  It has an ID #, a name,
     * a description, and a base price.
     */
    public abstract class ProductBean implements EntityBean { protected EntityContext ctx; public ProductBean() {
    } //----------------------------------------------------------------
    // Begin abstract get/set methods
    //----------------------------------------------------------------        public abstract String getName();
    public abstract void setName(String name);
    public abstract String getDescription();
    public abstract void setDescription(String description);
            public abstract double getBasePrice();
            public abstract void setBasePrice(double price);
    public abstract String getProductID();
    public abstract void setProductID(String productID); //----------------------------------------------------------------
    // End abstract get/set methods
    //---------------------------------------------------------------- //----------------------------------------------------------------
    // Begin EJB-required methods.  The methods below are called
    // by the Container, and never called by client code.
    //---------------------------------------------------------------- /**
     * Called by Container.
     * Implementation can acquire needed resources.
     */
    public void ejbActivate() {
    System.out.println("ejbActivate() called.");
    } /**
     * EJB Container calls this method right before it
     * removes the Entity Bean from the database.
     * Corresponds to when client calls home.remove().
     */
    public void ejbRemove() {
    System.out.println("ejbRemove() called.");
    } /**
     * Called by Container.
     * Releases held resources for passivation.
     */
    public void ejbPassivate() {
    System.out.println("ejbPassivate () called.");
    } /**
     * Called from the Container.  Updates the entity bean
     * instance to reflect the current value stored in
     * the database.
     *
     * Since we're using Container-Managed Persistence, we
     * can leave this method blank.  The EJB Container will
     * automatically set our public fields to the correct values.
     */
    public void ejbLoad() {
    System.out.println("ejbLoad() called.");
    } /**
     * Called from the Container.  Updates the database to
     * reflect the current values of this in-memory Entity Bean
     * instance representation.
     *
     * Since we're using Container-Managed Persistence, we can
     * leave this method blank.  The EJB Container will
     * automatically save our public fields into the database.
     */
    public void ejbStore() {
    System.out.println("ejbStore() called.");
    } /**
     * Called by Container.  Associates this Bean instance with
     * a particular context.  Once done, we can query the
     * Context for environment info
     */
    public void setEntityContext(EntityContext ctx) {
    System.out.println("setEntityContext called");
    this.ctx = ctx;
    } /**
     * Called by Container.  Disassociates this Bean instance
     * with a particular context environment.
     */
    public void unsetEntityContext() {
    System.out.println("unsetEntityContext called");
    this.ctx = null;
    } /**
     * Called after ejbCreate().  Now, the Bean can retrieve
     * its EJBObject from its context, and pass it as a 'this'
     * argument.
     */
    public void ejbPostCreate(String productID, String name, String description, double basePrice) {
    System.out.println("ejbPostCreate() called");
    } /**
     * This is the initialization method that corresponds to the
     * create() method in the Home Interface.
     *
     * When the client calls the Home Object's create() method,
     * the Home Object then calls this ejbCreate() method.
     *
     * NOTE: Since we're using Container-Managed persistence,
     * this method returns void.  With Bean-Managed Persistence,
     * we returned the PK.  This is because our Bean was
     * responsible for dealing with PKs and accessing
     * the database.  Now that we let the Container handle
     * persistence, the Container makes the Primary Key.
     *
     * We still need to initialize our Bean's fields with the
     * parameters passed from the client, so that the Container
     * can inspect our Bean and create the corresponding database
     * entries.
     */
    public String ejbCreate(String productID, String name, String description, double basePrice) {
    System.out.println("ejbCreate() called"); setProductID(productID);
    setName(name);
    setDescription(description);
    setBasePrice(basePrice); return productID;
    } // No finder methods - they are implemented by Container //----------------------------------------------------------------
    // End EJB-required methods
    //----------------------------------------------------------------
    }
      

  11.   

    总结起来其实,很简单:
    在EJB2.0中,从应用程序中查找HOME对象:Properties prop = new Properties( );
    prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    prop.put(Context.PROVIDER_URR,"t3://localhost:7001");
    Context ctx = new InitialContext(prop);
    home = (ProductHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("RemoteProductHome"), ProductHome.class);home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);//创建并初始化EJB对象
      

  12.   

    和EJB Test Client里一样啊,只不过换成了APPLET而已
      

  13.   

    我的EJB发布在
    C:\bea\user_projects\mydomain\myserver\upload\EJBTest\EJBTest.jar
    其中有2个自己写的方法getName和getMyText.
    要在Applet中调用EJB中的这2个方法,怎么调用?大家一下贴这么多,谢谢了,不过,反倒不明白了。
      

  14.   

    把jb8生成的TestClient.java考在另一台机子上
    javac -classpath ".;weblogic.jar" test\TestClient.java 
    java -classpath ".;weblogic.jar" test.TestClienttest中的文件清单:
    testRemote.class     <--------EJB远程接口
    testRemoteHome.class 〈-------EJB Home接口
    TestClient.java weblogic.jar要包括在类路径中!!如何调用自己写的方法getName和getMyText,可以研究一下jb8自动生成的例子
    用Applet的话也差不多
      

  15.   

    我的EJB发布在
    C:\bea\user_projects\mydomain\myserver\upload\EJBTest\EJBTest.jar    StringBuffer sbuf1 = new StringBuffer();
        URL url1 = new URL("http://localhost:7001/"); //在这里应该什么?
        this.getStringFromUrl(url1,sbuf1);