The goal of this tutorial is to create an EJB module that exposes two methods, the first for providing a single image, the second for providing all the images. Normally, the images would come from a database. Since database retrieval is not the focus of this tutorial, we simply place the images in a resource folder within our EJB module.
In the following procedure, you create an Enterprise Java Bean (EJB) that contains the images that will be passed by the web service as binary data.
To create the EJB module:
Choose File > New Project (Ctrl-Shift-N). The
New Project wizard appears. Select
EJB Module from the Java EE category and click Next. The Name and Location page opens.
Type FlowerAlbum in the Project Name field. Select the location you want for this project—use the same location for the other projects you create for this end-to-end application. Keep the default settings for the other options and click Next.
The Server and Settings page opens.
Select the application server and Java EE version. This must be the same for all projects in the end-to-end application. The IDE creates an EJB module project that looks like this in the Projects window.
Right-click the FlowerAlbum project node and choose New > Session Bean. Alternatively,
right-click the project node and choose New > Other. In the New File wizard,
choose Session Bean under Enterprise. The New Session Bean wizard appears.
Name the session bean Flower, together
with flower.album as the package name. Make sure to select Stateless and Remote. The wizard now appears as follows.
Click Finish.
The IDE adds a session bean to the Source Packages node, together with a remote interface,
as shown here. In the Enterprise Beans node, a new node is added
for your new FlowerBean.
Right-click in the editor and choose Insert Code > Add Business Method.
Type the following values in the Business Method dialog:
Name:getFlower
Return Type:byte[]
Click Add. In Name, type name. Leave the other
values unchanged. You now see the following.
Click the Exceptions tab. Click Add. The Find Type dialog opens. Type IO and select the IOException (java.io).
Click OK. You return to the Business Method dialog, which shows the IOException.
Click OK. You now have the basis of a method in your bean class,
and a method declaration in the remote interface class.
Call up the Business Method dialog again. This time, enter
the following values:
Name:allFlowers
Return Type:List<byte[]>
As before, add IOException to the Exceptions tab. Click through the dialogs and the IDE creates the method.
In the remote interface class, note that the methods have
successfully been generated by the previous steps.
Look in the bean class and note that stubs have been
created for the declared methods.
@Stateless
public class FlowerBean implements FlowerRemote {
public byte[] getFlower(String name) throws IOException {
return null;
}
public List<byte[]> allFlowers() throws IOException {
return null;
}
}
Alternatively, instead of using the Business Method dialog
you can manually add code to the bean class and to the
remote interface class. However, if you use the Business Method
dialog, the IDE adds code to both the bean class and the
remote interface class, simultaneously.
Fix imports in the bean and remote interface classes.
In each class, place the cursor anywhere in the code, right-click
to open the context menu, and select Fix Imports. A dialog box opens
showing all necessary imports. Press OK and NetBeans generates the
import statements. Alternatively, press Ctrl-Shift-I in each class to
open the import dialog box. Where you have an option of several List classes to import, select java.util.List.
In the Projects view, the images should appear as shown here:
In your code, notice
that the methods getFlower and allFlowers both
make use of the images in this package.
You EJB module is now complete! In the next section, you create
a web service that delegates to the EJB module, in order to
retrieve the images at the appropriate points in the code.
To send comments and suggestions, get support, and keep informed on the latest
developments on the NetBeans IDE Java EE development features, join
the mailing list.