How to Create a Website Using EJB
Enterprise JavaBeans (EJBs) provide the infrastructure for building server-side Java components. EJBs are remote objects used for creation, deletion and invocation. EJBs can be either entity beans or session beans.
Entity beans represent business objects such as customers or orders. The beans are persistent; they are saved in a storage mechanism that allows them to exist beyond the lifetime of the J2EE server process. Entity beans also allow multiple clients to share access and they can be related to other entity beans in container-managed relationships.
Session beans represent one client on the J2EE server. The bean executes complex tasks on the server for the client, but the data is not persistent like entity beans. These beans are often used to send order confirmation emails or fetch read-only data for the client.
Instructions
-
-
1
Install your JBoss and XDoclet runtimes in Eclipse. This will allow you to run Web modules and Enterprise JavaBeans from Eclipse, but you must tell Eclipse where JBoss and XDoclet are located.
Select "Window" and click the "Preferences" option on the Eclipse window. Select "Java" on the expandable menu to reveal the JREs that are currently installed.
-
2
Select the "Add" button. Browse for your JRE directory and select the folder where JDK is installed. Name the runtime and select "OK." Your new JRE will show up on the "Installed Java Runtime Environments" list.
-
-
3
Select "Window" and click the "Preferences" option. Select "Server" on the expandable menu. Click "Installed Runtimes" to add a new server runtime environment.
-
4
Click the "Add" button. Select the "Generic Server Runtime" option. Select the server type as "JBOSS." Click "Browse" to select your application server directory and Classpath variable. Both options should be set to the JBoss installation directory. Click "Finish."
-
5
Select "XDoclet" from the expandable menu. Enable the XDoclet Builder. Click the "Browse" button, and choose the XDoclet installation directory. Click the "Apply" button.
-
6
Click on "XDoclet" in the expandable menu and select "ejbdoclet." Make sure that all of the checkboxes are empty so that XDoclet can generate its own deployment descriptors. Click "OK" and close the box.
-
7
Create a simple Session Enterprise JavaBean and client web application. Follow this sequence to complete the project:
Mouse-over the "File" and "New" buttons and click on the "Other" button. Select "EJB" from the drop-down list and click the "J2EE EJB Module." Click "Next."
Click the "New" button. Name your project and pick your target server. Enter your module name. Click the "Advanced" button and deselect the "Add module to an EAR application" option. Select the "Create an EJB Client JAR module to hold the client interfaces and classes" option. Select the "Add support for annotated Java classes" option. Click the "Finish" button. You will be immediately prompted to switch to J2EE. Click "Yes."
-
8
Create your session bean by following this sequence:
Select the "ejbModule folder" and right-click the folder. Using the "New" menu, select "Other," then "EJB" and "Enterprise JavaBean." Click the button to choose the "SessionBean" and click "Next."
Set the "Project," "Module Name," "Java package," "Class name" and "Superclass" for the class file destination. The "Class name" must end in the word "Bean." Click "Next" and "Finish." This will cause your "NewBean" to appear in your "ejbModule folder." XDoclet will generate all other related classes.
Open your "NewBean" in the Java editor and edit only this file. XDoclet will change the supporting classes each time a change is made to your "NewBean" class. Save your work.
-
9
Publishing your Enterprise JavaBean allows you to test your EJB in a live environment. Publish your EJB to your server. Observe the console's output to see that the Enterprise JavaBean has been successfully deployed.
-
1
References
Resources
- Photo Credit Programming Code Source Background Texture Illustration Design image by Bob Davies from Fotolia.com famous programmer's phrase on the screen image by Sergey Peterman from Fotolia.com