X

Learn Tips and Best Practices from the Oracle JDBC Development

  • July 14, 2017

Create and Deploy a Java Servlet using WebLogic Server (WLS)

Nirmala Sundarappa
Principal Product Manager

Oracle WebLogic Server(WLS) is one of the popular Java EE containers.  In this blog, we want to highlight the steps required to create a Java servlet that connects to Oracle Database using Oracle JDBC and UCP.  The blog contains step-by-step instructions from installing WebLogic Server to deploying a servlet that connects to the Oracle database and retrieves data from the database.  Refer to the table below for more details.

Step Number Description
Step 1 Download the WebLogic Server v12.2.1.2 and Unzip the contents
Step 2 Install the WebLogic Server v12.2.1.2
Step 3 Create a domain
Step 4 Start the WebLogic  Server and access the admin console
Step 5 Create an Oracle Datasource from the console
Step 6 Create a Java Servlet that uses the Datasource created
Step 7 Compile the Java Servlet
Step 8 Deploy the WAR file and Invoke the Servlet from the browser
Step 9 Questions and Answers
 

Step 1:

Download the WebLogic Server v12.2.1.2.  Make sure to download "Quick Installer for Mac OSX, Windows, and Linux".  Unzip the contents of the Installer which includes read me files and fmw_12.2.1.2.0_wls_quick_Disk1_1of1.zip.

Step 2:

Install the WebLogic Server by using the command "java -jar fmw_12.2.1.2.0_wls_quick.jar".  This takes sometime and wait for the installation to complete.
It creates a folder "wls12212". Refer to this location as WLS_HOME.

Step 3:

Create a domain in WLS1.  A domain is required to start the adminstration server. Invoke the "Configuration Wizard" by running the config.sh from "WLS_HOME/oracle_common/common/bin/config.sh".  Follow the configuration wizard to
(a) Choose "Basic Weblogic Server Domain -12.2.1.2.0[wlsserver]*" which is the default option
(b) Enter weblogic username/password
(c) Choose "Administrator Server"
(d) Enter the port number if anything other than 7001 is required
(e) The configuration completes displaying the Admin Server URL: http://localhost:7001/console

Fig 4.1:  Configuration Wizard

Step 4:

Start the WebLogic Server by invoking startWebLogic.sh from "WLS_HOME/user_projects/domains/base_domain/bin/startWebLogic.sh"
Once the admin server is started, you can access the WebLogic console at http://localhost:7001/console.  Login with the username/password created from Step 4. 

Fig 4.1: WebLogic Administrator Console

Step 5:

Create an Oracle Datasource from the console. 
(a) Services --> Data Sources --> Click on New --> Choose JDBC or UCP or Active Grid Link (AGL) datasource from the drop down menu as shown in Fig 5.1.
(b) Choose "Generic Data Source" (JDBC only) or "UCP Data Source"(UCP).  Enter the Name, JNDI Name and choose the Database Type. JNDI name will be used in the servlet.  Name this as "orcljdbc_ds"
(c) Follow the steps of choosing the driver, entering the database information.  A summary page looks like Fig 5.2.  Click on "Test Connection" to verify the connection to the database.  Make sure that the connection is successful as shown in Fig 5.3 before we proceed with using this datasource in the Java servlet.
(d) Make sure to associate the Target for the new datasource created as shown in Fig 5.4. Click on "FINISH" to save the datasource.

Fig 5.1: Drop down menu of Datasources

Fig 5.2: Database details summary page

Fig 5.3: Successful connection test message

Fig 5.4: Associate Target of the Datasource to an AdminServer

Step 6:

Create a Java Servlet to use the Oracle datasource(orcljdbc_ds) created to obtain a connection.
A sample JDBCSample_Servlet.java is provided on Github for reference.  Below is the code fragment to obtain the connection.

 /* 
  * Method that creates the datasource for the JNDI lookup
  */
private DataSource getDataSource() throws NamingException {
  Context ctx = new InitialContext();
  // Look up for the JNDI datasource
  javax.sql.DataSource ds
        = (javax.sql.DataSource) ctx.lookup ("orcljdbc_ds");
  return ds;
} 
//For JDBC: Get the Oracle Datasource 
DataSource ds = getDataSource();
try (Connection connection = ds.getConnection()) {
...
}
//For UCP : Use PoolDataSource to get the connection 
PoolDataSource pds = (PoolDataSource) getDataSource();
try (Connection connection = ds.getConnection()) {
...
}

Step 7:

Compile the Java Servlet created.  Use WLS_HOME/wlserver/server/lib/weblogic.jar that is required to compile the servlet.  Refer to the build.xml for more details that can be used to build the war file.
Copy the war file to the location WLS_HOME/user_projects/domains/base_domain/JDBCSample.war

Step 8:

Deploy the war file and Invoke the servlet.
Go to Deployments --> Install --> Choose the war file to be deployed and follow instructions. 

Fig 8.1: Deploying a war file in WLS

Fig 8.2: Screenshot after invoking the Servlet

Step 9 : Questions and Answers

Question: Where is the JDBC driver located ?
Answer: $MW_HOME is the place where the WebLogic Server is installed. The JDBC driver is present at $MW_HOME/oracle_common/modules/oracle.jdbc/ojdbc8.jar. You can use $java -jar ojdbc8.jar command to check the JDBC version and more details as shown below.

➜  oracle.jdbc java -jar ojdbc8.jar
Oracle 12.2.0.1.0 JDBC 4.2 compiled with javac 1.8.0_91 on Tue_Dec_13_06:08:31_PST_2016
#Default Connection Properties Resource
#Fri May 25 15:37:18 PDT 2018

***** JCE UNLIMITED STRENGTH IS INSTALLED ****

Question: How do I use a different JDBC driver in WLS?
Answer:  Make sure you place the JDBC driver that you want at $MW_HOME/oracle_common/modules/oracle.jdbc/*. If you are using any JDBC driver other than the one that is already present then you will need to add the path of this driver explicitly at $WLS_HOME/oracle_common/common/bin/commExtEnv.sh as shown here.

 WEBLOGIC_CLASSPATH="${MW_HOME}/oracle_common/modules/oracle.jdbc/ojdbc8.jar${CLASSPATHSEP}..." $WLS_HOME

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.