...
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | ejb-jar.xml |
---|
|
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<description>Stateless Session Bean Example</description>
<display-name>Stateless Session Bean Example</display-name>
<enterprise-beans>
<session>
<ejb-name>RetrieveOrderInfoBean</ejb-name>
<business-local>
examples.session.stateless_dd.RetrieveOrderInfo
</business-local>
<ejb-class>
examples.session.stateless_dd.RetrieveOrderInfoBean
</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>
jdbc/DB2DataSource
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>
Shareable
</res-sharing-scope>
</resource-ref>
</session>
</enterprise-beans>
<interceptors>
<interceptor>
<interceptor-class>
examples.session.stateless_dd.RetrieveOrderCallbacks
</interceptor-class>
<post-construct>
<lifecycle-callback-method>
construct
</lifecycle-callback-method>
</post-construct>
<pre-destroy>
<lifecycle-callback-method>
destroy
</lifecycle-callback-method>
</pre-destroy>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>RetrieveOrderInfoBean</ejb-name>
<interceptor-class>
examples.session.stateless_dd.RetrieveOrderCallbacks
</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
|
The deployment descriptor of the OrderWEB.war is as followsIn the RetrieveOrderInfoBean, the following code is used to look up the Datasource object and obtain a database connection.
Code Block |
---|
| XMLJAVAXML |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | web.xml | examples.session.stateless_dd.RetrieveOrderInfoBean |
---|
|
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/DB2DataSource");
System.out.println("Got DataSource\n");
con = ds.getConnection();
System.out.println("Got Connection\n");
|
The deployment descriptor of the OrderWEB.war is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | web.xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>OrderWEB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>RetrieveOrder</display-name>
<servlet-name>RetrieveOrder</servlet-name>
<servlet-class>
examples.web.servlet.RetrieveOrder
</servlet-class>
</servlet>
<ejb-local-ref>
<ejb-ref-name>ejb/RetrieveOrderInfo
</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>Session<type>
<local>
examples.session.stateless_dd.RetrieveOrderInfo
</local>
<ejb-link>RetrieveOrderInfoBean</ejb-link>
</ejb-reflocal-type>ref>
<local><servlet-mapping>
<servlet-name>RetrieveOrder</servlet-name>
<url-pattern>/RetrieveOrder</url-pattern>
examples.session.stateless_dd.RetrieveOrderInfo
</local>
<ejb-link>RetrieveOrderInfoBean</ejb-link>
</ejb-local-ref>
<servlet-mapping>
<servlet-name>RetrieveOrder</servlet-name>
<url-pattern>/RetrieveOrder</url-pattern>
</servlet-mapping>
</web-app></servlet-mapping>
</web-app>
|
In the RetrieveOrder servlet, the following code is used to look up the ejb to retrieve the order details.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | examples.web.servlet.RetrieveOrder |
---|
|
Context ctx = new InitialContext();
System.out.println("Instantiating beans...");
retrieveOInfo = (RetrieveOrderInfo)ctx.lookup("java:comp/env/ejb/RetrieveOrderInfo");
String orderIdStr = request.getParameter("orderid");
int orderId = Integer.parseInt(orderIdStr);
OrderInfo oInfo = retrieveOInfo.getOrderInfo(orderId);
|
The deployment descriptor of the Order.ear is as follows.
...