EJB to JSP Integration Tool

In order to easily access Enterprise JavaBeans within a JavaServer Page, you can use the EJB to JSP integration tool provided with WebLogic Server. As you have learned over the last four days, accessing EJBs from an EJB client application is an elaborate task. You must first create a JNDI Context object and then look up the bean's home interface object in the naming service using the JNDI Context object in the client application. After obtaining the reference to the home interface object, the client application invokes the create() methods to obtain the bean's remote interface object. This remote interface object is then used to invoke the EJB's business methods.

If you need to access an EJB from within a JSP, you will need to add the code for the steps in the JSP. This results in an unwieldy and difficult to maintain JSP. To simplify matters, WebLogic Server provides the EJB to JSP integration tool. The EJB to JSP integration tool leverages the tag library features of JSPs. The tool generates tag libraries from the Java archive (.jar) files of EJBs. These tag libraries contain the entire code for accessing EJBs. Hence, no code is required to be embedded in the JSPs, thus making the JSPs a lot simpler and easier to maintain. Another advantage is that the EJB accessing code is centralized in one place the JSP tag libraries and hence can easily be reused in different JSPs.

To execute the EJB to JSP integration tool, you should execute the following command:

 java weblogic.servlet.ejb2jsp.gui.Main  

Using the Tool

You will now see how to use the EJB to JSP tool for generating JSP tag libraries. You can work with the EJBs of the restaurant application that you have built over the past few days. After starting the tool, select the New option under the File menu. Select the EJB Java archive file of the restaurant application that you had built, for which the JSP tag libraries need to be generated. Before generating the JSP tag libraries, you need to define the build options in the tool.

The screen of the EJB to JSP integration tool showing the different build options that can be set is shown in Figure 13.3. In the Build Options screen you can specify the Java package name under which the tag handler classes of the generated JSP tag libraries will be located. Two types of outputs can be generated by the tool: DIRECTORY or JAR. For a DIRECTORY output, the tool generates the tag library descriptor file and the tag handler classes in the directory specified in the DIRECTORY Classes field on the Build Options screen. Upon selecting a JAR output, the tool packages the tag library components in a single Java archive (.jar) file. During development, it is recommended to select DIRECTORY output. If you need to see the Java source code of the generated tag handler classes, you can select the Keep Generated Code check box.

Figure 13.3. Screen shot of EJB to JSP integration tool.

graphics/13fig03.jpg

In addition to these build options, the tool requires the Java source files of the EJBs. You can provide the path to the source files by clicking on the Edit SourcePath Elements button on the Build Options screen and specifying the directory path in the pop-up. You are now ready to generate the JSP tag libraries for the EJBs.

Now select the Build option from the File menu. Figure 13.4 shows the result of executing the build operation on the EJB Java archive file.

Figure 13.4. Generating the JSP tag libraries from the EJB Java archive file.

graphics/13fig04.jpg

Note

If you encounter a duplicate tag name error, it means that there is conflict in the tag names generated. This happens if you have overloaded methods in your EJBs. To get around this, you just need to change the tag names generated by the EJB to JSP tool and perform the build operation again.


Tip

By default, the tool searches for the source files in the same directory as the .jar file of your EJBs. Hence, you need to place the Java source files of your EJBs in the same directory as the .jar file of your EJBs. Alternatively, you can define the location of the Java source files in the tool from the Resolve Attributes option under the File menu.


Take a look at an example tag library descriptor file generated as output from the tool:

 <?xml version="1.0" encoding="ISO-8859-1" ?>  <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <shortname>ejb2jsp generated tag library</shortname>  <tag>   <name>getItemServingSize</name>   <tagclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags._ItemInterface_getItemServingSizeTag   </tagclass>   <teiclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags.           _ItemInterface_getItemServingSizeTagTEI </teiclass>   <info>   </info>  </tag>  <tag>   <name>getItemTemperature</name>   <tagclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags._ItemInterface_getItemTemperatureTag   </tagclass>   <teiclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags.           ItemInterface_getItemTemperatureTagTEI   </teiclass>   <info>   </info>  </tag>  <tag>   <name>setItemTaste</name>   <tagclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags._ItemInterface_setItemTasteTag   </tagclass>   <teiclass>   com.sams.learnweblogic7.ejb.cmp.jsp_tags._ItemInterface_setItemTasteTagTEI   </teiclass>   <info>    attribute 'taste' expects java type 'java.lang.String'   </info>   <attribute>    <name>taste</name>    <required>true</required>    <rtexprvalue>true</rtexprvalue>   </attribute>  </tag> ... </taglib> 

Also, the Java code of a tag handler generated by the tool is as follows:

 /*  * This code was automatically generated at 10:22:31 PM on Jul 21, 2002 * by weblogic.servlet.ejb2jsp.EJBMethodGenerator   do not edit. */ package com.sams.learnweblogic7.ejb.cmp.jsp_tags; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.rmi.RemoteException; import javax.ejb.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; // User imports import com.sams.learnweblogic7.ejb.cmp.ItemInterface; import com.sams.learnweblogic7.ejb.cmp.ItemHome; public class _ItemInterface_getItemServingSizeTag extends TagSupport {     private static final String HOME_JNDI_NAME = "Chef_Home" ;     String _return = null;     public void set_return(String r) { _return = r; }     public int doStartTag() throws JspException {         try {             verifyAllAttributesSet();             com.sams.learnweblogic7.ejb.cmp.ItemInterface ejb = getEJB();             java.lang.String _ret =  ejb.getItemServingSize() ;             if (_return != null) { pageContext.setAttribute(_return, _ret); }         } catch (RuntimeException e) {             throw e;         } catch (Exception e) {             throw new JspException("error invoking bean: " + e);         }         return EVAL_PAGE;     }     private void verifyAllAttributesSet() throws Exception {     }     public void release() {         _return = null;         super.release();     }     /* setter methods for all attributes */     /* the getEJB() method */     private com.sams.learnweblogic7.ejb.cmp.ItemInterface getEJB()             throws Exception {         com.sams.learnweblogic7.ejb.cmp.ItemInterface ret = null;         Tag p = null;         while ((p = getParent()) != null) {             if (p instanceof                    com.sams.learnweblogic7.ejb.cmp.jsp_tags.__Base                    _ItemHome_homeTag)             {ret =                    ((com.sams.learnweblogic7.ejb.cmp.jsp_tags.                    __Base_ItemHome_homeTag)p).                    getEJBFromHome();             }             return ret;         }         throw new JspException(                 "this tag must be enclosed in a _home tag for this EJB type");     } } 

Finally, to use this tag library in any JSP, take a look at this code snippet:

 <% taglib uri="/WEB-INF/item-ejb.tld" prefix="itemBean" %>  <itemBean:Itemhome-create         item         temperature="Cold"         taste="Sweet"         ingredients="Milk, Sugar, All Purpose Flour, Eggs, Cream, Bananas"         servingSize="One Scoop" /> 


Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net