link to table of contentsgo to previous topicgo to next topiclink to glossaryindex of terms


Adding the EJB to the Servlet

AppComposer uses JavaBeans that construct a visual interface to access EJBs for you, so if you are used to writing code to use EJBs, you will notice that this feels a little different.› EJB components that you add to a capsule have the Home and Remote interface methods accessible from the single EJB icon in the outline view.› When using a find method on an entity bean that returns more than one remote interface, AppComposer provides methods to iterate over the references returned.

Now you are ready to add the SimpleBank EJB.› By default, properties of the bean use JBoss as the EJB server. DigiSliceEJBHome is the name used in JNDI to look up the bean. If you use an EJB container other than the built-in JBoss, and your EJB application server uses a value other than what is in ejb-name in ejb-jar.xml, you have to change the value.

To add SimpleBank EJB to the capsule:

  1. Select BalancePage.
  2. Add an HTMLText actor.
    Html -> HTMLText
  3. Rename the HTMLText actor to BalanceText.
  4. Insert the SimpleBank EJB as a child of BalanceText. From the Palette, the path is:
    EJBGen -> SimpleBank -> SimpleBank
    The EJBGen menu contains all EJBs that have been imported into AppComposer. If SimpleBank is not there, you need to import it.
  5. Rename the SimpleBank bean in the outline SimpleBankBean.

At this point, the bean is accessible. That is, the servlet capsule can find and utilize the deployed bean.› You now need an action group behavior to handle the access.› For this tutorial, you will provide handling for two possible scenarios:

For this example, assume that the only possible error the user can make is to enter an invalid account number. Any error the servlet encounters will generate the error message that the account was invalid.

To decide whether to display the error message or the account balance, the SimpleBankBean looks in the database for an entry whose account number matches the parameter from the form.› If no matching entry is found, it throws an exception.› Since the account number is the primary key of the database, only one account exists for any valid account number.

You need to create behaviors that look up the account, warn of any errors, and display the balance amount from a successful search. The stimulus is when the actor BalanceText is about to be output.

To create a behavior to find an account:

  1. Select BalanceText.
  2. Add an action from the Palette.
    Behaviors -> Action
  3. Rename the action FindAccount.
  4. Define the action using the following settings in the property pane:

If FindAccount throws an exception, this example assumes that a matching account was not found. To handle this case, add a behavior that activates when FindAccount throws an exception. For simplicity, assume that all exceptions are caused by an invalid account number. In a real application, you would probably want to distinguish between an invalid account number and other kinds of errors.

To handle an exception:

  1. Select FindAccount.
  2. Insert -> Behavior -> Action
  3. Rename the behavior InvalidAccount.
  4. Define InvalidAccount using the following settings:

Now that you have provided handling for potential errors, you can proceed with handling valid entries.

When the EJB SimpleBankBean finds a database entry for the account number that the user supplied, it generates an EJB.selected event. When this happens, the only thing left to do is to ask SimpleBankBean for the balance of the account and output it.›

To access the data, you can define a call to a remote reference of a SimpleBankBean that currently represents that data we are interested in. To do this, create a behavior that activates on EJB.selected from the SimpleBank EJB, retrieves the account balance from the EJB, converts it to a string and assigns it to the BalanceText actor.

To get the balance from SimpleBankBean:

  1. Select BalanceText.
  2. Insert -> Action Behavior
  3. Rename the behavior SetBalance.
  4. Define SetBalance using the following settings:

Note that the account balance is returned as a numeric (Double) value, but Java automatically converts it to a string when we concatenate it onto the end of another string.

You are now done building the capsule!› Save your work.

Your outline should look like the following:

Run it

  1. Make sure the debug web server is running.
  2. Make sure JBoss is running.
  3. From the File menu, choose Run.
    AppComposer launches the servlet in the debug web server.

If the configuration has been set up correctly and the capsule has no errors, you should see the following page:

Some valid account numbers are : 111111, 123456, 000001, and 987654.› Enter one of these and click Get Balance.› You should see something similar to the following page:

If you enter an invalid account number, you should see an error message.› If anything is wrong with how you set up the example you will see the error message. If you have entered a valid account number and are getting the invalid account message, check that:

You have now completed this tutorial! To see a more complex AppComposer project that uses EJBs, see the icDVD example from the AppComposer/examples directory.

link to table of contentsgo to previous topicgo to next topiclink to glossaryindex of terms
      © 2003 DigiSlice Corporation