Note: The current release is NetBeans IDE 6.5. If you are using NetBeans IDE 6.5, see the latest version of this tutorial.
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.
Tutorials In This Trail
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:
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 the FlowerBean node and choose Add > Business Method. The Business Method dialog opens.
Type the following values in the Business Method dialog:
Click Add. In Name, type name. Leave the other values unchanged. You now see the following.

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: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.
@Remote
public interface FlowerRemote {
byte[] getFlower(String name) throws IOException;
List<byte[]> allFlowers() throws IOException;
}
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.
You have now declared your methods in the remote interface and implemented stubs in the bean class.The Projects window now shows two new nodes in the Enterprise Beans node, for your new methods, as shown here.

@Stateless
public class FlowerBean implements FlowerRemote {
private static final String[] FLOWERS = {"aster", "honeysuckle", "rose", "sunflower"};
public byte[] getFlower(String name) throws IOException {
URL resource = this.getClass().getResource("/flower/album/resources/"+name+".jpg");
return getBytes(resource);
}
public List<byte[]> allFlowers() throws IOException {
List<byte[]> flowers = new ArrayList<byte[]>();
for (String flower:FLOWERS) {
URL resource = this.getClass().getResource("/flower/album/resources/"+flower+".jpg");
flowers.add(getBytes(resource));
}
return flowers;
}
private byte[] getBytes(URL resource) throws IOException {
InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int read; (read = in.read(buf)) != -1;) {
bos.write(buf, 0, read);
}
return bos.toByteArray();
}
}
Create a new subpackage called resources. Copy the following images into that package:
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.
You are viewing a mobilized version of this site...
View original page here