---
title: "tomcat与jboss集成"
author: "cjc"
date: "2008-08-07T13:35:00Z"
tags: []
slug: "tomcat-jboss"
draft: false
lastmod: "2008-08-07T13:35:00Z"
id: 446879
url: "https://cjc.iteye.com/blog/446879"
---

好久没写blog了,最近在考虑openjweb迁移到jboss的问题,经过试验,发现tomcat与jboss结合是挺简单的,tomcat中如何连接jboss,其实只需要把jboss的client目录的jar包复制到tomcat的common\\lib中即可,我是复制的jboss开头的jar包,开发的EJB除了在jboss中部署外,ejb的home和接口类还要放到tomcat的web应用的WEB-INF/classes对应路径下,下面是tomcat web应用中一个ejb调用例子(testejb.jsp):

&lt;[%@page](mailto:%@page) import="com.test.\*,java.util.\*" %&gt;

&lt;%  
response.setContentType("text/html; charset=UTF-8");  
%&gt;  
&lt;%!  
com.test.TestEJBHome testHome= null;  
com.test.TestEJB test= null;

public void jspInit() {  
try{  
javax.naming.InitialContext ctx = getInitialContext();  
Object objref = null;  
if(testHome == null)  
objref = ctx.lookup("test111"); //ejb的名称  
testHome=(com.test.TestEJBHome)javax.rmi.PortableRemoteObject.narrow(objref, com.test.TestEJBHome.class);  
test = testHome.create();  
String s = test.getValue("abao");

System.out.println(test.getValue("abao")); //测试成功后输出hekki,abao

}catch(Exception e){ e.printStackTrace(); }  
}

public javax.naming.InitialContext getInitialContext() throws javax.naming.NamingException{  
System.out.println("init jndi be called....");  
java.util.Properties props = new java.util.Properties();  
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");  
props.put("java.naming.provider.url","localhost:1099"); //调用本地的jboss  
return new javax.naming.InitialContext(props);  
}

%&gt;

&lt;%

out.println("hello!");

%&gt;

下面是测试ejb的几个类:

//TestEJB接口

package com.test;

public interface TestEJB extends javax.ejb.EJBObject  
{

public String getValue(String name) throws java.rmi.RemoteException, Exception;

}

//session bean

package com.test;

import java.rmi.RemoteException;

import javax.ejb.EJBException;  
import javax.ejb.SessionContext;

public class TestEJBBean implements javax.ejb.SessionBean {

public void ejbActivate() throws EJBException, RemoteException {  
// TODO Auto-generated method stub

}

public void ejbPassivate() throws EJBException, RemoteException {  
// TODO Auto-generated method stub

}

public void ejbRemove() throws EJBException, RemoteException {  
// TODO Auto-generated method stub

}

public void setSessionContext(SessionContext arg0) throws EJBException,  
RemoteException {  
// TODO Auto-generated method stub

}

public String getValue(String name) throws java.rmi.RemoteException, Exception  
{  
return "hello,"+name;  
}

public void ejbCreate(){  
System.out.println("ejb create");  
//try{  
//ctx = new javax.naming.InitialContext();  
//}catch(Exception e){  
// e.printStackTrace();  
//}  
}

}

//home接口:

package com.test;

public interface TestEJBHome extends javax.ejb.EJBHome{  
public TestEJB create() throws javax.ejb.CreateException, java.rmi.RemoteException, Exception;

}

//部署文件ejb-jar.xml:

&lt;?xml version="1.0"?&gt;  
&lt;!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar\_1\_1.dtd'&gt;  
&lt;ejb-jar&gt;  
&lt;small-icon&gt;images/green-cube.gif&lt;/small-icon&gt;  
&lt;enterprise-beans&gt;  
&lt;session&gt;  
&lt;small-icon&gt;images/orange-cube.gif&lt;/small-icon&gt;  
&lt;ejb-name&gt;test111&lt;/ejb-name&gt;  
&lt;home&gt;com.test.TestEJBHome&lt;/home&gt;  
&lt;remote&gt;com.test.TestEJB&lt;/remote&gt;  
&lt;ejb-class&gt;com.test.TestEJBBean&lt;/ejb-class&gt;  
&lt;session-type&gt;Stateless&lt;/session-type&gt;  
&lt;transaction-type&gt;Container&lt;/transaction-type&gt;  
&lt;/session&gt;  
&lt;/enterprise-beans&gt;  
&lt;assembly-descriptor&gt;  
&lt;container-transaction&gt;  
&lt;method&gt;  
&lt;ejb-name&gt;test111&lt;/ejb-name&gt;  
&lt;method-intf&gt;Remote&lt;/method-intf&gt;  
&lt;method-name&gt;\*&lt;/method-name&gt;  
&lt;/method&gt;  
&lt;trans-attribute&gt;Supports&lt;/trans-attribute&gt;  
&lt;/container-transaction&gt;  
&lt;/assembly-descriptor&gt;  
&lt;/ejb-jar&gt;

//jboss.xml:

&lt;?xml version="1.0" encoding="euc-kr"?&gt;  
&lt;jboss&gt;  
&lt;enterprise-beans&gt;  
&lt;session&gt;  
&lt;ejb-name&gt;test111&lt;/ejb-name&gt;  
&lt;jndi-name&gt;test111&lt;/jndi-name&gt;  
&lt;/session&gt;  
&lt;/enterprise-beans&gt;  
&lt;resource-managers&gt;  
&lt;!----&gt;  
&lt;/resource-managers&gt;  
&lt;/jboss&gt;

部署时要打成ejb的jar包放到jboss的server\\default\\deploy中,jar包中除了有com为根目录的类外,在META-INF目录中还要放置上面两个xml文件.

其实tomcat的web应用中调用ejb的关键就是tomcat中要放置ejb客户端jar包以及在jndi调用中指定ejb服务器地址.