servlet调用EJB的问题(分不够再加)

ambest 2003-10-20 10:01:29
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() {
}
}
...全文
35 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zenzuguocsdn 2003-10-23
  • 打赏
  • 举报
回复
:)
ambest 2003-10-23
  • 打赏
  • 举报
回复
怎么没有人回答啊?
也不是蛮难嘛。
我搞定了。
etre 2003-10-21
  • 打赏
  • 举报
回复
你的JNDI对吗?发布是否成功了
lzl123 2003-10-21
  • 打赏
  • 举报
回复
1.如果你的session bean用的是Remote Interface,就用sessionBean1Home = (SessionBean1Home) PortableRemoteObject.narrow(ref, SessionBean1Home.class);
2.把错误用e.printStackTrace()打印出来.很明显你在JNDI查找home接口的时候就出错了,最可能的错误就是根据JNDI没找到home

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧