servlet代码:
package ne;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.text.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */public class servletu extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  TestHome home;
  TestDB testdb;
  final String testURL = "ne/test.jsp";
  final String test2URL = "ne/test2.jsp";
   public void init() throws ServletException {
   try{
  
InitialContext ic = new InitialContext();
        Object objRef = ic.lookup("TestHome");
        home = (TestHome)PortableRemoteObject.narrow(objRef, TestHome.class);
   }catch(Exception a){    System.out.println("aaaaaaaaaaa"+a.toString());
   }  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    //PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(true);
    Test love1=null;
    Iterator i=null;
     int j=0;    String yztj=request.getParameter("yztj");
    String name=request.getParameter("name");    try
    {
      if (yztj.equals("确认提交")){
        String a=(String)request.getParameter("a");
        String b=request.getParameter("b");
        String c=request.getParameter("c");
        String d=request.getParameter("d");
        if (d==null){
            d="0";
            }
        float pri=Float.parseFloat(d);
        Float aaa=new Float(pri);
     love1 = home.create(a,b,c,aaa);        }
      if (yztj.equals("查询确认")){
              int count=countfindname(name);
              System.out.println("count=============================="+count);
              testdb=new TestDB(count);
              i=home.findname(name).iterator() ;
              while(i.hasNext()){
                      Test t=(Test)PortableRemoteObject.narrow(i.next(),Test.class);
                       testdb.productid[j]=t.getProductid();
                       testdb.name[j]=t.getName();
                       testdb.desc[j]=t.getDesc();
                       testdb.price[j]=t.getPrice();
                        j++;
                    }            }    }
    catch (Exception e)
    {
    System.out.println("2222222222222222222222222222222222222"+e.toString());
    }try{    testdb.i=j;    session.setAttribute("testdb",testdb);
    //mLog.Out("转移" + HomeURL);
     if (yztj.equals("确认提交")){
            getServletContext().getRequestDispatcher(test2URL).forward(request, response);
               return;
      }else if(yztj.equals("查询确认")){
             getServletContext().getRequestDispatcher(testURL).forward(request, response);
      }
    return;
}catch(Exception ee){
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+ee.toString());}
  }public int countfindname(String name){
      Iterator it=null;
      int count=0;
      try{
      it=home.findname(name).iterator() ;
       while(it.hasNext()){
it.next();
             count++;
                  }
        }catch(Exception e){
            it=null;
            System.out.println("Exception!");
          }
  it=null;
  return count;
}}jsp代码:
<%@ page import="Converter,ConverterHome,javax.ejb.*, java.math.*, javax.naming.*, javax.rmi.PortableRemoteObject, java.rmi.RemoteException" %>
<%!
   private Converter converter = null;   public void jspInit() { 
      try {
         InitialContext ic = new InitialContext();
         Object objRef = ic.lookup("java:comp/env/ejb/TheConverter");
         ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objRef, ConverterHome.class);
         converter = home.create();
      } catch (RemoteException ex) {
            System.out.println("Couldn't create converter bean."+ ex.getMessage());
      } catch (CreateException ex) {
            System.out.println("Couldn't create converter bean."+ ex.getMessage());
      } catch (NamingException ex) {
            System.out.println("Unable to lookup home: "+ "TheConverter "+ ex.getMessage());
      } 
   }   public void jspDestroy() {    
         converter = null;
   }
%>
<html>
<head>
    <title>Converter</title>
</head><body bgcolor="white">
<h1><b><center>Converter</center></b></h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get">
<input type="text" name="amount" size="25">
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form><%
    String amount = request.getParameter("amount");
    if ( amount != null && amount.length() > 0 ) {
       BigDecimal d = new BigDecimal (amount);
%>
   <p>
   <%= amount %> dollars are  <%= converter.dollarToYen(d) %>  Yen.
   <p>
   <%= amount %> Yen are <%= converter.yenToEuro(d) %>  Euro.
<%
    }
%></body>
</html>

解决方案 »

  1.   

    用JBuilder上述方法固然可行若我的SERVLET是单独一个工程,而EJB是另外一个工程,甚至在另外一台服务器上,又怎么进行呢?
      

  2.   

    和上面一样处理。你的Servlet所在的工程需要一个EJB客户端的jar包就行了。
      

  3.   

    初学者,请讲明白点
    (我甚至连JAR文件做什么都不知)
    难道是将EJB工程中的JAR文件拷贝到相应Servlet的目录中吗?