160
Chapter 16. Developing Web Components
16.4. Accessing an EJB from a Servlet or JSP Page
Starting with JOnAS 2.6 with its
web
container service, it is possible to access an enterprise Java bean
and its environment in a J2EE-compliant way.
The following sections describe:
1. How to access the Remote Home interface of a bean.
2. How to access the Local Home interface of a bean.
3. How to access the environment of a bean.
4. How to start transactions in servlets.
Note
All the following code examples are taken from the The EarSample example provided in the JOnAS
distribution.
16.4.1. Accessing the Remote Home Interface of a Bean:
In this example the servlet gets the Remote Home interface
OpHome
registered in JNDI using an EJB
reference, then creates a new instance of the Session Bean:
import javax.naming.Context;
import javax.naming.InitialContext;
//remote interface
import org.objectweb.earsample.beans.secusb.Op;
import org.objectweb.earsample.beans.secusb.OpHome;
Context initialContext = null;
try {
initialContext = new InitialContext();
} catch (Exception e) {
out.print("
¤
li
¥
Cannot get initial context for JNDI: ");
out.println(e + "
¤
/li
¥
");
return;
}
// Connecting to OpHome through JNDI
OpHome opHome = null;
try {
opHome = (OpHome)
PortableRemoteObject.narrow(initialContext.lookup \
("java:comp/env/ejb/Op"), OpHome.class);
} catch (Exception e) {
out.println("
¤
li
¥
Cannot lookup java:comp/env/ejb/Op: "
+ e + "
¤
/li
¥
");
return;
}
// OpBean creation
Op op = null;
try {
op = opHome.create("User1");
} catch (Exception e) {
out.println("
¤
li
¥
Cannot create OpBean: " + e + "
¤
/li
¥
");
return;
}
Summary of Contents for Application Server
Page 1: ...Red Hat Application Server JOnAS User Guide ...
Page 8: ......
Page 22: ...14 Chapter 1 Java Open Application Server JOnAS a J2EE Platform ...
Page 58: ...50 Chapter 3 JOnAS Configuration ...
Page 66: ...58 Chapter 5 JOnAS Class Loader Hierarchy ...
Page 78: ...70 Chapter 6 JOnAS Command Reference ...
Page 80: ......
Page 86: ...78 Chapter 7 Developing Session Beans ...
Page 136: ...128 Chapter 9 Developing Message Driven Beans ...
Page 142: ...134 Chapter 10 Defining the Deployment Descriptor ...
Page 148: ...140 Chapter 11 Transactional Behavior of EJB Applications ...
Page 158: ...150 Chapter 14 EJB Packaging ...
Page 162: ...154 Chapter 15 Application Deployment and Installation Guide ...
Page 164: ......
Page 176: ...168 Chapter 18 WAR Packaging ...
Page 178: ......
Page 184: ...176 Chapter 20 Defining the Client Deployment Descriptor ...
Page 186: ...178 Chapter 21 Client Packaging ...
Page 188: ......
Page 192: ...184 Chapter 23 EAR Packaging ...
Page 194: ......
Page 200: ...192 Chapter 24 JOnAS Services ...
Page 204: ...196 Chapter 25 JOnAS and the Connector Architecture ...
Page 222: ...214 Chapter 27 Ant EJB Tasks Using EJB JAR ...
Page 234: ...226 Chapter 29 Web Services with JOnAS ...
Page 236: ......
Page 260: ...252 Chapter 34 How to use Axis in JOnAS ...
Page 270: ...262 Chapter 36 Web Service Interoperability between JOnAS and BEA WebLogic ...
Page 296: ......