[ Team LiB ] Previous Section Next Section

WebLogic Server EJB to JSP Integration Tool

The WebLogic Server EJB to JSP integration tool enables you to execute EJB method calls without implementing an EJB client code within your JSP page. The integration tool generates a JSP tag library associated with each individual EJB. To invoke EJB methods within your JSP page, you need only declare the tag library and implement the applicable JSP tag. For example, you would use our Global Auctions ItemEJB like so:

<% taglib uri="itemEJB.tld" prefix="itemEJB" %> (assumes uri registered in web.xml)
 //Invoking EJB Create
<itemEJB:home-create itemvalue="<%= iv %>" /> (assumes itemValue object--iv created and
graphics/ccc.gif populated)

Compare that to the EJB client implementation shown here:

ItemHome home;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");

// lookup ItemHome and store
try {
    InitialContext ic = new InitialContext(ht);
    home = (ItemHome)ic.lookup("ItemHome");
} catch (Exception e) {
ItemValue iv = new ItemValue();
iv.setDescription("Ming Chair");

Item item = (Item)home.create(iv);

Notice how much cleaner the implemented of the EJB method calls are. A tag is optionally generated for each EJB method. The tag's attributes are the associated EJB's method parameters. The integration tool supports Stateless and Stateful Session EJBs, and Entity EJBs. For more details about EJBs, see Part V, "Using Enterprise JavaBeans in WebLogic Applications."

Generating EJB to JSP Tag Library

To generate a JSP tag library for an EJB, start the WebLogic EJB to JSP integration tool by executing the weblogic.servlet.ejb2jsp.gui.Main application as follows:

$ java weblogic.servlet.ejb2jsp.gui.Main

When the previous command is executed, the EJB to JSP application window will appear. Set the source directory (the location of the Java files, not the class files corresponding to your EJB) by selecting Preferences from the File menu as shown in Figures 17.4 and 17.5.

Figure 17.4. Selecting preferences.


Figure 17.5. Setting source directory.


After setting the source directory (select OK to persist selection), select New from the File menu, and then select the target EJB JAR file, as shown in Figures 17.6 and 17.7.

Figure 17.6. Selecting new project.


Figure 17.7. Selecting EJB JAR file.


After selecting an EJB JAR file, the integration tool creates and displays the project in the left plane of the application window. Highlight Project Build Options under the project's name (the name of the JAR file) and set build options as shown in Figure 17.8.

Figure 17.8. Setting project build options.


After setting options (relative directory locations and packaging option—JAR file are recommended), build the tag library by selecting Build Project from the File menu, as shown in Figure 17.9.

Figure 17.9. Building the project.


The tag library will be produced and deposited within the directory selected under build options, as previously discussed.

Using EJB Tags Within a JSP Page

To use the generated EJB tags, register the tag library with your application's web.xml as shown here:


This ensures that the referenced JAR file is in the given location. You merely declare the tag library and reference the tags within the JSP page, as shown here:

<% taglib uri="ItemEJB-tags.tld" prefix="itemEJB" %> (assumes uri is registered within web
 <b>Create an Item EJB</b></br>//note: method create() returns an item object//
<itemEJB:home-creat itemvalue="<%= iv %>"_return="item"/>
Item Number: <%=item.getItemNumber()%>
Minimum Bid: <%=item.getMinBid()%>
Description: <%=item.getDescription()%>

Notice the _return="it" construct within the method invocation tag. This construct allows access to the values returned from the associated method; in our example, the create() method. This feature is available for EJB methods with non-void return types. If the returned value is a Java primitive data type, the primitive will be returned as the corresponding wrapper class; that is, a long will be returned as java.lang.Long, and an int will be returned as java.lang.Integer.

According to the EJB 2.0 Specification, an EJBHome interface may contain methods other than create() and find(). The tags generated for these other home interface methods are prepended with home- to avoid confusion.

Method tags generated for Stateful Session and Entity EJBs are required to be within a EJB home interface tag that corresponds to a find() or create() method. All EJB methods tags within enclosing (single-value) find() or create() method tags operate on the specific bean instance involved in the find() or create() method. Our previous example would be modified as shown here:

<% taglib uri="ItemEJB-tags.tld" prefix="itemEJB" %> (assumes uri is registered within web
 <b>Item Information</b></br>//note: method create returns an item object//
<item:home-findByPrimaryKey integer="1001" return="item">
 <itemEJB:setMinimumBid f="1000"/>
 <itemEJB:setDescription s="Teak Chair" />
 Item Number: <%=item.getItemNumber()%>
 Minimum Bid: <%=item.getMinBid()%>
 Description: <%=item.getDescription()%>

The _return construct will create a page variable referencing the found or created EJB instance. Home tag methods returning a collection of beans will iterate over their body for as many beans as are returned. The _return construct refers to the current bean in the iteration. The following code snippet

<item:home-findByPrimaryKey _return="item">
 Item Number: <%=item.getItemNumber()%>
 Minimum Bid: <%=item.getMinBid()%>
 Description: <%=item.getDescription()%>

prints a list of all auction items; that is, it iterates through a collection of EJBs.

Default attributes (preset values, such as initial or routine data, sent to your code) can be set using the integration tool as shown in Figure 17.10 (note the Expression field).

Figure 17.10. Setting default attributes.


If no values are set within the window, all attributes must be provided (passed from the JSP tag attribute).

    [ Team LiB ] Previous Section Next Section