Skip to content

Commit ea17e72

Browse files
committed
adding ear
1 parent 1f7a15d commit ea17e72

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

jee_demo/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,10 @@ When use Enterprise Java Bean?
9393
- EJB is an essential part of a J2EE platform.
9494
- J2EE platform have component based architecture to provide multi-tiered, distributed and highly transactional features to enterprise level applications.
9595
- EJB are primarily of three types: Session Bean,Message-Driven Bean,Entity Bean.
96+
- Servlet-EJB communications use JNDI for lookup and RMI for the EJB calls, over either ORMI (the Oracle implementation of RMI)
97+
or IIOP (the standard and interoperable Internet Inter-Orb Protocol).
98+
99+
The servlet-EJB can have three scenarios:
100+
- Local lookup: The servlet calls an EJB that is co-located, meaning it is in the same application and on the same host, running in the same JVM. The servlet and EJB would have been deployed in the same EAR file, or in EAR files with a parent/child relationship. The example uses EJB local interfaces, which were introduced in the EJB 2.0 specification. See "EJB Local Lookup".
101+
- Remote lookup: within the same application: The servlet calls an EJB that is in the same application but on a different host. (The same application is deployed to both hosts.) This requires EJB remote interfaces. This would be the case for a multitier application where the servlet and EJB are in the same application but on different tiers. See "EJB Remote Lookup within the Same Application".
102+
- Remote lookup: outside the application: The servlet calls an EJB that is not in the same application. This is a remote lookup and requires EJB remote interfaces. The EJB might be on the same host or on a different host, but is not running in the same JVM. See "EJB Remote Lookup Outside the Application".
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,68 @@
11
package servletmodule;
22

3+
import ejbmodules.beans.CalculatorBean;
4+
5+
import javax.naming.InitialContext;
6+
import javax.naming.NamingException;
37
import javax.servlet.GenericServlet;
48
import javax.servlet.ServletException;
59
import javax.servlet.ServletRequest;
610
import javax.servlet.ServletResponse;
711
import javax.servlet.annotation.WebServlet;
12+
import javax.sql.DataSource;
813
import java.io.IOException;
14+
import java.sql.Connection;
15+
import java.sql.SQLException;
916

1017
/**
1118
* @author Hikmat Dhamee
1219
*
1320
*/
1421
@WebServlet(value="/student", name="hello-student")
1522
public class StudentServlet extends GenericServlet {
23+
DataSource ds = null;
24+
Connection conn = null;
25+
26+
public void init() throws ServletException {
27+
try {
28+
InitialContext ic = new InitialContext(); // JNDI initial context
29+
ds = (DataSource) ic.lookup("jdbc/UserDB"); // JNDI lookup
30+
conn = ds.getConnection(); // database connection through data source
31+
}
32+
catch (SQLException se) {
33+
throw new ServletException(se);
34+
}
35+
catch (NamingException ne) {
36+
throw new ServletException(ne);
37+
}
38+
}
1639

1740
@Override
1841
public void service(ServletRequest req, ServletResponse res)
1942
throws ServletException, IOException {
2043
res.getWriter().println("Hello Sample!");
44+
45+
// simple JNDI lookup
46+
try {
47+
System.out.println("Connection Available: " + conn.getNetworkTimeout());
48+
} catch (SQLException e) {
49+
e.printStackTrace();
50+
}
51+
52+
// EJB call from servlet
53+
// Step 4: Use JNDI to look up the EJB local home interface.
54+
try {
55+
InitialContext ic = new InitialContext();
56+
CalculatorBean hlh = (CalculatorBean)ic.lookup("java:comp/env/ejb/CalculatorBean");
57+
res.getWriter().println(hlh.subtract(2,5));
58+
59+
} catch (NamingException ne) {
60+
System.out.println("Could not locate the bean.");
61+
} catch (Exception e) {
62+
// Unexpected exception; send back to client for now.
63+
throw new ServletException(e);
64+
}
65+
2166
}
2267

2368
}

jee_demo/servletmodule/src/main/webapp/WEB-INF/web.xml

+7
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010
<res-type>javax.sql.DataSource</res-type>
1111
<res-auth>Container</res-auth>
1212
</resource-ref>
13+
<!--EJB ref-->
14+
<ejb-local-ref>
15+
<ejb-ref-name>ejb/CalculatorBean</ejb-ref-name>
16+
<ejb-ref-type>Session</ejb-ref-type>
17+
<local-home>ejbmodules.beans.CalculatorBean</local-home>
18+
<local>ejbmodules.beans.CalculatorBean</local>
19+
</ejb-local-ref>
1320

1421
</web-app>

0 commit comments

Comments
 (0)