X

Best Practices from Oracle Development's A‑Team

Using JSP on Oracle Compute and Oracle DBaaS - End to End Example

Roland Koenn
Consulting Solution Architect

Introduction

Many customers request to get a quick demo how they would deploy their custom Java application in the Oracle Cloud. A great way to do this is Oracle Compute Service which can easily be combined with the Oracle Database as a Service offering. In this example two VMs will be deployed. One for the application server - GlassFish. The second on is a DBaaS VM to hold the database. A simple JSP will be created to display data from the database on the client browser as shown below.

Drawing1

 

Deploying Oracle DBaaS

For this example a simple Database will be deployed in the Cloud. In order to achieve this; first login to “My Services” from cloud.oracle.com.

image1

Enter your Identity Domain

image2

And provide your username and password.

image3

Scroll down until you find the Oracle Database Cloud Service and click on “Service Console”

image4

For this example select “Oracle Database Cloud Service” and based on your billing preference choose Monthly or Hourly. Finalise your selection by clicking “Next”.

image5

For this example we will use the Pluggable Database Demos and hence will select “Oracle Database 12c Release 1”

image6

 

For this example any edition can be selected.

image7

 

In this screen select a service name, e.g. “clouddb” and fill the other information as per screenshot below. Make sure to select the checkbox “Include “Demos” PDB”. Once done click the Edit Button next to SSH Public Key.

image8

Download Putty Key Generate (PuTTYgen.exe) from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

After the download start the puttygen.exe and click generate with SSH-2 RSA and 1024 select. Randomly move your mouse over the blank area to generate the key.

The procedure for other Operating Systems can be found here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/javaservice/JCS/JCS_SSH/create_sshkey.html

image9

Once generated save the private key - it is recommended to use a pass-phrase in most cases and copy and paste the public key into a new text file to be saved with the private key.  Save the keys in a secure location and take a backup as there will be no access to the VMs if the keys are lost. Note using the Save public key option will not save the public key in the required format.

image10

Keep the public key in your clipboard and paste it into the “Public key input for VM access” finish by clicking “Enter”.

image11

Review and Confirm all details in the next screen and click “Create” to start the build of the database.

image12

You will see when the database is provisioned in the Service Console. Open the detail page and note the Public IP Address of the Database for later use.

image13a

 

Compute Provisioning

Scroll down in the Main Menu to “Oracle Compute Cloud Service” and open the Service Console.

image14

Create a new Compute Instance and give it a meaningful name. For this example the Oracle Linux 6.6 Image with oc1m shape is sufficient. Click Next.

image15

Select the DNS Hostname Prefix carefully as this will be the hostname of the VM that gets provisioned. Select a Persistent Public IP Reservation or choose Auto Generated to have the first available Public IP assigned to the VM.

image16

You can add additional storage – however this example won’t require additional storage.

image17

At the SSH Public Keys point copy and paste the Public Key generated above, give it a name and click Next. You can create a separate Key for this VM, if you prefer following the steps outlined earlier.

image18

Review the selection and press “Create” to start the VM provisioning.

 

image19

Once done the Instance will show up in the Service Console. Write down the public IP it will be required shortly.

image20

 

Connect to the VMs

In this example Putty is used to connect to the VMs. Putty can be downloaded here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Start Putty and enter the Public IP of the Database VM in the Hostname field.

image21

Navigate to Connection – SSH – Auth and click browse to select your previously created Private Key.

image22

Head back to the Session Menu - Enter a name for this session – Click Save followed by Open.

image21

For the first connection you will recive a Security Alert – this is expected. Answer with Yes.

image23

Next the Terminal opens and you can simply enter the username opc.

image24

Security Configuration

Validate the listener is running by executing “lsnrctl status” with the user oracle as shown below on the Database VM - click on the picture to enlarge. You should see a separate service for the demos PDB.

image25

Open the Compute Service Console and select the Network Tab. Click on Create Security IP List to start.

image26

Enter the IP of all hosts that you would like to give similar Network Security Settings. For this example it’s sufficent to use the Compute VM. Enter a description that describes the list.

image27

Switch to the Security Rules tab and hit the “Create Security Rule” button.

image28

The application in this example will use port 1521 for the communication with the database, which is provisioned as part of the database provisioning for each instance with the name ora_dblistener. Select the Security IP List created above as the source and select the Database VM as the destination for this Security Rule.

image29

Creating this list will enable the communication between the VMs on Port 1521.

Testing communication between the compute and Database VM.

A great and simple way to test communication is telnet. Please note you might have to install it on your Compute VM with “yum install telnet” as shown here:

image30

Once installed the connection can be tested with telnet as shown below. If you receive the “Escape character is …” message the connection is working.

image31

In order to verify that there are no superseeding rules to the rule just created it is useful to disable the rule and test wheter communication is still possible.

Disable the rule via the Security Rules screen from the Network section. Click on the context symbol next to the rule and select update from the context menu.

image32

In the Update Security Rule screen, set the status to Disabled and submit the change by clicking the Update button.

image33

The communcation should be closed between the Compute VM and the Database VM. Try to telnet – if the rule is setup correctly the communication will timeout.

image34

Reopen the Update Security Rule Dialog and set the status to Enabled.image35

Using telnet verify that the communication is running again, if the rule is correct it should work again. This confirms that the rule is working as expected and that there are no superseeding rules.

image31

Allow access to Compute VM to the Public Internet

In this example the application will be exposed to the public internet. Consider this carefully when using your own data in the backend.

Create a Security Application from the Network Tab. This example is using Port 4848 and 8080. Port 4848 is the Administration Port for the Glassfish Server - this rule should be disabled after the configuration is finished.

image36

Create a Security List with the Inbound Policy of Deny to block all traffic except the explicitly allowed traffic. You can allow packets to travel outbound from the Cloud VM by selecting Permit in the Outbound Policy.

image37

Once the Security List is created, you will need to add the Compute VM to the List. Do this by opening the Service Console for the Instance and click “Add to Security List”.

image38

This open a drop-down list, where the created Security list needs to be selected. Attach the Security List to the VM.

image39

Combine the Security List, Security Application and Security IP list by creating a new Security Rule. Make sure to select the predefinded Security IP List “public-internet” to grant access to every host. The destination has to be the Security List created above to allow access to the Compute VM. Ensure to select the correct Security Application has been selected to allow access on Port 4848 for this example.

image40

Repeat the process for Port 8080 to allow application access.

image41

Create the corresponding Security Rule:

image42

Application Deployment

Download the latest JDK from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Download the latest Glassfish release here:

https://glassfish.java.net/download.html

Download the latest Oracle JDBC Driver here (ojdbc7.jar only)

http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html

 

Extract both archives to /u01.

sudo su – oracle tar xvzpf jdk-8u71-linux-x64.tar.gz unzip glassfish-4.1.1.zip export PATH=/u01/jdk1.8.0_71/bin:${PATH} glassfish4/bin/asadmin create-domain clouddomain glassfish4/bin/asadmin start-domain clouddomain glassfish4/bin/asadmin --host localhost --port 4848 enable-secure-admin cp ojdbc7.jar glassfish4/glassfish/domains/clouddomain/lib glassfish4/bin/asadmin restart-domain clouddomain
 <a href="https://www.atcfiles.com/wp-content/uploads/2016/02/image43.png" rel="attachment wp-att-36642"><img src="https://www.atcfiles.com/wp-content/uploads/2016/02/image43.png" alt="image43" width="600" height="485" class="alignnone wp-image-36642" /> </a>

You can now login to the Admin Console from your local browser:

image44

Create JDBC Connection

Using the asadmin tool the JDBC Connection is created quickly:

glassfish4/bin/asadmin create-jdbc-connection-pool --restype javax.sql.DataSource --datasourceclassname oracle.jdbc.pool.OracleDataSource --property "user=hr:password=hr: url=jdbc\\:oracle\\:thin\\:@<your-cloud-ip>\\:1521\\/demos.rdb.oraclecloud.internal" CloudDB-Pool glassfish4/bin/asadmin ping-connection-pool CloudDB-Pool glassfish4/bin/asadmin create-jdbc-resource --connectionpoolid CloudDB-Pool jdbc/CloudDB

This application is based on this example only modified to connect to the HR Schema in the Demo PDB: https://docs.oracle.com/cd/E17952_01/connector-j-en/connector-j-usagenotes-glassfish-config-jsp.html

Create a folder with the following directory structure:

index.jsp WEB-INF    |    - web.xml    - sun-web.xml

The code for sun-web.xml is:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd"> <sun-web-app>   <context-root>HelloWebApp</context-root>   <resource-ref>     <res-ref-name>jdbc/CloudDB</res-ref-name>     <jndi-name>jdbc/CloudDB</jndi-name>   </resource-ref> </sun-web-app>

The code for web.xml is:

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   <display-name>HelloWebApp</display-name>   <distributable/>   <resource-ref>     <res-ref-name>jdbc/CloudDB</res-ref-name>     <res-type>javax.sql.DataSource</res-type>     <res-auth>Container</res-auth>     <res-sharing-scope>Shareable</res-sharing-scope>   </resource-ref> </web-app>

The index.jsp contains:

<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %> <html> <head><title>Data from the cloud with JSP JSP</title></head> <body> <%   InitialContext ctx;   DataSource ds;   Connection conn;   Statement stmt;   ResultSet rs;   try {     ctx = new InitialContext();         ds = (DataSource) ctx.lookup("jdbc/CloudDB");     conn = ds.getConnection();     stmt = conn.createStatement();     rs = stmt.executeQuery("SELECT * FROM DEPARTMENTS");     while(rs.next()) { %>     <h3>Department Name: <%= rs.getString("DEPARTMENT_NAME") %></h3>     <h3>Department ID: <%= rs.getString("DEPARTMENT_ID") %></h3> <%     }   }   catch (SQLException se) { %>     <%= se.getMessage() %> <%   }   catch (NamingException ne) { %>     <%= ne.getMessage() %> <%   } %> </body> </html> 

Zip up the entire folder and login to the Glassfish Admin Console

image44

Select the option Applications in the tree on the left hand side and click on deploy.

image45

Select the previously created zip file and set “Web Application” as Type.

image47

Clicking OK will take you to the Deployed Applications screen from where you can press “Launch” to open a new browser window. The JSP will show the rows of the department table from the Cloud DB proving end-to-end communication.

image48

This concludes this example. It should illustrate how simple it is to deploy a custom application with an Oracle Cloud Database in the backend.

 

Further Reading

 

Oracle Cloud Documentation

https://docs.oracle.com/cloud/latest/

Glassfish

https://glassfish.java.net/

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha