Web app using Java & Cloudant NoSQLDatabase in IBM Bluemix

IBM® Bluemix™ is the Platform as a Service(PaaS) cloud offering from IBM®. It enables organizations and developers to quickly and easily create, deploy, and manage applications on the cloud. Bluemix delivers enterprise-level services that can easily integrate with your cloud applications without you needing to know how to install or configure them.

IBM® Cloudant® NoSQL Database for Bluemix® is a NoSQL database as a service (DBaaS). It’s built from the ground up to scale globally, run non-stop, and handle a wide variety of data types like JSON, full-text, and geospatial. Cloudant NoSQL DB is an operational data store optimized to handle concurrent reads and writes, and provide high availability and data durability.

This tutorial will help you to build and deploy a java application that inserts registration details to Cloudant NoSQLDB

Prerequisites

Let’s start building the java application,before that lets go to our Bluemix Catalog and select required services to build it!

Step 1: Goto Catalog and then click on Java Runtime

blog_2

Step 2: Enter your application name. Eg: samplecloudant as shown in the image below. Click on CREATE

Step 3: Application has been created and it can be viewed on the Bluemix DASHBOARD. Click on the application

Step 4: Click on ADD SERVICE in the application(samplecloudant) DASHBOARD

Step 5: In CATALOG goto Data and Analytics and click on Cloudant NoSQL DB

Step 6: Change the Service name of Cloudant NoSQL DB to myCloudant and click on CREATE.

The service is now up and running in Bluemix! We can now proceed to Eclipse IDE to build the registration application that inserts details into Cloudant NoSQL Database.

We will be creating this web application using a Maven project in Eclipse(it is the easiest way to retrieve the dependencies we need — the ektorp client jars).

Step 7: Open Eclipse and create a new Maven project

Set the following values

  • Group ID: com.register
  • Artifact ID: registerblue
  • Version: 1
  • Packaging: war
  • Leave everything else as-is and Click Finish

 

 

Copy dependencies into pom.xml inside <project>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>1.4.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

Create a servlet

  • Click on registerblue>src>main>java
  • Right click on register folder and then New > (Other) > Web > Servlet
  • Keep class name as RegisterServlet & package name as com and then click on Finish

In web.xml

<servlet>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>

In RegisterServlet.java ( Resource Injection)

@Resource(name = “couchdb/myCloudant”)
protected CouchDbInstance mydb ;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

response.setContentType(“text/html”);
response.sendRedirect(request.getContextPath() + “/success.jsp”);

String name=request.getParameter(“name”);
String emailid = request.getParameter(“emailid”);
String mobile = request.getParameter(“mobile”);
String company=request.getParameter(“company”);
String loc=request.getParameter(“loc”);

String dbname = “registerdb”;
try {
//creates a database with the specified name
CouchDbConnector dbconnector = mydb.createConnector(dbname, true);
//create a document to place into your new database
Map<String, Object> doc = new HashMap<String, Object>();
doc.put(“_id”, UUID.randomUUID().toString());
doc.put(“name”, name);
doc.put(“emailid”, emailid);
doc.put(“mobile”, mobile);
doc.put(“company”, company);
doc.put(“location”, loc);

dbconnector.create(doc);
} catch (Exception e) {
response.getWriter().println(e.getMessage());
}
}

Export the WAR file and push it into bluemix using Cloudfoundry command line interface.

Once the application has started,insert the details through registration page.

To view the inserted details,click on the Cloudant service that is bound to your application.

Click on LAUNCH, it will take you to database list,you can now find your database registerdb in the list.

Click on registerdb and then you can view your documents on the right side.

You can create a view in Design Documents section and then copy paste API URL(top right) to view the documents in the browserdetails

Courtesy:IBM Bluemix and Cloudant NoSQL DB!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s