|
Orion CMP Primer This article will show you how to write and run a simple Container-Managed Persistent Entity Bean using the Orion application server.
Introduction
Case description Step 1: Setup directory structure Step 2: Write the remote interface Step 3: Write the bean class Step 4: Write the home interface Step 5: Write the EJB deployment descriptor Step 6: Create the front end Step 7: Write the Web deployment descriptor Step 8: Write the J2EE application definition Step 9: Compile and create the .jar, .war and .ear files Step 10: Install the application Step 11: Bind the web application Step 12: Start Orion See also Credits 1 Introduction The Orion Primer showed you how to write, compile and deploy a servlet and a session bean. This article will show you how to write a Container Managed Entity Bean as well as an HTML page and a JSP that you can use to access the entity bean. ![]()
We will first draw a sketch of what we are going to build. Then we'll set up a directory structure for this, and write the necessary HTML, JSP and Java files. After that we will build the JAR, WAR and EAR files and deploy them into Orion. If you have any trouble, see if your question is answered by any of the resources at the bottom of this article. If it's not, try posting your question on the orion-interest mailing list (see the Orion website), or on the IRC channel #java on EFnet. Before you start, make sure you have downloaded and installed the following software: ![]()
2 Case description For this example, we will create a small address book application. Users will be able to list, add, edit and delete address entries. No security will be involved at this time, so all users will be able to perform these operations. Here's a sketch of what the structure of our J2EE application will be:
The web application will contain the files needed to implement the front end for our address book application. We will create a HTML page to represent the front page, and one JSP per action. 3 Step 1: Setup directory structure First thing we do, is set up a directory structure that will contain all of the files we write and those we generate. You should create the addressbook directory somewhere in your home directory, for example as
During this exercise we will put all
All
The directory
The generated files will end up in the
When archives are created, they will be placed under
![]()
4 Step 2: Write the remote interface Now for the back end we will write an interface for your bean, just as we did in the Orion Primer. Again the new interface will be derived from javax.ejb.EJBObject:
![]()
5 Step 3: Write the bean class
Again, we have the interface that the clients can use to modify an existing bean. So far there are little differences visible between the session bean developed in the Orion Primer trail, and the entity bean we are developing now. Now let's look at the entity bean. This is a class that must implement the interface javax.ejb.EntityBean
This class will need to contain the following things: The methods specified by the
The fields to be persisted (the name, address and city). The methods defined in EntityBean interface. Extra methods required for an entity bean:
The EntityBean interface defines the following methods: The EntityBean interface defines the following methods: The EJB specification mandates that we add an ejbCreate method too, and a corresponding ejbPostCreate: ![]()
Note that we have some additional functionality in our bean. We have a class field
For this, the bean uses a class called
![]()
6 Step 4: Write the home interface A
home interface is used to create and destroy instances of Enterprise JavaBeans. So we need a home interface for our
The home interface will be derived from
javax.ejb.EJBHome. We will call it
We need to use this interface from our JSP pages to get a reference to an instance of
![]()
This is all we need to do for our home interface. Orion will create an implementation for this interface that will implement the behaviour we need. Life doesn't have to be complicated ! 7 Step 5: Write the EJB deployment descriptor We need an XML deployment descriptor that will describe the EJB part of our application. In the deployment descriptor we describe every Enterprise JavaBean we would like to deploy. In our case, there is only one. This time it's an entity bean:
This deployment descriptor specifies a few characteristics for the entity bean, including the fields the EJB container (Orion in this case) should persist and what the primary key is. ![]()
8 Step 6: Create the front end
It is now time to create the files that implement the front end of our application. This is the web module. We will create a single HTML page as the front page, and 4 JSP files, one per action: We can visualize this as follows:
The idea of this article is not to teach you how to write JSP's, it helps you get your feet wet. See the links at the bottom of this article for links to more information that will help you improve your skills. ![]()
This is all there is to the front end. Take a good look at the code and make sure you get the general idea. The code contains quite some documentation to help you understand what goes on. 9 Step 7: Write the Web deployment descriptor The descriptor for our J2EE web module is very simple. With optional things removed it looks like this:
The
The
![]()
10 Step 8: Write the J2EE application definition Now we need to combine the EJB module and the web module into a J2EE application. For this we need to write a J2EE application deployment descriptor. This is an XML file that defines the modules of a Java 2 Enterprise Edition application, and how they should be deployed. In our case we can keep the file very simple:
The root tag is
![]()
11 Step 9: Compile and create the .jar, .war and .ear files
An
The
The file
For this purpose, I have written an Ant build file that will compile all the
![]()
In this file you may want to change one or two settings: We are now ready to generate the class files, the web module archive (
![]()
This will ( should) create the following files. Click on one to download the files generated on my system: 12 Step 10: Install the application To actually install our application, you need to add a line like the following to your server.xml in the Orion configuration directory (
In my case:
Please make sure that the process that will run Orion has
write access to the directory that contains the
13 Step 11: Bind the web application Now we need to bind the web application to the default site. This is done by adding the following line to the file
14 Step 12: Start Orion Everything should work, so let's start Orion. ![]()
You should see some log messages from Orion now. The exact messages depend on your version of Orion and configuration. On my system I got this:
Now open your favourite webbrowser, and point it to:
In my case I am running the Orion webserver on a different computer, that computer has IP address 10.0.0.1, and it runs Orion on port 9000, so I would open my browser to :
As soon as you access the web module, you should see a message appear in the screen you started Orion in, something like:
You should see the index.html file in your browser. It will look similar to this:
You can test the JSP pages by clicking on the link. You should see a screen indicating that there are no entries found, similar to the following:
Add entries using the Add entry link. After submitting a new address you will return to the Add address screen until you click the Back to the list link. After adding a few addresses the list page will look similar to this:
When you change anything in one of the deployment descriptors, JSP pages or Java source files, rerun ant to generate a new .ear file. Orion will detect the file has been changed and will redeploy the application. 15 See also See the following documents for more information on Orion: Orion Website (Orion) Orion Support Site ( Joseph B. Ottinger) Application Deployment (Orion) Orion Tutorial Index (Orion) See the following sites for more information on J2EE, EJB and JNDI: J2EE Homepage (JavaSoft) J2EE tutorial (JavaSoft) EJB tutorial (JavaSoft). J2EE Technology Center (JavaSoft) Topical index: Enterprise JavaBeans (JavaWorld) The JNDI Tutorial (JavaSoft) RMI over IIOP (JavaSoft) XML DTD for application.xml files (JavaSoft) XML DTD for ejb-jar.xml files (JavaSoft) XML DTD for web.xml files (JavaSoft) For more information about Ant, and to to download it, visit the following web pages: Ant Homepage (Apache) Download Ant (Apache) 16 Credits This article was written by Ernst de Haan ernst@jollem.com in 2001. Comments, additions, questions are welcome. Please include the revision number of this tutorial in your reply. Please redirect your flames to /dev/null. Thanks go to the following people for their invaluable comments, suggestions and improvements (in no particular order): Alex Chudnovsky chud@bigpond.net.au Sebastiaan van Erk sebster@sebster.com Scott Farquhar Scott.Farquhar@asx.com.au Dave Ford dford@smart-soft.com Mark Bernardinis mark@warezwally.net Thomas Plümpe thomanski@gmx.de Copyright © 2005 IronFlare AB |