Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : EJB : Building your first Enterprise JavaBean
 

Building your first Enterprise JavaBean

by Faisal Khan.Follow Faisal Khan on Twitter

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

 ( 3 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 32, Comments: 42 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Facing problem to Run EJB Sample
  2. How to make this?
  3. Best tutorial who want to start.
  4. Thanks very much very helpful
  5. Can I use Entity Bean with out session bean ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  6. Building your first Enterprise JavaBean
  7. Downloading EJB builder
  8. Exception while executing the above code
  9. how to debug in MyEclipse using JBoss server
  10. not able to deploy ejb,jsp in JBoss server
  11. HttpSession Replication
  12. How to Deploy the jar in Weblogic81?
  13. HTTP Status 404 - /firstEJB.jsp
  14. Regarding EJB tutorial
  15. ejb not bound ( 2 Replies )
  16. Ejb on Weblogic server using Eclipse ( 1 Reply )
  17. getting error while i am compiling FirstHome.java, how can i solve this error
  18. org.apache.jasper.JasperException: Unable to compile class for JSP
  19. Tomcat/Jboss: help me!! ( 1 Reply )
  20. Is it necessary to have EJBs deployed in EJBServer(JBoss) to be present in the client side
  21. not able to deploy jsp on tomcat server
  22. Not able to compile the java files.
  23. Great article. Thanks.
  24. Problem in EJB
  25. JBoss 3.2.1 and Tomcat 4.1.24 ( 2 Replies )
  26. JBoss 3.2.1 and Tomcat 4.1.18
  27. EJB on weblogic
  28. Thank you
  29. Return an object called client for example
  30. EJB Not Bound
  31. Tomcat/Jboss: help me !! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  32. Tomcat/JBoss : Port Issue ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.