Building your first Enterprise JavaBeanby Faisal Khan.
Overview :
In this tutorial we will learn how to create our first Enterprise JavaBean. We will then deploy this EJB on
a production class, open source, and free EJB Server; JBoss.
JBoss is a really popular EJB Container and is used by quite a lot of organizations World wide. We will thus
also learn how to install and run JBoss Server. The client for our EJB will be a JSP page running in a separate Tomcat
Server.
This tutorial consists of following topics :
i. Installing and Running JBoss Server :
You can download JBoss from JBoss web site. Current stable version
is 2.4.3. Download it from their web site. Once you have downloaded it, unzip the JBoss zip file into some directory e.g.
C:\JBoss. The directory structure should be something like following :
C:\JBoss
admin
bin
client
conf
db
deploy
lib
log
tmp
This is it as far as installation of JBoss is concerned. Now to start JBoss with default configuration go to
JBoss/bin directory and run the following command at the DOS prompt :
C:\JBoss\bin>run
run.bat is a batch file which starts the JBoss Server. Once JBoss Server starts, you should see huge lines of
text appearing on your command prompt screen. These lines show that JBoss Server is starting. Once JBoss startup
is complete you should see a message like following on your screen :
[Default] JBoss 2.4.3 Started in 0m:11s
Well done, you just installed and ran JBoss successfully on your system for the first time. To stop JBoss,
simply press Ctrl + C on the command prompt and JBoss will stop, again after displaying huge lines of text.
ii. Installing, Configuring and Running Tomcat Server :
First download latest stable release of Tomcat Server from
Tomcat web site. Current latest stable release is 4.0.1. If you are on Windows platform then you have the
luxury of downloading Tomcat in an executable ( .exe ) format. So from the different types of Tomcat files
available for download, select following :
jakarta-tomcat-4.0.1.exe
Once download is complete, double click this file to start the process of Tomcat installation. Installation is
quite straight forward. During setup if you are running Windows NT/2K/XP, you'll be asked if you want to install
it as a service, select 'yes' if you know what as a service is and how to start and stop it. Basically allowing the
Tomcat to be installed a service means that you will be able to auto-start Tomcat when Windows starts and
secondly Tomcat will run in the background and you won't have to keep one command prompt window open while it is
running. Both of these features are great if you are using Tomcat on production machines. During development I
will suggest that you don't install Tomcat as a service.
It will also ask you where you want it to be installed. Give it any location you want or simply allow it to
be installed on the default location of C:\Program Files\Apache Tomcat 4.0.
Before starting Tomcat, let's first configure it a bit on the next page.
Configuring and Running Tomcat :
Create a new folder under the main C:\ drive and name it "Projects". Now create a new sub-folder in the C:\Projects
folder and name it "TomcatJBoss". The directory structure should look like following :
C:\Projects
TomcatJBoss
Now open conf/Server.xml file from within the Tomcat directory where you
have installed it. By default this location will be :
C:\Program Files\Apache Tomcat 4.0\conf\server.xml
Some where in the middle where you can see multiple <Context> tags, add following lines between other
<Context> tags :
<!-- Tomcat JBoss Context -->
<Context path="/jboss" docBase="C:\Projects\TomcatJBoss\" debug="0"
reloadable="true" />
Now save Server.xml file. Go to Start -> Programs -> Apache Tomcat 4.0 -> Start Tomcat, to start Tomcat
Server. If everything has been setup correctly, you should see following message on your
command prompt.
Starting service Tomcat-Standalone
Apache Tomcat/4.0.1
Note: Creating C:\Projects\TomcatJBoss folder and setting up new /jboss context in Tomcat
is NOT required as far as accessing EJBs is concerned. We are doing it only to put our JSP client in a separate folder
so as not to intermingle it with your other projects.
iii. Developing your first Session EJB :
As we learned in "An Introduction to Enterprise JavaBeans" article, Session
EJBs are responsible for maintaining logic in our J2EE applications. What we did not say in that article was that
Session beans are also the simplest EJBs to develop. So that's why our first EJB will be a Session bean.
As you will see in a moment, every EJB class file has two accompanying interfaces and one XML file.
The two interfaces are Remote and Home interfaces. Remote interface is what the client gets to work with, in other
words Remote interface should contain the methods you want to expose to your clients. Home interface is actually EJB
builder and should contain methods used to create Remote interfaces for your EJB. By default Home interface must contain
at least one create() method. The actual implementation of these interfaces, our Session EJB class file remains
hidden from the clients. The XML file we talked about is named as "ejb-jar.xml" and is used to configure the EJBs
during deployment. So in essence our EJB, which we are going to create ( we will call it FirstEJB from now onwards )
consists of following files :
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
META-INF
ejb-jar.xml
First.java will be the Remote interface we talked about. FirstHome.java is our Home interface and FirstEJB.java
is the actual EJB class file. The ejb-jar.xml deployment descriptor file goes in the META-INF folder.
Create a new folder under the C:\Projects folder we had created earlier, and name it as "EJB". Now
create new sub-folder under C:\Projects\EJB folder and name it as "FirstEJB". Create a new folder "src" for
Java source files in the "FirstEJB' folder. The directory structure should look like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
Now create directory structure according to the package com.stardeveloper.ejb.session in the "src"
folder. If you know how package structure is built, it shouldn't be a problem. Anyhow, the final directory
structure should like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
com
stardeveloper
ejb
session Overview :
In this tutorial we will learn how to create our first Enterprise JavaBean. We will then deploy this EJB on
a production class, open source, and free EJB Server; JBoss.
JBoss is a really popular EJB Container and is used by quite a lot of organizations World wide. We will thus
also learn how to install and run JBoss Server. The client for our EJB will be a JSP page running in a separate Tomcat
Server.
This tutorial consists of following topics :
i. Installing and Running JBoss Server :
You can download JBoss from JBoss web site. Current stable version
is 2.4.3. Download it from their web site. Once you have downloaded it, unzip the JBoss zip file into some directory e.g.
C:\JBoss. The directory structure should be something like following :
C:\JBoss
admin
bin
client
conf
db
deploy
lib
log
tmp
This is it as far as installation of JBoss is concerned. Now to start JBoss with default configuration go to
JBoss/bin directory and run the following command at the DOS prompt :
C:\JBoss\bin>run
run.bat is a batch file which starts the JBoss Server. Once JBoss Server starts, you should see huge lines of
text appearing on your command prompt screen. These lines show that JBoss Server is starting. Once JBoss startup
is complete you should see a message like following on your screen :
[Default] JBoss 2.4.3 Started in 0m:11s
Well done, you just installed and ran JBoss successfully on your system for the first time. To stop JBoss,
simply press Ctrl + C on the command prompt and JBoss will stop, again after displaying huge lines of text.
ii. Installing, Configuring and Running Tomcat Server :
First download latest stable release of Tomcat Server from
Tomcat web site. Current latest stable release is 4.0.1. If you are on Windows platform then you have the
luxury of downloading Tomcat in an executable ( .exe ) format. So from the different types of Tomcat files
available for download, select following :
jakarta-tomcat-4.0.1.exe
Once download is complete, double click this file to start the process of Tomcat installation. Installation is
quite straight forward. During setup if you are running Windows NT/2K/XP, you'll be asked if you want to install
it as a service, select 'yes' if you know what as a service is and how to start and stop it. Basically allowing the
Tomcat to be installed a service means that you will be able to auto-start Tomcat when Windows starts and
secondly Tomcat will run in the background and you won't have to keep one command prompt window open while it is
running. Both of these features are great if you are using Tomcat on production machines. During development I
will suggest that you don't install Tomcat as a service.
It will also ask you where you want it to be installed. Give it any location you want or simply allow it to
be installed on the default location of C:\Program Files\Apache Tomcat 4.0.
Before starting Tomcat, let's first configure it a bit on the next page.
Configuring and Running Tomcat :
Create a new folder under the main C:\ drive and name it "Projects". Now create a new sub-folder in the C:\Projects
folder and name it "TomcatJBoss". The directory structure should look like following :
C:\Projects
TomcatJBoss
Now open conf/Server.xml file from within the Tomcat directory where you
have installed it. By default this location will be :
C:\Program Files\Apache Tomcat 4.0\conf\server.xml
Some where in the middle where you can see multiple <Context> tags, add following lines between other
<Context> tags :
<!-- Tomcat JBoss Context -->
<Context path="/jboss" docBase="C:\Projects\TomcatJBoss\" debug="0"
reloadable="true" />
Now save Server.xml file. Go to Start -> Programs -> Apache Tomcat 4.0 -> Start Tomcat, to start Tomcat
Server. If everything has been setup correctly, you should see following message on your
command prompt.
Starting service Tomcat-Standalone
Apache Tomcat/4.0.1
Note: Creating C:\Projects\TomcatJBoss folder and setting up new /jboss context in Tomcat
is NOT required as far as accessing EJBs is concerned. We are doing it only to put our JSP client in a separate folder
so as not to intermingle it with your other projects.
iii. Developing your first Session EJB :
As we learned in "An Introduction to Enterprise JavaBeans" article, Session
EJBs are responsible for maintaining logic in our J2EE applications. What we did not say in that article was that
Session beans are also the simplest EJBs to develop. So that's why our first EJB will be a Session bean.
As you will see in a moment, every EJB class file has two accompanying interfaces and one XML file.
The two interfaces are Remote and Home interfaces. Remote interface is what the client gets to work with, in other
words Remote interface should contain the methods you want to expose to your clients. Home interface is actually EJB
builder and should contain methods used to create Remote interfaces for your EJB. By default Home interface must contain
at least one create() method. The actual implementation of these interfaces, our Session EJB class file remains
hidden from the clients. The XML file we talked about is named as "ejb-jar.xml" and is used to configure the EJBs
during deployment. So in essence our EJB, which we are going to create ( we will call it FirstEJB from now onwards )
consists of following files :
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
META-INF
ejb-jar.xml
First.java will be the Remote interface we talked about. FirstHome.java is our Home interface and FirstEJB.java
is the actual EJB class file. The ejb-jar.xml deployment descriptor file goes in the META-INF folder.
Create a new folder under the C:\Projects folder we had created earlier, and name it as "EJB". Now
create new sub-folder under C:\Projects\EJB folder and name it as "FirstEJB". Create a new folder "src" for
Java source files in the "FirstEJB' folder. The directory structure should look like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
Now create directory structure according to the package com.stardeveloper.ejb.session in the "src"
folder. If you know how package structure is built, it shouldn't be a problem. Anyhow, the final directory
structure should like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
com
stardeveloper
ejb
session
|