Previous Next vertical dots separating previous/next from contents/index/pdf

Step 6: Write Test Source Code and Test the EJBs

You're almost ready to test. In this step you'll write page flow source code that invokes your session EJB — which, in turn, invokes your entity EJB. Specifically, rather than writing typical EJB invocation code, you'll use an EJB control that handles that for you.

In this section, you will:

To Add an EJB Control to the Test Project

In these steps you create an EJB control that will represent your entity bean to your test code.

  1. In the Package Explorer, expand VisitWebTest, right-click src, then click New > Package.
  2. In the New Java Package dialog, in the Name box, enter controls, then click Finish.
  3. Switch to the Page Flow perspective by selecting Window > Open Perspective > Page Flow.
  4. In the Page Flow Explorer, right-click the Referenced Controls node and select Add Control.
  5. In the Select Control dialog, under New System Control, select EJB Control. Click OK.
  6. In the New Control dialog, in the parent folder box, expand the tree to VisitWebTest > src > controls, then select the controls node.
  7. In the Control name box, enter VisitTrackerBeanCtrl, then click Next.

  8. Under EJB Control, click Browse Application EJBs.

    In the Browse Resources dialog, note that the two EJBs you've created are listed.
  9. Select VisitTrackerBean (remote jndi), then click Finish.

  10. Confirm that the settings displayed are as follows, then click Finish.

You'll get EJB control source code such as the following in code

The code's simplicity masks its capability. For example, by extending the SessionEJBControl interface, the control supports implicit retrieval of the target EJB's home and business interface instances as well as a check to discover whether a target instance is available for method invocation. In other words, this is plumbing common to using an EJB for which you don't need to write code — the control takes care of it.

The @ControlExtension annotation designates to the compiler that this interface is a control. The @EJBHome annotation specifies the JNDI name of the home interface for the target EJB that this control provides access to.

To Add EJB Control Reference and Method Invocations to the Page Flow

In the following steps you'll add page flow code to use the control you created. In page flows, a controller is a central place for client logic — a place to contain state variables and data that would otherwise need to be contained in separate files (such as in JSPs).

In the case of VisitWebTest, your controller will be responsible not only for invoking your session bean via the EJB control you created, but also for passing the bean's responses along to the JSP files that make up the presentation components in your application.

  1. On the Page Flow Explorer tab, locate and double-click it.
  2. In the source code, note that a variable representing the control has been added.

    EJB control variable in page flow controller

  3. In the Page Flow Editor tab, locate the begin icon, right-click it and select Convert to a Method.

  4. In the source code for, add the following import statements to support code you'll be adding.
  5. import java.rmi.RemoteException;
    import javax.ejb.CreateException;
  6. Edit the begin method so it appears as follows. This code looks up and stores the home interface for the entity bean VisitTrackerBean and forwards processing to index.jsp.
  7. @Jpf.Action(forwards = 
            @Jpf.Forward(name = "success", path = "index.jsp")
    protected Forward begin()
        } catch(CreateException ce) {}
        catch(RemoteException re) {}
        return new Forward("success");
  8. Beneath the begin method code, add the following code. This code provides an action that can be called from index.jsp; that action will invoke the session bean. If the action is successful, the result will be forwarded to showForm.jsp, which you'll create in a moment.
    @Jpf.Action(forwards = 
            @Jpf.Forward(name = "success", path = "showForm.jsp")
    protected Forward invokeEJB(InvokeEJBForm form)
        String response;
            response = visitTrackerBeanCtrl.greetVisitor(form.getName());
        } catch(RemoteException re) 
            response = "An error has occurred";
        getRequest().setAttribute("returnvalue", response);
        return new Forward("success");
     * Get and set methods may be overwritten by the Form Bean editor.
    public static class InvokeEJBForm implements
        private static final long serialVersionUID = 1L;
        private String name;
        public void setName(String name)
   = name;
        public String getName()
  9. Press Ctrl+Shift+S to save your work.

To Create a User Interface and Test the EJB

Here you'll update files in your application's user interface so that the page flow controller can use them to present the results of its interaction with your EJBs.

  1. In the Page Flow Explorer tab, open the node Pages > index.jsp. Double-click index.jsp to open the file.
  2. Modify the <netui:html> tag so that it appears as follows. This code calls the invokeEJB action you coded in the preceding steps.
  3. <netui:html>
                EJB Tester
            <h2> EJB Tester </h2>
            <netui:form action="invokeEJB">
                    <tr valign="top">
                        <td>Your Name:</td>
                            <netui:textBox dataSource=""/>
                <netui:button value="invoke EJB" type="submit"/>
  4. On the Page Flow Explorer tab, right-click the Pages > showForm.jsp node and select Create.
  5. Double-click the Pages > showForm.jsp node to open its source code.
  6. Replace its source code with the following. This code receives results from a successful invocation of the session bean and displays a response.
  7. <%@ page language="java" contentType="text/html;charset=UTF-8"%>
    <%@taglib uri="" prefix="netui"%>
    <%@taglib uri="" prefix="netui-data"%>
    <%@taglib uri="" prefix="netui-template"%>
                EJB Response
            <h2>EJB Response</h2>
            Here is the result returned from EJB: <netui:label value="${requestScope.returnvalue}"></netui:label>
            <netui:anchor href="index.jsp">Let's do this again</netui:anchor>
  8. Press Ctrl+Shift+S to save your work.
  9. On the Page Flow Explorer tab, right-click, then click Run As > Run on Server.
  10. If the Select Tasks dialog is displayed, click Finish.
  11. After Workshop for WebLogic has finished building your application's components, it will display the index.jsp.
  12. Enter a name in the box provided, then click invoke EJB.

  13. Note that the response page includes the name you entered echoed back to you. Remember that the logic that generates this message is contained in the VisitTrackerBean session bean that you created in Step 3.

  14. To keep testing, click Let's do this again, enter the same name as before, then click invoke EJB again. With each test you'll see an updated response.

That's it! You've built and tested two Enterprise JavaBeans with Workshop for WebLogic. The next step in this tutorial is a summary of what the tutorial covered, along with links to information you might find helpful.

Related Topics


Click one of the following arrows to navigate through the tutorial:


Skip navigation bar   Back to Top