Enterprise JavaBeans: Getting Started with EJB, Part 1
Java Programming, Lecture Notes #1310
First in a miniseries
This lesson is also the first lesson in a miniseries of several lessons intended to get you up and running with Sun's Enterprise JavaBeans(TM) very quickly.
First you learn to step, then you learn to run
The lessons in this miniseries will show you the steps involved in writing, compiling, deploying and executing a simple bean. However, the miniseries won't expend much effort explaining why you need to take those steps.
More details in subsequent lessons
Subsequent lessons will come back and fill in the gaps to give you a better understanding of each of the steps.
Subsequent lessons will also teach you about the different kinds of beans and how to use those beans in different situations.
Overview of the steps
Here are the required steps to get your first Enterprise JavaBeans(TM) up and running very quickly.
- Download and install Java 2 Platform, Standard Edition from Sun. As of the date of this writing, it can be found at the following URL: http://java.sun.com/j2se/1.3/
- Download and install the Java 2 Platform, Enterprise Edition. As of the date of this writing it can be found at the following URL: http://java.sun.com/j2ee/download.html
- Write the bean class and any helper classes that the bean may need.
- Write two interfaces known as the home and remote interfaces.
- Write a batch file to control the compilation process for the bean and the interface files.
- Use the batch file to compile the bean class, any helper classes, and the two interfaces.
- Use Sun's J2EE deploytool program to deploy the bean into the server, being careful to save the client jar file. The client jar file contains some class files for stubs and skeletons that are required by the client.
- Write the client program.
- Write a batch file to control the compilation of the client program.
- Use the batch file to compile the client program.
- Copy the client jar file into the directory containing the client class files.
- Write a batch file to control execution of the client program.
- Start the server running.
- Use the batch file to execute the client file, which in turn will cause the bean to be executed in the server.
(Note that the use of batch files is optional. However, I find that the use of batch files makes it easier for me to get it right.)
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth in the material to refer back to previously-discussed material without losing the place where you are reading.
Recommended supplementary reading
It is strongly recommended that you study the previous lessons on Java 2 Platform Enterprise Edition before embarking on this lesson.
I will show you the specific steps required to get a bean up and running in Sun's J2EE environment. I will provide several explanations as to "What is an EJB?" In addition, I will discuss the purpose and goal of enterprise beans, along with a very brief description of how that goal is achieved.
Rather than to discuss EJB servers in a generic sense, I plan to teach you specifically how to deploy beans in the Java 2 Platform Reference Implementation. I will do this in the believe that early success with a specific implementation will make it easier for you to expand your knowledge into the more general world of EJB servers from different vendors.
Because of the likelihood of confusion regarding the use of the term beans, I will begin by telling you that an EJB is not a JavaBean Component(TM), as discussed in my series of tutorial lessons beginning with lesson number 500.
Here is what Sun has to say on the matter: "Enterprise JavaBeans(TM) is the server side component architecture for the J2EE platform. EJB enables rapid and simplified development of distributed, transactional, secure and portable Java applications."
And finally, Richard Monson-Haefel has this to say in his excellent book entitled Enterprise JavaBeans, from O'Reilly: "Enterprise JavaBeans is a standard component model for component transaction monitors."
Now that you have been inundated with definitions, lets see what enterprise beans might be good for.
Purpose of a bean
The purpose of a bean is to function as a reusable software component in the assembly of large-scale server-side enterprise applications.
The goal of the combination of EJB and the Java 2 Platform Enterprise Edition (J2EE) is to reduce the development cost and reduce the time-to-market for new enterprise applications or upgrades to existing enterprise applications.
Achievement of goal
This goal is achieved by wrapping the beans in a special container, which, in combination with the server, handles many of the complex essential services required for enterprise applications. These services include concurrency, transactions, persistence, distributed objects, naming, and security (see Enterprise JavaBeans by Richard Monson-Haefel).
Because the server and container provide these services, the application programmer is free to concentrate on writing code to implement business logic rather than having to write code to provide the complex services.
Purpose of these lessons
A number of excellent books have been written about beans (such as the book by Monson-Haefel mentioned above) that discuss historical perspectives, theory, and details on such topics as component transaction monitors and security.
The purpose of these lessons is not to repeat that information. Rather, the purpose of these lessons is to go beyond the theory and show you how to actually write and deploy Enterprise Java Beans along with the required client-side programs.
EJBs live in servers
To be of any use, a bean must be deployed in a server that is compliant with Sun's Java 2 Platform Enterprise Edition. A number of such servers are available from a variety of vendors. These servers differ in many respects, particularly with regard to their setup and administration.
From the general to the specific
Most of the books that I have read discuss servers in a very generic way, assuming that if they teach you the general, you can then teach yourself the specific.
I can't quarrel with that approach because I often use it myself in other areas of Java instruction. However, in this case, I haven't found the general-to-specific approach to be very satisfactory.
Deployment information can be confusing
With the general-to-specific approach, a person can understand everything in a book about simple enterprise beans and still have a very difficult time getting even the simplest bean deployed and operational. This is because it can be very difficult to correlate the information in the books with the deployment information provided by the server vendor.
Will take the opposite approach
In these lessons, I plan to take the opposite approach. I plan to concentrate on the use of the Java 2 Platform Reference Implementation from Sun as a J2EE-compliant server.
I plan to provide all of the information that you need, not only to write your beans, but also to deploy them successfully in the J2EE server, and to access them from compatible client programs. My thinking is that early success in getting simple beans deployed and operational will make you much more likely to hang in there and learn the more advanced material about beans that will be presented later.
From the specific to the general
My hope is that after I teach you the specific (with regard to an EJB server) you can then obtain information about other servers from other vendors and teach yourself how to use those servers for deployment of your beans.
I showed you the specific steps required to get a bean up and running in Sun's J2EE environment. I provided several explanations as to "What is an EJB?" I discussed the purpose and goal of EJB, along with a very brief description as to how that goal is achieved.
I explained that rather than to discuss EJB servers in a generic sense, I plan to teach you specifically how to deploy beans in the Java 2 Platform Reference Implementation. I will do this in the believe that early success with a specific implementation will make it easier for you to expand your knowledge into the more general world of EJB servers from different vendors.
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited. Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.