Skip to Content

Create a basic Java app in SAP Cloud Platform

Previous

Create a basic Java app in SAP Cloud Platform

By akula86

Create a basic Java app in SAP Cloud Platform

You will learn

In this tutorial series you will start from zero and develop a fully operational weather application. In total there are 10 parts to the series, each building on top of its predecessor. The entire source code of both the final and all intermediate parts are available on GitHub.

Details

The constituent parts of this tutorial series cover the following:

  • How to create a simple web application on HCP
  • How to apply authentication and authorization
  • How to expose business functionality as an external RESTful API
  • How to add JPA-based persistence to your web app
  • How to leverage the multi-tenancy features of SAP Cloud Platform
  • How to use the connectivity service to consume external services
  • How to add a mobile-friendly UI5-based user interface to the web application

In Part 1, you will develop a basic Java app to ensure that both Eclipse IDE and the local SAP Cloud Platform (HCP) tooling have been properly installed and configured.


Step 1: Create a new dynamic web project

Create a new dynamic web project by selecting the File > New > Dynamic Web Project menu entry and enter the following information:

  • Name: weatherapp
  • Target Runtime: Java Web
  • Dynamic Web Module Version: 2.5

Click on Next

Creating a new dynamic web project
Please log in to access this content.
Step 2: Add new folder

Remove the standard src Source folder and add a new one called src/main/java to create a project that adheres to the standard Maven Directory Layout.

Change the default output folder to target/classes

Click on Next.

Configuring java project
Please log in to access this content.
Step 3: Change the Content Directory

Change the Content Directory from WebContent to src/main/webapp (again, to adhere to Maven conventions)

Click on Finish.

specifying output directory to comply with Maven conventions
Please log in to access this content.
Step 4: Add details to weather app project

Make sure you have your weatherapp project folder selected, and then create a new Servlet by selecting the File > New > Servlet menu entry and enter the following information:

  • Package name: com.sap.hana.cloud.samples.weatherapp.web
  • Class name: HelloWorldServlet

Click on Next.

Creating a new servlet
Please log in to access this content.
Step 5: Change the URL Mapping

Change the URL Mapping from /HelloWorldServlet to /hello to make it a bit easier to memorize.

Click on Finish and the HelloWorldServlet.java file will open in the editor.

Changing the Servlet URL mapping
Please log in to access this content.
Step 6: Replace comment with code

Now we need to do our first bit of coding. Navigate to the servlet’s doGet() method and replace the TODO comment with the following line of code and save your changes:

response.getWriter().println("Hello World!");
Modifying the doGet() method
Please log in to access this content.
Step 7: Run on server

Deploy the application to your local server by using the Run as > Run on Server context menu of the HelloWorldServlet node in the Project Explorer view.

Please log in to access this content.
Step 8: Manually define a new Server

Choose the Manually define a new Server option and select the SAP / Java Web Server option from the server selection. Make sure to select Java Web as the server runtime environment.

Click on Finish. The internal browser is now started and displays the traditional message marking the first step into a new programmer’s journey.

Creating a new local Java Server to run your app
Please log in to access this content.

Next Steps

Updated 03/24/2017

Time to Complete

10 min

Intermediate

Tags

Prerequisites

Next
Back to top