当前页面: 开发资料首页 → J2EE 专题 → 一个简单ejb的问题!!急啊!!
一个简单ejb的问题!!急啊!!
摘要: 一个简单ejb的问题!!急啊!!
我的ejb如下:
remote接口:Hello.java
package hello.ejb;
/**
* Remote interface for Enterprise Bean: Hello
*/
public interface Hello extends javax.ejb.EJBObject {
public String hello() throws java.rmi.RemoteException;
}
=========================================================================
Home接口:HelloHome.java
package hello.ejb;
/**
* Home interface for Enterprise Bean: Hello
*/
public interface HelloHome extends javax.ejb.EJBHome {
/**
* Creates a default instance of Session Bean: Hello
*/
public Hello create() throws
javax.ejb.CreateException,
java.rmi.RemoteException;
}
==================================================================
ejbBean:HelloBean;
package hello.ejb;
/**
* Bean implementation class for Enterprise Bean: Hello
*/
public class HelloBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext mySessionCtx;
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
/**
* Remote interface
*/
public String hello() {
return "hello , i am a ejb Bean ! time "
+new java.util.Date();
}
}
===========================================
调用程序:hello.jsp
<%@ page import="hello.ejb.*" %>
<%@ page import="java.util.Properties" %>
<%@ page errorPage="errorpage.jsp" %>
<%@ page import="javax.ejb.*, javax.naming.*, javax.rmi.PortableRemoteObject, java.rmi.RemoteException" %>
<%!
public void jspInit() {
try
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
System.out.println("Got context");
// Get a reference to the Interest Bean
//jboss默认jndi名为ejb-jar.xml中的:ejb-name
Object ref = jndiContext.lookup("Hello");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
HelloHome home = (HelloHome)PortableRemoteObject.narrow(ref, HelloHome.class);
// Create an Hello object from the Home interface
Hello hello = home.create();
// call the hello() method
System.out.println(hello.hello());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void jspDestroy() {
}
public String convert(String s){
try{
return new String(s.getBytes("ISO-8859-1"),"GB2312");
}catch(Exception e){return null;}
}
%>
===================================================================
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
helloEJB
Hello
hello.ejb.HelloHome
hello.ejb.Hello
hello.ejb.HelloBean
Stateless
<transaction-type>Container</transaction-type>
====================================================
jboss.xml:
<?xml version="1.0" encoding="UTF-8"?>
jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
Hello
HelloBean
====================================================
但我放在jboss上运行的时候出现以下错误:
2006-07-04 09:54:49,383 INFO [STDOUT] Got context
2006-07-04 09:54:49,383 INFO [STDOUT] Got reference
2006-07-04 09:54:49,414 INFO [STDOUT] java.lang.ClassCastException
怎么办?哪里错了?请大虾指点!!
Object ref = jndiContext.lookup("Hello");
lookup的应该是jndi而不是ejbname
Object ref = jndiContext.lookup("HelloBean");
对,这个我改了,但还是出错java.lang.ClassCastException
是运行到
HelloHome home = (HelloHome)PortableRemoteObject.narrow(ref, HelloHome.class);
这句的时候出错,为什么?
不应该啊,这句话没错.
阿,那有什么错阿???程序就是运行到这里出错了,怎么办?各位帮我看看!!!
少东西了吧?jndi配置没走?
try
{
//jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");
//out print jndi配置
p.list(System.out);
// Get a naming context
InitialContext jndiContext = new InitialContext(p);
System.out.println("Got context");
// Get a reference to the Interest Bean
//jboss默认jndi名为ejb-jar.xml中的:ejb-name
Object ref = jndiContext.lookup("Hello");
System.out.println("Got reference");
Object ref = jndiContext.lookup("Hello");
改为
Object ref = jndiContext.lookup("HelloBean");
试试看
恩?? //jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");
//out print jndi配置
p.list(System.out);
// Get a naming context
//jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");
//out print jndi配置
p.list(System.out);
// Get a naming context
InitialContext jndiContext = new InitialContext(p);
这个跟
//jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");
//out print jndi配置
p.list(System.out);
// Get a naming context
InitialContext jndiContext = new InitialContext(p);
这样子跟
//jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");
//out print jndi配置
p.list(System.out);
// Get a naming context
InitialContext jndiContext = new InitialContext(p);
这样子跟
InitialContext jndiContext = new InitialContext();有什么区别阿?
InitialContext jndiContext = new InitialContext(p);
这样子跟
InitialContext jndiContext = new InitialContext();有什么区别阿?
好了吗?
好了,不过我用的是jboss-4.0.4.GA,还要改配置文件才行!!弄了我一天!!
InitialContext jndiContext = new InitialContext(p);
这样子跟
InitialContext jndiContext = new InitialContext();有什么区别阿?
我看有些书上用InitialContext jndiContext = new InitialContext();为什么??
构造方法摘要
InitialContext()
构造一个初始上下文。
protected InitialContext(boolean lazy)
构造一个具有不进行初始化的选项的初始上下文。
InitialContext(Hashtable<?,?> environment)
使用所提供的环境构造一个初始上下文。
有些书上用InitialContext jndiContext = new InitialContext(); 也可以 可能是因为他的客户程序与EJB运行在同一个JVM中,或者是因为提供了jndi.properties文件吧
谢谢,马上结贴,最后一个问题:我的是运行在同一个jvm中,jndi.properties在哪里指定?????
我想用InitialContext jndiContext = new InitialContext();这个方法!!
将JBOSS下的jndi.properties加到环境变量里面
怎么加???
不会是加到windows的path里吧??
没看到又回复了啊,呵呵~~
把这个文件所在目录设定到环境变量classpath
好,谢谢!!