Over a million developers have joined DZone.

Step by Step Java Web Application Development using IBM Bluemix PaaS (Part 2)

DZone's Guide to

Step by Step Java Web Application Development using IBM Bluemix PaaS (Part 2)

· Cloud Zone
Free Resource

Linkerd, the open source service mesh for cloud native applications. Get the complete guide to using Linkerd and Kubernetes to build scalable, resilient applications.

Lesson 2 - Develop an web java application written once and run it everywhere

For this tutorial, I chose the second approach of developing on Bluemix™ (presented within the previous article - Step by Step Java Web Application Development using IBM Bluemix™  PaaS (Part 1) ). In the previous lesson, we have created an application through Bluemix™ dashboard, binded it to a database service and now we have its first version stored locally, in our IDE workspace.  Code of the application can be found at the following url: https://github.com/ammbra/JSPSample 

Step 1 - Setup the development environment

If you are not familiar with GIT  and its actions, in order to clone a repository you need to give the url of the remote branch (in this case the master branch of the GIT url available on Bluemix™ dashboard) and as credentials your Bluemix™ credentials.  Cloning a repository has the advantage of creating a local branch and a connection between local-remote repositories; that connection becomes usefull for cases when you would like to automatically propagate your commited changes to the remote branch, without being asked for credentials confirmation.

In addition to the IDE and GIT, there is another local requirement : an web server configured with a domain available for application deployment. The web server should be set as deployment environment for the java project retrieved by GIT.

The application created in Bluemix™ has a service already implemented, but the scope of our application is to prove that a simple recipe like a CRUD application with JSPs can be developed quickly and easy, without a conducting a deep investigation on how Bluemix™, cloud, PaaS work.

Step 2  - Prepare the data model and add sample data

For our application we have a database service available and binded in Bluemix™. Before changing application code, let's create some tables and add sample data for our application.

At the url https://github.com/ammbra/JSPSample  there is also an sql file (bluemix.sql) containing tables definitions, relationships between them and sample data to be added. In order to execute sql commands, go to Bluemix™ dashboard and select the database service.

Click on the Launch button and you should see this:

From this view select Work with Database Objects and then Run DDL :

Personal Observation : When you create your database service, you are being assigned as administrator of a schema (that belongs to your database) having a name similar to your Bluemix username. Use that schema inside your database sqls.

Step 3 - Connect to the database

After binding a SQL Database instance to the application, the following configuration is added to your VCAP_SERVICES environment variable. This information is available on your application dashboard from Bluemix  by clicking the title for your runtime or by clicking "Show Credentials" on the SQLDB title.

   "sqldb": [
         "name": "SQL Database-DB",
         "label": "sqldb",
         "plan": "sqldb_small",
         "credentials": {
            "port": 50000,
            "db": "I_774843",
            "username": "uv4567yt",
            "host": "",
            "hostname": "",
            "jdbcurl": "jdbc:db2://",
            "uri": "db2://uvbrndyt:8sly8hxvtqin@",
            "password": "8sly8hxv456in"

In java code, connect to the database (com.tutorial.crudwithjsp.util.DatabaseUtil) following similar implementation to what is presented below :

  1. define your local connection for local tests (instead of this, you can store your cloud database credentials and connect to it as you would do with an usual database server) :    
    private static String databaseHost = "localhost";
    private static int port = 50000;
    private static String databaseName = "mydb";
    private static String user = "myuser";
    private static String password = "mypass";
    private static String url = "myurl";
    2.  when deployed in cloud, replace the default values with VCAP_SERVICES values by calling a method similar to:       
private static boolean processVCAP() {
        // VCAP_SERVICES is a system environment variable
        // Parse it to obtain the for DB2 connection info
        String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
        System.out.println("VCAP_SERVICES content: " + VCAP_SERVICES);

        if (VCAP_SERVICES != null) {
            // parse the VCAP JSON structure
            BasicDBObject obj = (BasicDBObject) JSON.parse(VCAP_SERVICES);
            String thekey = null;
            Set<String> keys = obj.keySet();
            System.out.println("Searching through VCAP keys");
            // Look for the VCAP key that holds the SQLDB information
            for (String eachkey : keys) {
                System.out.println("Key is: " + eachkey);
                // Just in case the service name gets changed 
                // to lower case in the future, use toUpperCase
                if (eachkey.toUpperCase().contains("SQLDB")) {
                    thekey = eachkey;
            if (thekey == null) {
                System.out.println("Cannot find any SQLDB service in VCAP; exit");
                return false;
            BasicDBList list = (BasicDBList) obj.get(thekey);
            obj = (BasicDBObject) list.get("0");
            System.out.println("Service found: " + obj.get("name"));
            // parse all the credentials from the vcap env variable
            obj = (BasicDBObject) obj.get("credentials");
            databaseHost = (String) obj.get("host");
            databaseName = (String) obj.get("db");
            port = Integer.parseInt(obj.get("port").toString());
            user = (String) obj.get("username");
            password = (String) obj.get("password");
            url = (String) obj.get("jdbcurl");
        } else {
            System.out.println("VCAP_SERVICES is null");
            return false;

        return true;
    3. interact with the database through a Datasource object
DB2SimpleDataSource dataSource = new DB2SimpleDataSource();
dataSource.setPassword (password);
Step 4 - Develop business logic

This step consists of creating data objects based on  database tables :

  • com.tutorial.crudwithjsp.model.Department
  • com.tutorial.crudwithjsp.model.Employee

After mapping the data objects to the database tables,  we can procede to data access layer creation. EmployeeDAO and DepartmentDAO manipulate the database transactions for each of the data objects; these DAOs contain a database connection in their constructor and have methods for inserting, updating, deleting and selecting from each of the tables.

Step 5 - Develop Controller(s)

Application servlets  are needed because they define application behavior and select view for response; also they  maps user actions to model updates. In each servlet should be created skeleton code in to handle client requests (com.tutorial.crudwithjsp.controller.DepartmentController extends a HttpServlet and com.tutorial.crudwithjsp.controller.EmployeeController extends a HttpServlet). Finally, create mappings for views in deployment descriptor.

Step 6 - Organize the application front-end

 Views render the model(s) and request updates from model(s); they send user's requests to the controller and allow it to select which of the views will be displayed. The final stage of the java development process consists of :

  • placing JSP pages in the application's WEB-CONTENT directory
  • customizing their look through css files and images stored separately.
  • optional, add language support.

Lesson 3 - Continuous Deliver&Build&Deploy using IBM DevOps services

In the previous lessons we discovered IBM Bluemix™ platform, created an application and a database service available to users through it; also the linkage between our local code and code pulled by the platform is made through a remote GIT repository.  But there are another two "magical" steps before the application entering its running phase: build and deploy.

Build and deploy are being maintained through the IBM DevOps services,  but in addition to those two  DevOps Services provides another  capabilities that are useful for application development:

  • Hosting for public projects
  • Edit code delivered to the project(s)
  • Multiple source code repositories
  • Support for teams doing Agile development, etc

Through DevOps services the developer  has only one duty: to deliver correct code, without  treating environmental changes inside it. The services  have the ability  to install, configure or maintain what was delivered.  These services become available with the first commit made through the chosen versioning system (GIT or Jazz for the moment) and will accompany every code delivery made through the branch/stream. 

 Personal observations:
  1.  Be carefull when adding/configuring your project libraries and make your build.xml  aware of your project changes also, before delivering your code. If you make changes that will work locally (like setting a .jar library in your classpath dependent on your local file system) and deliver those changes, you will end up with broken builds.
  1. Do not forget to add binded services to the manifest.yml  file if your application uses them! Remember: the cloud exposes the services and you are using them through the VCAP_SERVICES variables, no matter their value or provider!  If you hardcode the values of some services, the application will work, but it will mean more effort invested in coding. You want to ease your developer life, not to complicate it!
  1. If your build has not been successful, do not discourage yourself! There are logs available when you click the build number inside the DevOps site (in advanced mode of build and deploy there is always displayed the last build number; click on it and you will see your build history) 
  1. If your deployment was unsuccessful, there are also logs available inside DevOps site.
  1. Your build was successful, also your application deployment went well, the application is running but when you access it you are seeing an exception? "Server logs" are available on Bluemix, when you click you  Application Name > Files and Logs> Logs


The cloud platform-java development experience represents the 21st Century way of coding and through these lessons anyone can decide how easy/hard is to adapt to it. If the interaction with the cloud platform and coding practices seemed easy, then develop your knowledge through coding your own cloud experience! If the interaction with the cloud platform, coding, tracking and deploying seemed difficult, then overcome the obstacles by gaining more knowledge about them and apply what you learned!

Perhaps the platform is not fully prepared for every kind of development approach, maybe our skills as programmers are not upgraded to current coding trends or we lack the experience given by years of hands on coding, but  do not forget that computers are made by humans, so both of the entities  are allowed to mistake. And since both make mistakes, both can improve themselves:  computers are controlled through code that can continuously be tracked, planed, built and deployed by us the humans that can continuously learn, think, act and collaborate.


Linkerd, the open source service mesh for cloud native applications. Get the complete guide to using Linkerd and Kubernetes to build scalable, resilient applications.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}