Middleware
Application Server
Date: 5/16/06
Author: Debu Panda
This example application demonstrates Oracle's support for the Servlet 2.5 annotations and EJB 3.0 specification. It provides an example of using the new EJB 3.0 components from a Web application.
OC4J supports the following annotations in the web container:
javax.ejb.EJB annotation as defined by EJB 3.0 specificationjavax.persistence.PersistenceContext and javax.persistence.PersistenceUnit as defined by EJB 3.0 Java Persistence API.javax.xml.ws.WebserviceRef as defined by Java XML Web services 2.0It also supports the corresponding XML elements in the web module deployment descriptor.
This demonstration uses the Employee entity to demonstrate a entity using EJB 3.0 JPA that is accessed by the web module using EmployeFacade session bean.
The bean class is a plain java class that is annotated with @Entity to mark it as an entity bean.
@Entity
@Table(name = "EMP")
public class Employee implements java.io.Serializable
{
private int empNo;
private String eName;
private double sal;
@Id
@Column(name="EMPNO")
public int getEmpNo()
{
return empNo;
}
..
}
The @Table annotation is used to specify the table name to be used by this Entity bean.
The @Id annotation is used to mark the empNo field as the primary key of the entity bean.
The @Column annotation is used to specify that the empNo field is mapped to the EMPNO column in the table.
The javax.persistence.EntityManager API is used for persisting, finding and updating entity instances.
In this example, the EmployeeFacadeBean uses PersistenceContext injection to grab an EntityManager instance and then persists entity instance as follows:
@Stateless
public class EmployeeFacadeBean implements EmployeeFacade {
@PersistenceContext
private EntityManager em;
....
public void addEmployee(int empNo, String eName, double sal) {
emp = new Employee();
emp.setEmpNo(empNo);
emp.setEname(eName);
emp.setSal(sal);
em.persist(emp);
}
}
In this example, the InsertServlet uses dependency injection using javax.ejb.EJB annotation to use EmployeeFacadeBean as follows:
public class InsertServlet extends HttpServlet {
@EJB private EmployeeFacade employeeFacade;
..
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int empId;
String name = "";
double sal = 0;
empId = (int) Integer.valueOf(request.getParameter("empId"));
name = request.getParameter("empName");
sal = Integer.valueOf(request.getParameter("Sal"));
try {
System.out.println("Id:" + request.getParameter("empId"));
//Invoke EJB Method on injected EJB instance
employeeFacade.addEmployee(empId,name,sal);
System.out.println("Persisted successfully ..");
this.getServletContext().getRequestDispatcher("/jsp/success.jsp")
.forward(request, response);
} catch (Exception e)
You must have version="2.5" in the web.xml for the web-app tag in order for OC4J to scan for annotations as follows:
<web-app version="2.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
For further information on EJB 3.0, see the following documents on OTN:
This demonstration requires that the following software components are installed and configured correctly:
The Javadoc for this application is located in the %HOWTO_HOME%/doc/javadoc/ directory.
The configuration files are located in the %HOWTO_HOME%/etc directory, including deployment descriptor files such as application.xml.
To run the sample application on a standalone instance of Oracle Application Server 10g 10.1.3, follow these steps:
Ensure the following environment variables are defined:
This example is based on the EMP table from the SCOTT schema in an Oracle database. If you do not have SCOTT schema installed in your Oracle database, or are using a database other than Oracle, then the table will be automatically created during deployment of your applications because automatic table creation is switched. You can also manually create the table using the table.sql script in the %HOWTO_HOME%/scripts directory.
This example requires a DataSource to be configured to connect to the database that contains the EMP table.
For OC4J, you must configure a datasource in the %ORACLE_HOME%/j2ee/home/config/data-sources.xml file and point it at the schema that owns the EMP table.
An example configuration:
<connection-pool name="ScottConnectionPool">
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource"
user="scott"
password="tiger"
url="jdbc:oracle:thin:@localhost:1521:ORCL" >
</connection-factory>
</connection-pool><managed-data-source name="OracleManagedDS"
connection-pool-name="ScottConnectionPool"
jndi-name="jdbc/OracleDS"
/>
Start OC4J stand alone using the following command after you make the above changes.
>%ORACLE_HOME%/bin/oc4j -start
If you are using an OracleAS managed install, start using the following command after you make the above changes.
> %ORACLE_HOME%/opmn/bin/opmnctl startall
Ant 1.6.2 is shipped with OC4J and you have to set your PATH environment variable to $ORACLE_HOME/ant/bin. On some operating systems, Ant does not currently support the use of environment variables. If this is the case for your operating system, please modify the ant-oracle.xml file located in the %HOWTO_HOME% directory.
Edit ant-oracle.properties (in the demo directory) and ensure the following properties are set to the correct values, as indicated below for OC4J standalone:
If you are using OracleAS managed install then you have appropriately change the following properties beside changing oc4j.admin.user and oc4j.admin.password for your managed OC4J instance in OracleAS install.
You have to uncomment appropriate deployer.uri in the ant-oracle.properties based on your environment i.e. a single instance OC4J or a clustered OC4J instance/group managed by OPMN.
To build the application, type the following command from the %HOWTO_HOME% directory:
>ant
You should now have the newly created DIfromweb.ear in your %HOWTO_HOME%/lib directory.
This command will attempt to deploy the application archive if the build is successful. It will first test whether OC4J is running before attempting the deployment operation.
Note that you can also deploy the application separately . Ensure the %ORACLE_HOME% environment variable is defined, and from the %HOWTO_HOME% directory, type the command:
>ant deploy
Run the sample by providing invoking the following URL from your favorite browser:
http://localhost:8888/DIfromweb
In this page, enter employee no, Name and Salary and then click on Add Employee button.
The InsertServlet will be invoked that will try to create the entity instance invoking addEmployee method in EmployeeFacadeBean that uses EntityManager API.
You will be redirected to a success page if your record was inserted successfully. You can also check the database table to ensure the record was created.
In this document, you should have learned how to: