MongoDB Java Servlet Web Application Example Tutorial

Welcome to MongoDB Web Application example. Earlier in MongoDB Java Example we learned how to use MongoDB java driver in standalone application. Today we are moving forward to integrate MongoDB in Java Servlet web application.

MongoDB Web Application

We will create a web application where we will be manage Person data and store it in the MongoDB database. We will be able to create, read, update and delete Person records from the user interface and corresponding operations will be performed against MongoDB database.

First step is to create a dynamic web application in Eclipse and then convert it to Maven project, so that our maven based web application skeleton code is ready. Below image shows the final project structure and different components of it.

MongoDB Web Application

Let’s look into each of the components one by one.

MongoDB Web Application Maven Dependencies

Our final pom.xml file looks like below.

Notice that we have MongoDB java driver dependency to connect to MongoDB server, JSTL and standard jars are required for using JSTL tags in the JSP pages.

Deployment Descriptor

Here is our deployment descriptor web.xml file details.

  • MongoDB server host and port details are configured as context parameters, rather than hardcoding them somewhere in the code.
  • We have single JSP page for view purposes, I have added it in the welcome file list to avoid empty page for web application home.

Model Bean or POJO Class

We have Person.java class as Model class, this bean will be saved as Mongo DBObject into database.

Person.java

Notice that it has id attribute, this will be generated by MongoDB and it won’t be editable by user. This will serve as the primary key for MongoDB object. Notice that MongoDB record primary key is stored with “_id” key and when we retrieve it, it’s returned as ObjectId instance. For loose coupling, I am using String but we can also use ObjectId type.

Since we are not using ObjectId for primary key, we need to convert it into the ObjectId and vice versa at several places.

Java Bean to MongoDB DBObject Converter

We have a helper class for converting Person object to MongoDB DBObject and vice versa.

PersonConverter.java

Conversion is very simple, just take a note of converting the id attribute to ObjectId and vice versa.

MongoDB DAO Implementation

We could have created a Person DAO interface and provided MongoDB implementation, but for simplicity we have simple MongoDB DAO implementation to expose different operations we can perform for Person object in MongoDB database.

MongoDBPersonDAO.java

MongoClient instance is required for any MongoDB operations, so I have created a constructor where we need to pass it.

Our MongoDB operations setup classes are ready, we can move now to integrate it with the web application.

MongoDB ServletContextListener

MongoClient is thread safe and internally manages it’s own connection pool. Best practice is to create an instance of it and reuse it. We should close it when the application is shut down, that makes ServletContextListener implementation best choice to initialize and destroy it.

MongoDBContextListener.java

Notice that I am using @WebListener annotation to configure it as listener class, your servlet container should support it or else you will have to use XML based configurations. I am using Apache Tomcat 7 that supports Servlet API annotations, so make sure you use compatible servlet container or change the code to use XML based configurations.

We are creating instance of MongoClient and adding it as context attribute, so that it will be accessible everywhere in the application.

Servlet Classes

We have three servlet classes for CRUD operations, they have some validation logic to make sure input data is valid. If everything is fine with request parameters, then it’s using MongoDB DAO implementation to perform database operations and forward the request to the JSP page after setting correct attributes.

AddPersonServlet.java

EditPersonServlet.java

DeletePersonServlet.java

Notice the use of @WebServlet annotation for configuring the URI pattern for each of the servlets. It will be used in JSP pages to send the request to correct servlet.

JSP View Page

Final piece of the project is the view page, i am using JSTL tags for rendering the response html page.

persons.jsp

Recommended Read: JSTL Tutorial, JSP EL and JSP implicit objects.

MongoDB Java Web Application Test

Our application is ready for a test drive, below screenshots show some of the response pages of different CRUD operations.

Home Page

MongoDB-Web-Application-Home

Create Person Page

MongoDB-Web-Application-Create

Read Person Page

MongoDB-Web-Application-Create

Update Person Page

MongoDB-Web-Application-Create

Delete Person Page

MongoDB-Web-Application-Create

You will also find below logs in the server log file.

Summary

This post was intended to provide a complete example of using MongoDB server as data storage, you learned how to use MongoDB java driver for CRUD operations and create a web application. Download the final project from below link and explore more.

By admin

Leave a Reply