corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Web Service Passing Binary Data, pt 2: Creating the EJB Module

Download the sample

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.

Lessons In This Tutorial

Content on this page applies to NetBeans IDE 6.5
  1. Overview
  2. => Creating the EJB Module
  3. Creating the Web Service
  4. Testing the Web Service
  5. Modifying the Schema and WSDL Files to Pass Binary Data
  6. Creating the Swing Client

Creating the 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:

  1. 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.
  2. 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.
  3. 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.
    Projects view with new EJB project
  4. 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.
  5. 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.
    New Session Bean wizard with name, location, session type and interface

    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.


    Projects view showing new Flower bean
  6. Right-click in the editor and choose Insert Code > Add Business Method.
  7. 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.
    Business Method dialog showing name, return type and Name parameter

  8. Click the Exceptions tab. Click Add. The Find Type dialog opens. Type IO and select the IOException (java.io).
    Find Type dialog with IOException chosen

    Click OK. You return to the Business Method dialog, which shows the IOException.
    Business Method dialog showing IOException chosen

    Click OK. You now have the basis of a method in your bean class, and a method declaration in the remote interface class.

  9. 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.

  10. 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.

  11. 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.

    For FlowerBean.java, the import statements are:

    import java.io.ByteArrayOutputStream;
    import java.util.List;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.ArrayList;
                    import javax.ejb.Stateless;

    For FlowerRemote.java, the import statements are:

    import java.io.IOException;
    import java.util.List;
                    import javax.ejb.Remote;
  12. 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.
    Project view with two Enterprise Bean nodes

  13. Fill out the bean class with the following code, for exposing a single image and for exposing all images.
    @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();
        }
    
                    }
  14. Create a new subpackage called resources. Copy the following images into that package:

    In the Projects view, the images should appear as shown here:


    Project view showing downloaded images

    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.

Next step:

Creating the Web Service


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.