servlet调用EJB的问题(分不够再加)
Servlet调用EJB的问题
用JB9+webLogic8做了一个Hello World的EJB。并且发布成功。应用程序的TestClient也运行成功。
我想在Servlet中调用EJB。重新建了一个project。然后新建了一个webApplication,新建一个servlet。servlet的代码基本上是修改的
TestClient中的。开始总是提示那个SessionBean1Home和SessionBean1(EJB的名字)没有定义。于是我新建了两个class,相当于是将EJB中的这
两个文件拷了过来。现在servlet可以编译了。发布这个webApplication。在浏览器中输入这个serlvet的地址的时候却出现下面的错误:
start...
lookup OK
Error narrow:null
create:null Error in HelloWorld(): Remote interface reference is null. It must be created by calling one of the Home
interface methods first. Calling HelloWorld() null
我找的错误代码已经在注释中标出来了。
就是不知道是什么原因,也不知道怎么样可以解决。有没有谁能指点一下?
附servlet源程序如下:
package ejbclient01;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
/**
* <p>Title: ambest</p>
* <p>Description: EJBtest</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: AMB</p>
* @author ambest
* @version 1.0
*/
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of
the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private SessionBean1Home sessionBean1Home = null;
private SessionBean1 sessionBean1 = null;
PrintWriter out;
private Context getInitialContext() throws Exception {
String url = "t3://Ambest: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) {
out.println("Unable to connect to WebLogic server at " + url);
out.println("Please make sure that the server is running.");
throw e;
}
}
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
out = response.getWriter();
out.println("<html>");
out.println("<head><title>ServletEJBTest</title></head>");
out.println("<body>");
long startTime = 0;
try {
//get naming context
Context context = getInitialContext();
out.println("<br>start...<br>");//调试时的输出信息。
//look up jndi name
Object ref = context.lookup("SessionBean1");
out.println("lookup OK<br>");
//look up jndi name and cast to Home interface
sessionBean1Home = (SessionBean1Home)ref;
//上面这一行本来是这样的:sessionBean1Home = (SessionBean1Home) PortableRemoteObject.narrow(ref,
SessionBean1Home.class);
//这两种方法是同样的问题。
//错误码应该是在这一行出的问题吧。
if (logging) {
long endTime = System.currentTimeMillis();
out.println("Succeeded initializing bean access through Home interface.");
out.println("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
out.println("<font color=red>Error narrow:"+e.getMessage()+"</font><br>");
}
//create();
try {
sessionBean1 = sessionBean1Home.create();
out.println("create ok");
if (logging) {
long endTime = System.currentTimeMillis();
out.println("Succeeded: create()");
out.println("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
out.println("create:"+e.getMessage());
}
if (sessionBean1 == null) {
out.println("Error in HelloWorld(): " + ERROR_NULL_REMOTE);
}
out.println("Calling HelloWorld()");
startTime = System.currentTimeMillis();
try {
out.println("Hello World:"+sessionBean1.HelloWorld());
if (logging) {
long endTime = System.currentTimeMillis();
out.println("Succeeded: HelloWorld()");
out.println("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
out.println(e.getMessage());
}
//out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
out.println("</body></html>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}