An EJB Example with NetBeans 4

tamerunSoftware and s/w Development

Aug 15, 2012 (4 years and 10 months ago)


A Session EJB Example with NetBeans 4.1 and Sun AS

Select File

New Project… and then choose the Enterprise category and EJB Module

Click Next. Specify a project name and location:

Click Finish.

click the HelloWorld project and
select New

Session Bean. Specify an EJB
name, package, and choose to create a stateless session and remote interface:

Click Finish.

Expand the Enterprise Beans under the project and left click on HelloEJBSB and choose

Business Method.

In thi
s example from the Mastering Enterprise JavaBeans book, specify the name as hello
and a return type of String. If the method required parameters, you can add them at the
same time by clicking Add…:

Click OK.

To see the results of adding a business met
hod, under the project, expand Source
Packages and then the package, and double
click the bean class
You’ll notice the empty method at the bottom of the listing:

click the interface (this is impleme
nted by and extended by and you’ll see the method

In the bean class, add the code as outlined in the book to the hello()

Save everything, then build the project. Next, locat
e the HelloWorld.jar file in the
project’s dist folder and drag it onto the Sun Autodeploy folder (SAS should be running
at this point) to deploy the EJB. Launch Sun’s Deploytool and verify the EJB is


New Project:

Next. Specify p
roject name and location:

Click Finish. Right click on the web app project and select New

Servlet. Specify class
name and package:

Click Next. Keep default:

Click finish. In the servlet code, right click in the class portion of the servlet
and select
Enterprise Resources

Call EnterpriseBean.

Select HelloEJBSB and click OK. Note the added code to the servlet:

Add the following code to processRequest() in the servlet:

Save everything, then run the web app. When the browser window

appears, specify the
servlet, then reload:

To redeploy, make whatever changes to your EJB, rebuild, and drag the HelloWorld.jar
onto the Sun Autodeploy folder. The EJB is automatically re