| Prev | Next | J2EETM Developer's Guide
Advanced Topics |
ConverterJSPApp. This application contains the following elements:
ConverterEJB.java file is in the doc/guides/ejb/examples/converter directory. The other files are in the doc/guides/ejb/examples/jsptag directory.
ConverterApp you created in the Getting Started chapter.ConverterApp, save the ConverterBean component in a file named ConverterEJB.jar.ConverterJSPApp.ConverterEJB.jar file to the ConverterJSPApp application.Converter.jsp file. These tags are marked by bold font in the full listing of Converter.jsp included at the end of this section. (For more information on writing JSP files, see the JavaServer Pages web site .)
The taglib directive is the first JSP tag in the Converter.jsp file. This directive identifies the tag library descriptor (taglib.tld) and defines the tag prefix (j2ee) that associates subsequent tags with the tag library:
The tag library described in the<%@ taglib uri="taglib.tld" prefix="j2ee" %>
taglib.tld file has just one tag, which is named ejb. The ejb tag has three attributes: jndiName, homeInterface, and homeVar. The ejb tag file assigns values to each attribute:
The<j2ee:ejb jndiName="java:comp/env/ejb/MyConverter" homeInterface="ConverterHome" homeVar="converterHome"> <% converter = converterHome.create(); %> </j2ee:ejb>
Converter.jsp file uses script elements in conjunction with the tag library. The first of these elements declares the Converter session bean:
The next script element creates a new session bean:<% Converter converter = null; %>
The last two script elements invoke methods on the session bean, returning values which are displayed:<% converter = converterHome.create(); %>
The full listing for the Converter.jsp file follows:<%= converter.dollarToYen(100.00) %> <%= converter.yenToEuro(100.00) %>
<html>
<%@ taglib uri="taglib.tld" prefix="j2ee" %>
<head>
<title>Converter JSP</title>
</head>
<h1><b><center>Converter JSP Example</center></b></h1>
<hr>
<% Converter converter = null; %>
<j2ee:ejb
jndiName="java:comp/env/ejb/MyConverter"
homeInterface="ConverterHome"
homeVar="converterHome">
<% converter = converterHome.create(); %>
</j2ee:ejb>
<p>
dollarToYen: <%= converter.dollarToYen(100.00) %>
<p>
yenToEuro: <%= converter.yenToEuro(100.00) %>
</html>
taglib directives. The taglib directive in the Converter.jsp page refers to the taglib.tld tag library descriptor:
A listing of the<%@ taglib uri="taglib.tld" prefix="j2ee" %>
taglib.tld file follows. The tag element defines the ejb action, including its three attributes (jndiName, homeInterface, and homeVar). The tagclass element defines the tag handler class (EjbTag). The teiclass element specifies the TagExtraInfo class (EjbExtraInfo).
<?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">
<!-- a tab library descriptor -->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<urn></urn>
<info>
Tag library for EJB support
</info>
<!-- ejb tag -->
<tag>
<name>ejb</name>
<tagclass>EjbTag</tagclass>
<teiclass>EjbExtraInfo</teiclass>
<bodycontent>JSP</bodycontent>
<info>
Look up home interface and declare enterprise bean.
</info>
<attribute>
<name>jndiName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>String</type>
</attribute>
<attribute>
<name>homeInterface</name>
<required>true</required>
</attribute>
<attribute>
<name>homeVar</name>
<required>true</required>
</attribute>
</tag>
</taglib>
EjbTag class, for example, is the tag handler for the ejb action. The doInitBody method of the EjbTag class sets the homeVar attribute to the reference returned by a JNDI lookup method:
public void doInitBody() throws JspException {
try {
System.out.println("doInitBody()");
InitialContext ic = new InitialContext();
Object homeRef = ic.lookup(jndiName);
homeRef = PortableRemoteObject.narrow(homeRef,
Class.forName(homeInterface));
pageContext.setAttribute(homeVar, homeRef);
} catch (NamingException ex) {
throw new JspTagException("Unable to lookup home: "+jndiName);
} catch (ClassNotFoundException ex) {
throw new JspTagException("Class "+homeInterface+" not found");
}
}
TagExtraInfo class. A subclass of TagExtraInfo, the EjbExtraInfo class implements the getVariableInfo method. This method provides information about the homeVar and homeInterface attributes. The EjbExtraInfo class follows:
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class EjbExtraInfo extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo(
(String)data.getAttribute("homeVar"),
(String)data.getAttribute("homeInterface"),
true,
VariableInfo.NESTED),
};
}
To compile the EjbTag and TagExtraInfo classes, change to the examples/jsptag directory and execute these commands:
Windows:CPATH=.:$J2EE_HOME/lib/j2ee.jar:ejb.jar javac -classpath "$CPATH" *.java
set CPATH=.;%J2EE_HOME%\lib\j2ee.jar;ejb.jar javac -classpath %CPATH% *.java
WAR File General Properties Dialog Box:
a. In the combo box labelled "Web Component Will Go In," select ConverterJSPApp.
b. In the WAR Display Name field, enter ConverterWAR.
c. Click Add.
d. In the Add Content Files dialog box, choose the examples/jsptag directory. You may either type the directory name in the Root Directory field or locate it by clicking Browse.
c. Select theConverter.jspandtaglib.tldfiles from the text area and click Add.
d. Click Next.
e. Choose the examples/jsptag directory again.
f. Select theEjbExtraInfo.classandEjbTag.classfiles from the text area and click Add.
g. Click Finish.
h. Click Next.Choose Component Type Dialog Box:
a. Select JSP.
b. Click Next.Component General Properties Dialog Box:
a. In the JSP Filename combo box, select Converter.jsp.
b. In the Web Component Display Name field, enter TheConverter.
c. Click Next.Enterprise Bean References Dialog Box:
a. Click Add.
b. In the Coded Name column enter ejb/MyConverter.
c. In the Type column select Session.
d. In the Home column enter ConverterHome.
e. In the Remote column enter Converter.
f. Click Finish.
2. In the Web Context tabbed pane, enter ConverterContextRoot in the ContextRoot column.
ConverterJSPApp, specify MyConverter as the JNDI name for both the ejb/Converter reference and the ConverterBean component.
2. In the first dialog box, do not select the checkbox labelled "Return Client Jar."
3. In the second dialog box, verify the JNDI names.
4. In the third dialog box, verify the context root.
Converter.jsp from your browser, specify the URL as follows, but replace <host> with the name of the machine that is running the J2EE server:
http://<host>:8000/ConverterContextRoot/Converter.jsp
AccountJSPApp. Unlike the ConverterJSPApp, the AccountJSPApp accesses an enterprise bean through a JavaBeansTM component. This component mirrors the enterprise bean-- its state matches the enterprise bean's state. The ConverterJSPApp does not have to synchronize the state of an intermediary object with that of the enterprise bean. However, the JSP page of the ConverterJSPApp cannot maintain a state throughout a session. Every time a web client invokes the Converter.jsp file, all of the page's tags are executed. Because of this stateless nature, the approach taken by the ConverterJSPApp is not appropriate for an application that carries on a "conversation" with the end-user.