这里以jsp调用ejb为例,因为jsp与servlet其实是一会事,是jboss自带的template例子,运行环境是jboss3+tomcat4:1.代码级别调用:
try {
Context lContext = new InitialContext();//创建容器环境
TestSessionHome lHome = (TestSessionHome) lContext.lookup(
"java:comp/env/ejb/webtest/TestSession"
);//通过引用名来查找ejb的本地接口
TestSession lSession = lHome.create();//创建ejb引用
out.println( "" + lSession.getNewEntityId() );//调用ejb中的方法
}
catch( Exception e ) {
out.println( "Caugth exception: " + e.getMessage() );
e.printStackTrace();
}2.部署(deployment)设置:
因为servlet与ejb都运行在容器中,通过容器来交流,因此,容器必须知道通过什么方式正确找到ejb,一般分这两步:
a)sun标准的web.xml部署描述:
<web-app> //根标签,指明本文件对一个web application进行部署描述
<display-name>Web Client</display-name>
- <!-- The Welcome File List
-->
- <welcome-file-list>
<welcome-file>index.jsp</welcome-file>//default页面,没有关系
<welcome-file>index.html</welcome-file>
</welcome-file-list>
- <ejb-ref>
<ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//指明ejb引用别名
<ejb-ref-type>Session</ejb-ref-type> //指明ejb类型,session或者entity
<home>test.interfaces.TestSessionHome</home> //指明ejb本地接口
<remote>test.interfaces.TestSession</remote> //指明ejb远程接口
</ejb-ref>
</web-app>b)每个服务器容器虽然遵循了sun j2ee的规范,但是在实现上有差异,以jboss为例,为了获得ejb的实际jndi名称(又一个名字!!!),jboss在jboss-web.xml做了如下指定:<jboss-web>
<ejb-ref>
<ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//ejb引用名,跟web.xml中的一致
<jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,容器唯一识别名
</ejb-ref>
</jboss-web>c)那么,如何通过jndi名来知道实际的bean呢?jboss里头在ejb-jar.xml来进行了描述,记住,ejb-jar.xml是sun的标准文件:<ejb-name>test/TestSession</ejb-name>//ejb名称,又一个名称!!! <home>test.interfaces.TestSessionHome</home>//实际的ejb内容
<remote>test.interfaces.TestSession</remote>//同上
<ejb-class>test.session.TestSessionBean</ejb-class>//同上
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>那么,这个文件又怎么同jndi名字挂上钩呢?以下又是jboss自己的文件了--jboss.xml:
<session>
<ejb-name>test/TestSession</ejb-name>//ejb名称,同ejb-jar.xml
<jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,ok,红军胜利会师
</session>3.总结:
程序里通过context来调用,部署文件的引用关系是:应用别名-->jndi名-->ejb名-->ejb类和接口.4.建议:弄清楚这几个名字的含义和关系,你就可以掌握部署的核心了。
try {
Context lContext = new InitialContext();//创建容器环境
TestSessionHome lHome = (TestSessionHome) lContext.lookup(
"java:comp/env/ejb/webtest/TestSession"
);//通过引用名来查找ejb的本地接口
TestSession lSession = lHome.create();//创建ejb引用
out.println( "" + lSession.getNewEntityId() );//调用ejb中的方法
}
catch( Exception e ) {
out.println( "Caugth exception: " + e.getMessage() );
e.printStackTrace();
}2.部署(deployment)设置:
因为servlet与ejb都运行在容器中,通过容器来交流,因此,容器必须知道通过什么方式正确找到ejb,一般分这两步:
a)sun标准的web.xml部署描述:
<web-app> //根标签,指明本文件对一个web application进行部署描述
<display-name>Web Client</display-name>
- <!-- The Welcome File List
-->
- <welcome-file-list>
<welcome-file>index.jsp</welcome-file>//default页面,没有关系
<welcome-file>index.html</welcome-file>
</welcome-file-list>
- <ejb-ref>
<ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//指明ejb引用别名
<ejb-ref-type>Session</ejb-ref-type> //指明ejb类型,session或者entity
<home>test.interfaces.TestSessionHome</home> //指明ejb本地接口
<remote>test.interfaces.TestSession</remote> //指明ejb远程接口
</ejb-ref>
</web-app>b)每个服务器容器虽然遵循了sun j2ee的规范,但是在实现上有差异,以jboss为例,为了获得ejb的实际jndi名称(又一个名字!!!),jboss在jboss-web.xml做了如下指定:<jboss-web>
<ejb-ref>
<ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//ejb引用名,跟web.xml中的一致
<jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,容器唯一识别名
</ejb-ref>
</jboss-web>c)那么,如何通过jndi名来知道实际的bean呢?jboss里头在ejb-jar.xml来进行了描述,记住,ejb-jar.xml是sun的标准文件:<ejb-name>test/TestSession</ejb-name>//ejb名称,又一个名称!!! <home>test.interfaces.TestSessionHome</home>//实际的ejb内容
<remote>test.interfaces.TestSession</remote>//同上
<ejb-class>test.session.TestSessionBean</ejb-class>//同上
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>那么,这个文件又怎么同jndi名字挂上钩呢?以下又是jboss自己的文件了--jboss.xml:
<session>
<ejb-name>test/TestSession</ejb-name>//ejb名称,同ejb-jar.xml
<jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,ok,红军胜利会师
</session>3.总结:
程序里通过context来调用,部署文件的引用关系是:应用别名-->jndi名-->ejb名-->ejb类和接口.4.建议:弄清楚这几个名字的含义和关系,你就可以掌握部署的核心了。
解决方案 »
- jsp include 不动态
- 帮我解决个小问题啊
- My97DatePicker 控制两个月份为一年内
- 使用spring官方的spring flex结合第3方组件遇到很重要的问题
- asp.net的web控件,我很讨厌,可又不知jsp好在哪里?
- windows2000sp4+jdk1.5+resin2.12运行报错
- 今天打开JBuilder 突然发现无法debug项目?无法打开tool下的config server
- 如何将字符串1990-11-20转化成日期型1990-11-20?
- 请问哪位大侠有讨论区的源码?
- 请帮助!最好两天内搞定。本周内没有答案,就不用回答了。
- 关于jive,请多指教了!
- jb7+tomcat怎么开发一个struct应用?
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;public class s1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private UserHome userHome; //inite
public void init(ServletConfig config) throws ServletException {
super.init(config);
try
{
String initCtxFactory=getInitParameter(Context.INITIAL_CONTEXT_FACTORY);
String providerURL=getInitParameter(Context.PROVIDER_URL);
Properties env=new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,initCtxFactory);
env.put(Context.PROVIDER_URL,providerURL);
Context ctx=new InitialContext(env);
userHome=(UserHome)ctx.lookup("UserHome");
}
catch(Exception e){e.printStackTrace();}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String name=null;
String password=null;
try
{
out.println("hello,welcome to java world!!");
User user=(User)userHome.findByPrimaryKey("gongqh21");//问题就处在这里!??
out.println(user.getPassword());
out.println(user.getName()); }catch(Exception e){out.println(e.toString());}
} //Clean up resources
public void destroy()
{
}
}用IE打开Servlets使显示如下:
hello,welcome to java world!! java.lang.NullPointerException 是不是我那个findByPrimaryKey("gongqh21");用错了啊?!请教!!!!!!!
userHome=(UserHome)ctx.lookup("UserHome");
}
catch(Exception e){e.printStackTrace();}
在异常里头打印信息,或者调用之前判断userHome有没有已经被赋值。应该好解决,呵呵。
hello,welcome to java world!!
java.lang.NullPointerException //out.println(e.toString());java.lang.NullPointerException
结果两个都有异常!(原来的语句块和这个语句)
Starting WebLogic Server...<2003-4-12 下午12时57分58秒> <Notice> <Management> <140005> <Loading configuration D:\bea\user_projects\mydomain\.\config.xml> <2003-4-12 下午12时58分06秒> <Error> <EmbeddedLDAP> <171516> <Could not get exclusive access to the embedded LDAP data files directory: .\myserver\ldap\ldapfiles> <2003-4-12 下午12时58分06秒> <Critical> <WebLogicServer> <000364> <Server failed during initialization. Exception:weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing serverweblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)> <2003-4-12 下午12时58分06秒> <Emergency> <WebLogicServer> <000342> <Unable to initialize the server: Fatal initialization exception
Throwable: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server
weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)> ***************************************************************************The WebLogic Server did not start up properly.Exception raised: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)Reason: Fatal initialization exception
Throwable: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server
weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)***************************************************************************
请问这是什么原因?!该怎么处理?
这里的参数是pkclass ?
你用的是String...
看一下.
还有你的ejb到底怎么配置的,jndi name 是什么?
发布成功了吗?