myeclipse+jboss创建Ejb/EAR详细步骤

我用的是myeclipse8.6、jboss-5.1.0。

ear项目=web项目+ejb项目,所以这也可以说是学习ejb工程。

各种jvm环境的配置就不说了,直接说正事:

创建Enterprise Application Project即EAR项目

接下来Next,然后默认到完成,就会自动生成两个项目:earEJB和earWeb。这样就三个项目了:

现在来看一下最后的目录结构吧:

earEJB:                                                                                     earWeb:

          

总的EAR项目,会自动生成:

关于XDoclet大家肯定有所了解,这里不再啰嗦,只给出最后的配置:

其他的建包,建bean,run xdoclet什么的大家肯定都懂的,不懂自己去查。现在上代码:

在earEJB项目中创建"ejb3 session bean":(注意按我所给图上的目录包层次创建)

ConverterBean:

 

/*
 * Created on 2004-7-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.ejb;
import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
 * @ejb.bean name="Converter"
 *	jndi-name="ConverterBean"
 *	type="Stateless" 
 * 
 *--
 * This is needed for JOnAS.
 * If you are not using JOnAS you can safely remove the tags below.
 * @jonas.bean ejb-name="Converter"
 *	jndi-name="ConverterBean"
 * 
 *--
 **/
public class ConverterBean implements SessionBean {
/**
 * @ejb.interface-method
 *	view-type="remote" 
**/
public String toLowerCase(String s){ 
	return s.toLowerCase(); 
}
/**
 * @ejb.interface-method
 *	view-type="remote" 
**/
public String toUpperCase(String s){ 
	return s.toUpperCase();
}
/**
 * @return 
 * @ejb.interface-method
 *	view-type="remote" 
**/
public void ejbCreate() throws CreateException {
    // TODO Add ejbCreate method implementation
}
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
	
}
}

其他的run xdoclet自动生成。可以删除不需要的 .java文件。
然后在earWeb项目中编写测试程序:

新建一个Servlet:testConverter.java:(注:设置<url-mapping>/convert</url-mapping>)

/*
 * Created on 2004-9-17
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import com.interfaces.*;

/**
 * @author aa
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class testConverter extends HttpServlet {
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		//TODO Method stub generated by Lomboz
//		TODO Method stub generated by Lomboz
//		TODO Method stub generated by Lomboz
		response.setContentType("text/html");
		PrintWriter out =response.getWriter();
		out.println("<html><head><title>the first EJB</title></head>");
		try{
		InitialContext ctx=new InitialContext();
		Object objRef = ctx.lookup("ConverterBean");
//		主接口
		ConverterHome
		home=(ConverterHome)javax.rmi.PortableRemoteObject.narrow(
		objRef,ConverterHome.class);
//		组件接口
		Converter bean =home.create();
		out.println(bean.toLowerCase("HOW ARE YOU"));
		out.println(bean.toUpperCase("how are you"));
		}catch(javax.naming.NamingException ne){
		out.println("Naming Exception caught:"+ne);
		ne.printStackTrace(out);
		}catch(javax.ejb.CreateException ce){
		out.println("Create Exception caught:"+ce);
		ce.printStackTrace(out);
		}catch(java.rmi.RemoteException re){
		out.println("Remote Exception caught:"+re);
		re.printStackTrace(out);
		}
		out.println("</body></html>");

	}
}

http://localhost:8080/earWeb/convert 运行时会报错:

Warning: A Session bean must define at least one ejbCreate method.
Warning: Session bean class must not be abstract.

故而上面的代码还要改进一下,才不会出错:

针对第一个警告:加上ejbCreate方法就是了:

public void ejbCreate() throws CreateException { }  

针对第二个警告:使类不抽象就是了,去掉ConvertBean.java中的abstract关键字,然后:


(大家可能觉得,为何不直接给代码,我想说的是:有错误出现时多看看错误提示!)

项目在Jboss下的部署就不啰嗦了吧,重启jboss,最后上效果:

本人花了两天时间才搞定这个,用weblogic感觉没有用jboss简单,不过总算是成功了!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值