July 21, 2018
Hot Topics:

Who Says EJB 2.1 is Too Hard?

  • October 11, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

EJBs, and in particular the EJB 2.1 spec, have a reputation of being powerful but complex. I have seen more arguments against using Java/J2EE because EJBs are too complex than for any other reason, and from many different sources. Common wisdom seems to be that J2EE and EJBs are synonymous; hence, J2EE is too complex to use.

Obviously, even casual inspection of this reasoning shows the flaws. For one thing, EJBs are just a part of the J2EE specification (or the Java EE specification as it is now known), and need not be used at all. I also take issue that the complexity is somehow the fault of EJBs. Certainly, they could be simplified (and EJB 3.0 goes a long way to doing that), but the real problem is that they deal with a very complex problem domain. Delivering components that have transactional support, security, location transparency, discovery, scalability and concurrency is not easy.

However, I will admit that using EJB 2.1 as detailed in the specification, with deployment descriptors, containers, and EJB lookup is quite a steep learning curve. It is a tough architecture to roll out to developers who need to be up and running fast, unless you have good tools that you can rely on to simplify the whole thing.

And that is what this article is about. It is intended to demonstrate that EJBs, even in their 2.1 form, need not be so scary, that you can jump in and with little time and effort have a working system that uses the full strength EJB architecture. Between using NetBeans to create the EJB, Sun App Server 8.1 to deploy it, and Java Studio Creator to consume it, you will not see a deployment descriptor, lookup code, or anything else complex (unless you really want to).

First Steps

This article can either be used as an introduction to the approach and tools available, or as a click-along tutorial. If you intend to do the latter, you will need to download NetBeans and Java Studio Creator 2 EA 2:

For NetBeans, you will need to download at least NetBeans 4.1. You can choose to download the NetBeans 5.0 Beta, but should know that if you do so you should get a package that includes the Sun App Server PE 8.1 (or download it separately). Unless you have the app server and have configured NetBeans to know about it, you will not be able to create an enterprise project in NetBeans.

Download NetBeans from http://www.netbeans.org.

For Java Studio Creator, you need to get the Early Access 2 release of Creator 2. To do this, go to http://developers.sun.com/prodtech/javatools/jscreator/ea/jsc2/index.html and register; you then will be able to download the EA2 version of Creator 2 that has all of the new EJB support.

This article demonstrates a very narrow part of the whole field of EJB creation and consumption. For one thing, I intend to demonstrate Session Beans only (I prefer using only Session beans for remote APIs) and will also skip the persistence part, prefering to concentrate on the creation of an API and the consumption within Java Studio Creator.

However, there are some excellent guides and tutorials that do a better job of demonstrating Entity EJBs (for persistence) in NetBeans, and also an alternative one that shows how Hibernate could be used for persistence instead (should that be your preference).

Using NetBeans to Create the Session API

First, you are going to use NetBeans to create your Session EJB-based API. To do this, start NetBeans and:

  1. From the Welcome page, select New Project.
  2. In the new project wizard, select Enterprise/EJB Module. Click Next.
  3. Choose a name for your EJB module (I suggest CreatorDemoEJB).
  4. Choose a directory for the project.
  5. For Server, you should have Sun Application Server 8.1 selected. If it is not available, you need to download it and configure it in NetBeans. The easier option is to download the NetBeans distribution that already includes it.
  6. Leave everything else default and click Finish.

Create a DTO Class

Before you do anything else, you need an object to pass data from the server to the client. You could certainly do this using arrays, but a full DTO (Data Transfer Object) lets you send structured data across from the server, and this will demonstrate some of the richer features in Java Studio Creator.

For this demo, you are going to create an object that holds some typical details about a person (like first and last name, telephone numbers, age, salary, and whether the person is on vacation—this last one is in there as a way to demonstrate boolean values).

So, create a new class called PersonDTO:

  1. In the top left pane, select the Projects tab and find the Source Packages node in the project tree, right-click on it, and select New->Java Class....
  2. In the dialog, type the name PersonDTO for the class name, and choose a suitable package. Click Finish.
  3. A new class will be created. Add the following code into it:
    • The DTO needs to be serializable, so add implements Serializable to the class definition.
    • In the Projects pane, expand the node for the new PersonDTO class, and the node inside that. You should see four nodes named Fields, Constructors, Methods, and Bean Patterns.
    • Right-click on Bean Patterns, and select Add->Property. In the dialog, add a property with name lastName and type String. This is the quick way to add new properties without having to write getter and setter methods (they are provided for you).
    • Repeat for the following fields: firstName of type String, homeTel of type String, workTel of type String, income of type double, age of type int, and onVacation of type boolean.
    • You are not doing any persistence code, so you need some test data. Add the following code to set some up:
    public static PersonDTO[] peopleList = null;
    static {
       // set up some demo data for the people list
       peopleList = new PersonDTO[10];
       peopleList[0]=new PersonDTO("Bloggs","Fred","770-555-1212",
       peopleList[1]=new PersonDTO("Schmo","Joe","404-555-3434",
       peopleList[2]=new PersonDTO("McTog","Nog","770-555-5555",
                                   "770-555-6666",25000.00,17, true);
       peopleList[3]=new PersonDTO("Pepper","Petey","770-555-5432",
       peopleList[4]=new PersonDTO("Fairlane","Sandy","770-555-9999",
       peopleList[5]=new PersonDTO("Lane","Lois","123-555-8833",
       peopleList[6]=new PersonDTO("Wesley","Tom","770-555-7654",
       peopleList[7]=new PersonDTO("McDunne","Diane","770-555-5275",
       peopleList[8]=new PersonDTO("Burt","George","770-555-8111",
       peopleList[9]=new PersonDTO("Smith","Anne","770-555-5656",
  4. You need to add a constructor to build the data using this information. In the class, add the following code:
  5. /**
     * PersonDTO constructor. Takes all fields to produce a ready
     *  populated Person Data Transfer Object
     * @param last String containing last name
     * @param first String containing first name
     * @param homeTel String containing home telephone
     * @param workTel String containing work telephone
     * @param income double containing person's income
     * @param age int age of person
     * @param onVac boolean flag set to true if person is on vacation
    public PersonDTO(String last,String first,String homeTel,
                     String workTel,double income,int age,
                     boolean onVac) {
       this.lastName = last;
       this.firstName = first;
       this.homeTel = homeTel;
       this.workTel = workTel;
       this.income = income;
       this.age = age;
       this.onVacation = onVac;

That's it for your DTO object. Now, on to the EJB.

Page 1 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date