EJB calling Java Stored Procedure
Table of Contents
Overview of the application
|
|
This Sample application demonstrates a Simple EJB implementation
for sending mails using Java Mail API. This Sample also demonstrates
how to invoke a Java Stored Procedure from an EJB.In this Sample,
a Java Stored Procedure namely 'sendMail', sends mails to appropriate
recipients. The EJB invokes the Java Stored Procedure, sendMail(),
with proper parameters like recipient addresses, Message Text and
Subject of the email. The Java Stored Procedure "sendmail"
is implemented to send mails through SMTP server using Java Mail
API .The Java Mail API provides a set of abstract classes that model
a mail system. The API provides a platform independent and protocol
independent framework to build Java technology-based mail and messaging
applications.
The sample application prompts the user to enter the From address,
To address, Cc address if any, subject and message text of the mail.
After entering valid information in above fields, user can press
"Send" button to send the mail to corresponding recipients.
Steps in the Sample
- Upon invoking application, user is provided with a GUI where
he/she can input all the Email information like TO Address, CC
Address, Subject and Message Text.
- User can enter recipient addresses in the text fields 'To'
and 'CC'. He/she can enter his/her own email address in the From
Text field.
- In Subject Text field , subject of the Email should be entered
whereas Message Text of the email should be entered in the provided
canvas.
- Upon pressing the "Send" button, the validity checking
of the Email addresses is done.
- After validity checking, the client application, looks for
the EJB published as 'Mailer', creates a new EJB instance and
access the method callJSP() to send the corresponding Email.
- Upon pressing "Exit" , the application exists.
The software required for running the sample are :
- Oracle9iAS
(9.0.2.0.0) Containers for J2EE (or higher) downloadable
from here
- Oracle9i
Database Release 9.0.2 (or higher) - Production downloadable
from here
- Oracle9i JDeveloper
9.0.3 downloadable from here
(or) Ant Version 1.5 downloadable from here
This following notations are used through out this document
|
Notation
|
Description
|
|
<OC4J_HOME>
|
Folder where OC4J is installed. For example, D:\oc4j
|
|
<SAMPLE_HOME>
|
Folder where the EJBCallsJSP sample will be unzipped. For example, C:\OTNSamples
|
|
<JAVA_HOME>
|
Folder where JAVA is installed. For example, C:\jdk1.2
|
|
<ANT_HOME>
|
Folder where the ANT is installed. For example, C:\ant
|
Extracting the source code
|
|
Execute the following command to extract the sample application.
In windows environment, this file can also be opened using Winzip
utility.
The above command extracts all the sample files to EJBCallsJSP
folder. Click here to view the directory
structure and description of sample files
Configuring the application
|
|
Preparing the Database
Step 1: Ensure that
'Travel Schema' is loaded into the database. For more details about
loading the Travel Schema Click here.
Step 2: Grant permission
to read system properties, by using the following command.
| SQL> |
CONNECT
sys/change_on_install@db_sid AS SYSDBA |
| SQL> |
EXEC dbms_java.grant_permission(
'TRAVEL','SYS:java.util.PropertyPermission', '*', 'read,write'); |
|
Step 3: Edit <SAMPLE_HOME>\config\config.properties.
Provide SecurityPrincipal and SecurityCredentials. These values
will be used while looking up the EJB, by the Client program.
Step 4: Edit <SAMPLE_HOME>\src\oracle\otnsamples\ejbcallsjsp\jsp\MailJSPSample.java
Change the String host = "gmsmtp01.oraclecorp.com" and
provide the mail server appropriately.
Deploying and Running the application
using Oracle9i
JDeveloper
|
|
This section describes the steps required
in deploying this application to the Standalone OC4J using Oracle9i
JDeveloper
Step 1:. Open the <SAMPLE_HOME>\EJBCallsJSP\EJBCallsJSP.jws
in Oracle9i
JDeveloper. The workspace contains
EJBCallsJSP.jpr (project file), deployment files and other files.
Step 2: Create Database Server
Connection
- Select Database Server under the
Connections Node.
- Right click on Database Servers and select "New Connection".
- In Step 1 of the wizard, Enter the Connection Name as 'Travel'.
- In Step 2 of the wizard, Provide 'travel' as user name and
password for the travel schema. (by default travel).
- In Step 3 of the wizard, Provide
the appropriate database connection information where travel schema
is loaded.
- In Step 4 of the wizard, Test the connection and Select 'Finish'
button
Step 3: Create Standalone OC4J
Connection
- Select Application Server under
the Connections Node.
- Right click on Application Servers and select "New Connection".
- In Step 1 of the wizard, Enter the Connection Name and select
Connection Type as 'Standalone OC4J'
- In Step 2 of the wizard, Provide 'admin' user name and password
for the admin user.
- In Step 3 of the wizard, Check
if the URL targets to the machine, where OC4J runs.
- In Step 4 of the wizard, Test the connection and Select 'Finish'
button. Ensure that Standalone OC4J is started.
Step 4: Deploy the Java Stored
Procedure to the database.
- Right click MailerJSP.deploy under EJBCallsJSP.jpr
- Select 'Deploy to ' the connection created in Step 2.
This deploys the java stored procedure and associated libraries
to the targeted database.
Step 5: Deploy the EJB to the
Standalone OC4J
- Right click MailerEJB.deploy under EJBCallsJSP.jpr
- Select 'Deploy to ' the connection created in Step 3.
This deploys the EJB to the Standalone OC4J.
Step 6: Right click EJBCallsJSP.jpr
and select "Make EJBCallsJSP.jpr". This compiles all the java classes
in the project.
Step 7: Right click EJBCallsJSP.jpr
and select "Run EJBCallsJSP.jpr". This runs the MailClientSample.java
Deploying and Running the application
using ANT
|
|
This section describes the steps required
in deploying this application to the Standalone OC4J using ANT Tool.
Step 1: SET Environment
Variables
- Open the Command Prompt and Go to the <SAMPLE_HOME>\EJBCallsJSP
directory
- Ensure that <JAVA_HOME>\bin
is in the PATH
- Ensure that <ANT_HOME>\bin
is in the PATH
Step 2: Build the EAR
file
From <SAMPLE_HOME>\EJBCallsJSP, execute ant
| <SAMPLE_HOME>\EJBCallsJSP>ant |
|
This will create
the EJBCallsJSP.ear file.
Step 3: Having created a EJBCallsJSP.ear
file, to deploy this application to Standalone OC4J, use the following
command, from <SAMPLE_HOME>/EJBCallsJSP directory.
| > |
java
-jar <OC4J_HOME>\j2ee\home\admin.jar ormi://localhost:23791
admin <oc4j password> -deploy -file <SAMPLE_HOME>\EJBCallsJSP\EJBCallsJSP.ear
-deploymentName EJBCallsJSP |
| For
example, |
| > |
java
-jar D:\oc4j\j2ee\home\admin.jar ormi://localhost:23791
admin welcome -deploy -file C:\OTNSamples\EJBCallsJSP\EJBCallsJSP.ear
-deploymentName EJBCallsJSP |
|
Step 4: Add data source
to the application
Execute the following command, substitute database host name, database
listening port and database sid appropriately.
| > |
java
-jar <OC4J_HOME>\j2ee\home\admin.jar ormi://localhost:23791
admin <oc4j password> -application EJBCallsJSP
-installDataSource -url jdbc:oracle:thin:@<DB_HOST>:<DB_PORT>:<DB_SID>
-location jdbc/TravelDS -username travel -password <schema
password> -connectionDriver oracle.jdbc.driver.OracleDriver
-className oracle.jdbc.pool.OracleConnectionPoolDataSource |
| For
example, |
| > |
java
-jar D:\oc4j\j2ee\home\admin.jar ormi://localhost:23791
admin welcome -application EJBCallsJSP -installDataSource
-url jdbc:oracle:thin:@demo.oracle.com:1521:orcl -location
jdbc/TravelDS -username travel -password travel -connectionDriver
oracle.jdbc.driver.OracleDriver -className oracle.jdbc.pool.OracleConnectionPoolDataSource |
|
Step
5: Now, deploy the following jar and class libraries to
the database, using the following load java command.
- <SAMPLE_HOME>/EJBCallsJSP/EJBCallsJSP-jsp.jar
- <OC4J_HOME>/j2ee/home/lib/mail.jar
- <OC4J_HOME>/j2ee/home/lib/activation.jar
| > |
loadjava
-user travel/<schema password>@<DB_HOST>:<DB_PORT>:<DB_SID>:
-v -f -r -t -s -g public <jarfile> |
| For
example, |
| > |
loadjava
-user travel/travel@demo.oracle.com:1521:orc1 -v -f -r
-t -s -g public C:\OTNSamples\EJBCallsJSP\EJBCallsJSP-jsp.jar |
|
Repeat the above step to load the classs libraries mail.jar and
activation.jar.
Step 6: Run the sample
Add the following class libraries
to the class path and execute the jar.
<OC4J_HOME>/j2ee/home/oc4j.jar
<OC4J_HOME>/j2ee/home/lib/ejb.jar
<SAMPLE_HOME>/EJBCallsJSP/EJBCallsJSP-client.jar
and execute the following command
| >java
oracle.otnsamples.ejbcallsjsp.mailclient.MailClientSample |
|
Description of sample files
|
|
Directory |
Filename |
Description |
EJBCallsJSP |
build.xml |
This XML file is used by ANT to create the application
ear file |
EJBCallsJSP.jws |
JDeveloper WorkSpace File |
EJBCallsJSP.jpr |
JDeveloper Project file |
MailerEJB.deploy |
EJB Deployment Profile |
MailerJSP.deploy |
Java Stored Procedure deployment Profile |
EJBCallsJSP\src\oracle\otnsamples\ejbcallsjsp\ejb |
Mailer.java |
This is Remote interface defines of the Mailer EJB |
MailerHome.java |
This is the Home Interface of the Mailer EJB |
EJBCallsJSP\src\oracle\otnsamples\ejbcallsjsp\ejb\impl |
MailerBean.java |
This is the bean implementation class of the Mailer
EJB |
EJBCallsJSP\src\oracle\otnsamples\ejbcallsjsp\jsp |
MailJSPSample.java |
This class contains logic to send the mail. This
class is deployed to the database as java stored procedure. |
|
EJBCallsJSP\src\oracle\otnsamples\ejbcallsjsp\mailclient
|
MailClientFrame.java |
This class brings up GUI for the Mail Client |
MailClientSample.java |
This class contains business logic for the Mail Client |
EJBCallsJSP\src\oracle\otnsamples\ejbcallsjsp\utils
|
GridBagConstraints2.java |
This utility class extends GridBagConstraint by overloading
the constructor. |
SysSetting.java |
This class reads the properties from the config.properties
file. |
EJBCallsJSP\docs |
Readme.html |
This file. It contains description about the sample
and steps to deploy and run the sample. |
EJBCallsJSP\config |
application.xml |
This XML describes the sample application. |
ejb-jar.xml |
This XML describes the Mailer EJB |
config.properties |
This file contains sample properties. |
Please enter your comments about this sample in the OTN
Sample Code Discussion Forum.
|