Saturday, 10 November 2018

Connecting Servlets to HANA as a Service on Cloud Foundry

Introduction


This tutorial will help you creating in a servlet and deploy it to SAP Cloud Platform with a Cloud Foundry instance using Eclipse.  The Eclipse landscape has been updated since the aforementioned blog was written and some of the prerequisites have changed for Eclipse. Additionally, this tutorial will connect the sample application to a HANA database schema on SAP Cloud Platform.  The tutorial uses JNDI and traditional SQL which is a common means to access the database.

Prerequisites:


◈ SAP Cloud Platform account with HANA as a Service entitlements (Note: the trial is soon expected to have HANA as a Service)
◈ Eclipse 2018-09 IDE for Java EE Developers


SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Step 1: Verify Eclipse Cloud Foundry Support.


1. Verify that your Eclipse installation has the necessary Cloud Foundry support by selecting “Help->Eclipse Marketplace”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Search for Cloud Foundry in the Eclipse Marketplace.  If it is not already installed, install it now.  You should see Eclipse Tools for Cloud Foundry 1.2.0.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

2. After installing the tools, allow Eclipse to restart. If you are not already in the Java development perspective, click on the “Workbench” button on the top right.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Step 2: Create a New Maven Project with a Servlet


1. Create a new Maven project by selecting “File->New->Maven Project” as shown below.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

2. Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

3. Select the maven archetype “webapp” as shown below.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

4. Specify “com.sap.cloud.sample” for Group Id and “hello-world” for Artifact Id. Press Finish.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

5. Lets create a servlet within the project. Ensure the previously created Maven project is selected, select “File->New->Servlet”

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

6. Specify “com.sap.cloud.sample” for the Java package and “HelloWorldServlet” for the Class name and press the Finish button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

7. For some reason the project does not currently follow the conventional Maven file structure for Java, so create a new folder call “java” under the main folder under source. Right click on the main folder and select “New->Folder”.  Specify the name “java” for the new folder.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

8. Move the “com” folder and sub-folders created when you created the servlet to the new “java” folder as shown below by dragging the “com” folder and dropping on top of the “java” folder. This is the file structure that Maven is expecting.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

9. The new servlet will have a Maven dependency on “javax.servlet-api”. Right click on your project select “Maven->Add Dependency”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

10. Enter the “javax.servlet” for the Group Id, “javax.servlet-api” for the Artifact Id and “3.1.0” for the version. (Note: your version may vary on your Maven repository).

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

11. Press the OK button.

Optional.  You could try at this point to package the application into war by right clicking on the project and select “Run As->Run Configurations”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Enter “package” as the “Goals” and verify you see a “Build Success” message in the console.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Step 3: Setup your SAP Cloud Platform Cloud Foundry Database


1. Assuming the packaging process worked you can move on to setup the database services on SAP Cloud Platform for the application. Open your browser and log into SAP Cloud Platform to a subaccount that has Cloud Foundry entitlements assigned for HANA and a simple Java application.  If you have not already created a space, create one now for the subaccount.  Below you will see that there is a space called “dev”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

2. Navigate to your space and select “Service Marketplace”. Select “SAP HANA Service”. This will be your main database and later you will create a schema on top of it.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

3. Select Instances on the left

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

4. Click on the “New Instance” button

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

5. Select the “standard” plan as you will not need enterprise features for this tutorial.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

6. Provide a password for your SYSTEM user.  Press the Next button as we will not need any additional features for the database.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

7. Click on the Next button as you will be binding the application using Eclipse later on in the tutorial.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

8. Provide a name of the instance and press the Finish button. Below “hana-std32″ was used as a instance name.  Good Work!  You have create your first HANA as a Service instance!

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

If you wanted to accomplish the same thing via the Cloud Foundry command line, the command to do this would be as shown below:

cf create-service hana-db standard hana-std32 -c “{\"SystemPassword\":\"<password>\",\"memory\":\"2\"}”​

9. Click on your space name again and select “Service Marketplace” again. Select “SAP HANA Schemas & HDI Containers”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

You have several different options for HANA as a Service.  If you were doing development using SAP’s Core Data Services (CDS) and Multi-Target Applications (MTA), you likely would select the HDI Shared option, there is a great blog on doing this in the useful resource section.  If you are doing more traditional SQL style development, you likely would be using the “schema” option.  Each of options will be tied to the previous database service that you just created.  This tutorial will use a “schema”.

10. Select Instances on the left.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

11. Click on the New Instance button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

12. Select “schema” plan and press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

13. You can give your schema name using the JSON in the image below.  By doing this, your application could reference table as MYSCHEMA.tablename.  Otherwise you will get a system generate schema name.  Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

14. We could bind the application if we had it ready at this point.  As mentioned earlier, you will bind the application with the database using Eclipse later on in the tutorial.  Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

15. Enter the instance name for the schema as shown below and press the Finish button.  When setting up JNDI configuration (later on in this document) the definitions reference the name “myHANAdb”. If you use a different name, you will need to change the context.xml to reflect the name you used here.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

The schema gets assigned to the database service you created in step 8 earlier.  Each database has a GUID which you can get from the Cloud Foundry CLI.  If you wanted to get the GUID for the database you created you would use the following Cloud Foundry command.

cf service hana-std32 –guid​

Using the command below you can create the HANA schema and associated with the hana-std32 database through the GUID.

cf create-service hana schema myHANAdb -c "{ \"database_id\":\"putyourGUIDfromabovehere\", \"schema\":\"myHANAdb\" }"​

Step 4: Add Database code to the Servlet


1. Let’s add the code necessary for the database access to your application. Switch back to the Eclipse environment.  Open the web.xml file located under src -> main-> webapp.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

In the location where you see <insert code here> above, paste the following XML snippet to web.xml.  Note that you wont have <insert code here> tags in your web.xml.  This definition establishes the name “jdbc/DefaultDB” as a JNDI JDBC resource for the application.

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/DefaultDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

Save and close the web.xml file.

2. Create the file context.xml in META-INF folder. Right click on the webapp folder and select “New->Folder”. Name the folder “META-INF”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Right click on the new folder “META-INF”, select “New->File”.  Name this file context.xml (use this exact case for the name).

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

3. Paste following XML into the context.xml file. This definition establishes that “jdbc/DefaultDB” will connect to a service named myHANAdb (which you just created).  Save and close the context.xml file.  Again if you used a different instance name above in step 15, you will need this file to reference the name used in step 15.  The factory object referenced in the XML below will be provided by the sap_java_buildpack when you deploy the application to Cloud Foundry.

<?xml version="1.0" encoding="UTF-8"?>
<Context>  <Resource name="jdbc/DefaultDB"
    auth="Container"
    type="javax.sql.DataSource"
    factory="com.sap.xs.jdbc.datasource.tomcat.TomcatDataSourceFactory"
    service="myHANAdb"/>
</Context>

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

4. If the HelloWorldServlet.java file is not already opened, open it and position the cursor under the first import statement. Paste the following code to include key libraries for JDNI and JDBC.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;​

Expanded the import section should appears as shown below.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

5. Scroll down to the “doGet” method. Comment out the line after “// TODO Auto-generated method stub”, Paste the following code into the editor after your new comment.

             Context ctx = null;
             Connection con = null;
             Statement stmt = null;
             ResultSet rs = null;
             try {
               ctx = new InitialContext();
               DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDB");
               con = ds.getConnection();
               stmt = con.createStatement();
               rs = stmt.executeQuery("select current_user as myuser from dummy");
               rs.next();
               response.getWriter().append("Served at:").append(rs.getString(1)).append(request.getContextPath());
             }catch(NamingException e){
                    e.printStackTrace();
             } catch (SQLException e) {
                    e.printStackTrace();
             }​

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

Step 5: Add the Cloud Foundry Server and Run the Application


1. Let’s add the Cloud Foundry Server. Under the Servers window, click on “Click on this link to create a new server”.  If you already had a server in the window, right click on the “Servers” window and select “New->Server”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

2. Select Cloud Foundry and provide a name. Below the name “Cloud Foundry” is used. Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

3. Enter your SAP Cloud Platform user and password. Click on the “Manage Cloud…” button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

4. Click the Add button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

5. You will need to get the Cloud Foundry API Endpoint URL for your cloud instance.  Switch back to your browser and navigate to the overview section of your subaccount.  The URL will be on the right with a heading of API Endpoint.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

6. Copy the URL.  Switch back to Eclipse and paste the URL into the URL field.  Provide a name for the Cloud URL.  Press the Finish button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

7. Press the OK button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

8. Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

9. Select the space under which you had setup the database services.  Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

10. Select your “hello-world” application and press the Add button.  Press the Finish button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

In the “Servers” window, you will see that your application is assigned to the new server but not deployed.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

11. Lets clean the project to ensure we are getting the latest code.  Right click on the project and select “Run As->Maven clean”.  Ensure your directory structure has the java folder under the main and not resource folder under “src”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

12. Right click on the project and select “Run As->Run on Server”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

13. Select the Cloud Foundry server that you created earlier and click on the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

14. Click on the Finish button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

15. You will be prompted for Application details, enter the “sap_java_buildpack” for the Buildpack URL and check the box to update the manifest file. Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

There several buildpacks on SAP Cloud Platform.  The following Cloud Foundry command will provide a list of buildpacks avalable:

cf buildpacks

16. Change the memory limit to 1024. Press the Next button.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

17. Bind to the schema instance name “myHANAdb” that you created earlier by clicking the check box for “myHANAdb”.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

18. Press the Finish button

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

In the console you will see the following output:

Checking application - hello-world
Generating application archive - hello-world
Pushing application - hello-world
Creating application - hello-world
Application successfully pushed
Starting application - hello-world
[Application Running Check] - Checking if application is running - hello-world. Please wait...
[Application Running Check] - Application appears to be running - hello-world.​

Eclipse may not use https in the attempt to run the application and may fail.  If it uses http, change the URL from “http” to “https” when launching the browser.Also, the process of verifying the application may not appear to be complete within Eclipse very quickly but if you switch to your browser and navigate to the space and you will see your application listed under application as hello-world, you will find an URL under Application Routes for the Application route.  Click on this link for the application route and you will see Hello World! In the browser.  Before you click on the URL, ensure you see the green “Started” message as shown below.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

19. Append the text “/HelloWorldServlet” to the URL above and you should see “Served at: MYSCHEMA” which is illustrates that you are connected to the database with a user name MYSCHEMA.

SAP HANA Study Materials, SAP HANA Tutorial and Materials, SAP HANA Guides, SAP HANA Learning, SAP HANA Live

1 comment: