Home Ejb Ejbtutorial Building a Simple EJB Application Tutorial

Related Tutorials


 
 

Share on Google+Share on Google+

Building a Simple EJB Application Tutorial

Advertisement
In this tutorial we will create a simple session EJB and a client web application using eclipse IDE along with Lomboz plug in and XDoclet.

Building a Simple EJB Application - A Tutorial

     

By Jeevaraj Gnanaselvan Dhanaraj (jeevaraj_1970@yahoo.com) (Jeeva has over 7 years of experience in designing and developing enterprise class web applications using JAVA and J2EE technologies. He currently works for Itreya Technologies, Bangalore, leading a team of over 10 programmers and designers, developing a multi-user, distributed, web-based workflow application)

 Introduction

In this tutorial we will create a simple session EJB and a client web application using eclipse IDE along with Lomboz plug in and XDoclet. This application,    while simple, provides a good introduction to EJB development and some of the Web development tools available.

Environment

J2SDK 1.4.2
http://java.sun.com/

Eclipse 3.1

http://www.eclipse.org/

JBoss 4.0.2

http://www.jboss.org/  

XDoclet 1.2.3
http://xdoclet.sourceforge.net

Lomboz 3.1RC2
http://lomboz.objectweb.org/

Installation

Install JDK (in D:\j2sdk1.4.2_04)

Install JBoss (in E:\jboss-4.0.2)

Install Xdoclet (in D:\xdoclet\xdoclet-1.2.3)

Install Eclipse (in E:\Eclipse3.1)

Install Lomboz (in E:\Eclipse3.1) 

Setting up

Set up the installed JRE in eclipse (Windows -> Preferences -> Java -> Installed JREs)

 


Set up the installed runtime for server in eclipse (Windows -> Preferences -> Server -> Installed Runtimes)

Set up Xdoclet in eclipse (Windows -> Preferences -> J2EE Annotations -> XDoclet)

Set up the ejbdoclet for JBoss in eclipse (Windows -> Preferences -> J2EE Annotations -> Xdoclet -> ejbdoclet)

Creating a Session Bean

Open the J2EE perspective in eclipse (Windows -> Open Perspective -> Other -> J2EE)

Create a new EJB Project from the Project Explorer (EJB Projects -> New -> EJB Project)

Enter name as "SimpleEJBTutorial" and select project location as "E:\Test".

Now create a new Session Bean from the Project Explorer (EJB Projects -> Simple EJB Tutorial -> ejbmodule -> New -> Other)

Note:

XDoclet is an extended Javadoc Doclet engine. It's a generic Java template engine that lets us create custom Javadoc @tags and based on those @tags generate source code or other files (such as deployment descriptors in xml form). XDoclet supports a set of common standard tasks such as web.xml or ejb-jar.xml generation. It uses special JavaDoc @tags to define settings for each component. For example, putting a @ejb.bean name="Hello" jndi-name="Hello" type="Stateless" in HelloBean.java
We have to edit only the HelloBean.java. All others are automatically generated and XDoclet will regenerate them each time we make a change to the HelloBean class.

Edit the HelloBean class and change the foo method to

  public String sayHello(String param) {
  return "Hello..";
  }

The XDoclet builder will start working again and update our classes. At the end our projects will look like: 

The highlighted classes HelloBean.java and HelloSession.java are server side classes and Hello.java and HelloHome.java are public interfaces. These are the classes that will be needed by all clients.

Deploying the Session Bean

Open the Servers view (Windows -> Show View -> Servers)

Create a new server by right-clicking inside the "Servers" view.

Choose SimpleEJBTutorial from the available projects.

Ensure that the option "Automatically publish when starting server" is checked.

Look for console messages such as:

22:02:13,274 INFO [EjbModule] Deploying Hello
22:02:13,556 INFO [EJBDeployer] Deployed: file:/E:/jboss-4.0.2/server/default/deploy/SimpleEJBTutorial.jar

Now the EJB has been defined and deployed. It is ready to be used. We can now build a client to make the HelloBean say Hello.

Creating a Web client Application

Create a new Dynamic Web Project namely ?HelloWeb? from the Project Explorer (Dynamic Web Projects -> New -> Dynamic Web Project)

We will need to access the EJB interface types such as Hello and HelloHome in client applications.

These classes are required to be added in the build path.  

In the Project Explorer, right click on the HelloWeb dynamic web project and Choose Properties...

In the Java buildpath, add the SimpleEJBTutorial project to the project references. This will help allows us to compile against the latest ejb client classes in this web project.

Create a new "test.jsp" under the HelloWeb\WebContent  directory.

Open test.jsp in the JSP source page editor.

Add the following lines in the "body" of test.jsp

<%
com.tutorial.Hello hello = null;
try{
com.tutorial.HelloHome home = com.tutorial.HelloUtil.getHome();
hello = home.create();
}catch(Exception exception)
{
}
%>
<b><%= hello.sayHello(" my friend !") %></b>

Save the test.jsp. Our web application is now complete.

Deploying and running the Web client Application

Right click on test.jsp and select Run As -> Run on Server...

Look for something like: [TomcatDeployer] deploy, ctxPath=/HelloWeb, warUrl=file:/E:/jboss-4.0.2/server/default/tmp/deploy/tmp52097HelloWeb-exp.war/ on the console.

Same application can be invoked from browser using the url, http://localhost:8080/HelloWeb/test.jsp

Experiment adding more methods and ejbs.

Conclusion

In this tutorial we learned how to configure Eclipse to work with XDoclet and create a J2EE EJB project that has a Session Bean. We created a client Web application. This application, while simple, provides a good introduction to EJB development and some of the Web development tools available.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2005

Related Tutorials

Discuss: Building a Simple EJB Application Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Jeevan Reddy
May 29, 2011
can we integrate jboss 6.0 with eclipse 3.1

Hi, can we integrate jboss 6.0 with eclipse 3.1. if so can you please forward the steps to my email id. thanks, Jeevan
Amol
August 18, 2011
Very helpful

I found this tutorial very useful.Thank you.
poo
February 29, 2012
Thanks

This post was very helpful and it just worked...
Victor Trapiello
May 22, 2012
Doubt

Hi, first of all congrat for this tutorial, I have a legacy project and Im studying these old fashion technologies, following your example it pops up an error with Xdoclet Buildfile: C:\workspaceSTS\.metadata\.plugins\org.eclipse.jst.j2ee.ejb.annotations.xdoclet\tempAnt.xml init: ejbdoclet: BUILD FAILED C:\workspaceSTS\.metadata\.plugins\org.eclipse.jst.j2ee.ejb.annotations.xdoclet\tempAnt.xml:57: taskdef class xdoclet.modules.ejb.EjbDocletTask cannot be found using the classloader AntClassLoader[C:\workspaceSTS\ejb21Test\build\classes;C:\jboss-4.0.2\lib\commons-httpclient.jar;C:\jboss-4.0.2\lib\commons-logging.jar;C:\jboss-4.0.2\lib\concurrent.jar;C:\jboss-4.0.2\lib\dom4j.jar;C:\jboss-4.0.2\lib\endorsed\resolver.jar;C:\jboss-4.0.2\lib\endorsed\xalan.jar;C:\jboss-4.0.2\lib\endorsed\xercesImpl.jar;C:\jboss-4.0.2\lib\endorsed\xml-apis.jar;C:\jboss-4.0.2\lib\getopt.jar;C:\jboss-4.0.2\lib\gnu-regexp.jar;C:\jboss-4.0.2\lib\jaxen.jar;C:\jboss-4.0.2\lib\jboss-common.jar;C:\jboss-4.0.2\lib\jboss-jmx.jar;C:\jboss-4.0.2\lib\jboss-system.jar;C:\jboss-4.0.2\lib\log4j-boot.jar;C:\jboss-4.0.2\lib\namespace.jar;C:\jboss-4.0.2\lib\webdavlib.jar;C:\jboss-4.0.2\server\default\lib\activation.jar;C:\jboss-4.0.2\server\default\lib\autonumber-plugin.jar;C:\jboss-4.0.2\server\default\lib\bcel.jar;C:\jboss-4.0.2\server\default\lib\bindingservice-plugin.jar;C:\jboss-4.0.2\server\default\lib\bsh-1.3.0.jar;C:\jboss-4.0.2\server\default\lib\bsh-deployer.jar;C:\jboss-4.0.2\server\default\lib\commons-httpclient.jar;C:\jboss-4.0.2\server\default\lib\commons-logging.jar;C:\jboss-4.0.2\server\default\lib\log4j.jar;C:\jboss-4.0.2\server\default\lib\hsqldb-plugin.jar;C:\jboss-4.0.2\server\default\lib\hsqldb.jar;C:\jboss-4.0.2\server\default\lib\javax.servlet.jar;C:\jboss-4.0.2\server\default\lib\javax.servlet.jsp.jar;C:\jboss-4.0.2\server\default\lib\jboss-common-jdbc-wrapper.jar;C:\jboss-4.0.2\server\default\lib\jboss-j2ee.jar;C:\jboss-4.0.2\server\default\lib\jboss-jaxrpc.jar;C:\jboss-4.0.2\server\default\lib\jboss-jca.jar;C:\jboss-4.0.2\server\default\lib\jboss-jsr77.jar;C:\jboss-4.0.2\server\default\lib\jboss-management.jar;C:\jboss-4.0.2\server\default\lib\jboss-monitoring.jar;C:\jboss-4.0.2\server\default\lib\jboss-saaj.jar;C:\jboss-4.0.2\server\default\lib\jboss-transaction.jar;C:\jboss-4.0.2\server\default\lib\jboss.jar;C:\jboss-4.0.2\server\default\lib\jbossmq.jar;C:\jboss-4.0.2\server\default\lib\jbosssx.jar;C:\jboss-4.0.2\server\default\lib\jmx-adaptor-plugin.jar;C:\jboss-4.0.2\server\default\lib\jnpserver.jar;C:\jboss-4.0.2\server\default\lib\jpl-pattern.jar;C:\jboss-4.0.2\server\default\lib\jpl-util.jar;C:\jboss-4.0.2\server\default\lib\mail-plugin.jar;C:\jboss-4.0.2\server\default\lib\mail.jar;C:\jboss-4.0.2\server\default\lib\properties-plugin.jar;C:\jboss-4.0.2\server\default\lib\scheduler-plugin-example.jar;C:\jboss-4.0.2\server\default\lib\scheduler-plugin.jar;C:\jboss-4.0.2\server\default\lib\scout.jar;C:\jboss-4.0.2\server\default\lib\snmp-support.jar;C:\jboss-4.0.2\server\default\lib\webcallbackhandler.jar;C:\jboss-4.0.2\client\activation.jar;C:\jboss-4.0.2\client\avalon-framework.jar;C:\jboss-4.0.2\client\axis-ws4ee.jar;C:\jboss-4.0.2\client\commons-discovery.jar;C:\jboss-4.0.2\client\commons-logging.jar;C:\jboss-4.0.2\client\log4j.jar;C:\jboss-4.0.2\client\concurrent.jar;C:\jboss-4.0.2\client\getopt.jar;C:\jboss-4.0.2\client\jacorb.jar;C:\jboss-4.0.2\client\jboss-client.jar;C:\jboss-4.0.2\client\jboss-common-client.jar;C:\jboss-4.0.2\client\jboss-deployment.jar;C:\jboss-4.0.2\client\jboss-iiop-client.jar;C:\jboss-4.0.2\client\jboss-j2ee.jar;C:\jboss-4.0.2\client\jboss-jaxrpc.jar;C:\jboss-4.0.2\client\jboss-jsr77-client.jar;C:\jboss-4.0.2\client\jboss-juddiaxis.jar;C:\jboss-4.0.2\client\jboss-saaj.jar;C:\jboss-4.0.2\client\jboss-system-client.jar;C:\jboss-4.0.2\client\jboss-transaction-client.jar;C:\jboss-4.0.2\client\jboss-ws4ee-client.jar;C:\jboss-4.0.2\client\jbossall-client.jar;C:\jboss-4.0.2\client\jbosscx-client.jar;C:\jboss-4.0.2\client\jbossha-client.jar;C:\jboss-4.0.2\client\jbossjmx-ant.jar;C:\jboss-4.0.2\client\jbossmq-client.jar;C:\jboss-4.0.2\client\jbosssx-client.jar;C:\jboss-4.0.2\client\jmx-client.jar;C:\jboss-4.0.2\client\jmx-invoker-adaptor-client.jar;C:\jboss-4.0.2\client\jnp-client.jar;C:\jboss-4.0.2\client\logkit.jar;C:\jboss-4.0.2\client\mail.jar;C:\jboss-4.0.2\client\namespace.jar;C:\jboss-4.0.2\client\scout.jar;C:\jboss-4.0.2\client\wsdl4j.jar;C:\ant\lib\ant.jar;C:\XDOCLET_HOME\xdoclet-1.2.1.jar;C:\XDOCLET_HOME\xjavadoc-1.0.3.jar;C:\ant\lib\ant-antlr.jar;C:\ant\lib\ant-apache-bcel.jar;C:\ant\lib\ant-apache-bsf.jar;C:\ant\lib\ant-apache-log4j.jar;C:\ant\lib\ant-apache-oro.jar;C:\ant\lib\ant-apache-regexp.jar;C:\ant\lib\ant-apache-resolver.jar;C:\ant\lib\ant-apache-xalan2.jar;C:\ant\lib\ant-commons-logging.jar;C:\ant\lib\ant-commons-net.jar;C:\ant\lib\ant-jai.jar;C:\ant\lib\ant-javamail.jar;C:\ant\lib\ant-jdepend.jar;C:\ant\lib\ant-jmf.jar;C:\ant\lib\ant-jsch.jar;C:\ant\lib\ant-junit.jar;C:\ant\lib\ant-junit4.jar;C:\ant\lib\ant-launcher.jar;C:\ant\lib\ant-netrexx.jar;C:\ant\lib\ant-swing.jar;C:\ant\lib\ant-testutil.jar;C:\sts\sts-2.9.1.RELEASE\configuration\org.eclipse.osgi\bundles\138\1\.cp\lib\antdebug.jar;C:\sts\sts-2.9.1.RELEASE\configuration\org.eclipse.osgi\bundles\138\1\.cp\lib\remote.jar;C:\sts\sts-2.9.1.RELEASE\configuration\org.eclipse.osgi\bundles\139\1\.cp\lib\remoteAnt.jar;C:\Program Files\Java\jdk1.7.0\lib\tools.jar;C:\sts\sts-2.9.1.RELEASE\plugins\org.eclipse.swt.win32.win32.x86_3.7.2.v3740f.jar] Total time: 2 seconds
VIKAS SINGH
July 18, 2012
EJB IN ECLIPSE WITH J BOSS

I wanted to ask u how can create on jboss in eclipse pls give kind of idea. i have tried to more time but there r getting some problems REGDS. VIKAS SINGH
Kofi
September 16, 2012
EJB

Thanks a lot Jeevaraj. A very good starter but needed more explanation. Good job.
DMCA.com