{"id":56603,"date":"2018-04-02T15:00:15","date_gmt":"2018-04-02T12:00:15","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=56603"},"modified":"2018-04-18T15:24:52","modified_gmt":"2018-04-18T12:24:52","slug":"mongodb-and-jsp-servlet-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/","title":{"rendered":"MongoDB and JSP\/Servlet Example"},"content":{"rendered":"<p>Hello readers, in this tutorial, we will learn how to connect the Servlet with the Mongo database. For this tutorial, we\u2019ll have a login form to validate the user\u2019s login credentials in the Mongo database.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#servletFramework\">1.1 What is Servlet?<\/a><\/dt>\n<dt><a href=\"#jspFramework\">1.2 What is JSP?<\/a><\/dt>\n<dt><a href=\"#mongodbFramework\">1.3 What is MongoDB?<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#mongoDbAndJspServlet\">2. MongoDB and JSP\/Servlet Example<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#toolsUsed\">2.1 Tools Used<\/a><\/dt>\n<dt><a href=\"#projectStructure\">2.2 Project Structure<\/a><\/dt>\n<dt><a href=\"#projectCreation\">2.3 Project Creation<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#applicationBuilding\">3. Application Building<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#dbAndTableCreation\">3.1 Database &amp; Table Creation<\/a><\/dt>\n<dt><a href=\"#mavenDependencies\">3.2 Maven Dependencies<\/a><\/dt>\n<dt><a href=\"#javaClassCreation\">3.3 Java Class Creation<\/a><\/dt>\n<dt><a href=\"#jspViews\">3.4 Creating JSP Views<\/a><\/dt>\n<dt><a href=\"#deploymentDescriptor\">3.5 Deployment Descriptor<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#runTheApplication\">4. Run the Application<\/a><\/dt>\n<dt><a href=\"#projectDemo\">5. Project Demo<\/a><\/dt>\n<dt><a href=\"#projectConclusion\">6. Conclusion<\/a><\/dt>\n<dt><a href=\"#projectDownload\">7. Download the Eclipse Project<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>If you have installed the MongoDB application (<em>version 3.6<\/em>) on Windows or Ubuntu operating system and you wish to learn the usage of Mongo database with JSP and Servlet then follow the below steps. It is very simple, but before moving further let&#8217;s take a look at the Servlet\u2019s, JSP, and the Mongo database.<\/p>\n<h3><a name=\"servletFramework\"><\/a>1.1 What is Servlet?<\/h3>\n<p><strong>Servlet<\/strong> is a Java program which exists and executes in the J2EE servers and is used to receive the <code>HTTP<\/code> protocol request, process it and send back the response to the client. Servlets make use of the Java standard extension classes in the packages <code>javax.servlet<\/code> and <code>javax.servlet.http<\/code>. Since Servlets are written in the highly portable Java language and follow a standard framework, they provide a means to create the sophisticated server extensions in a server and operating system in an independent way.<\/p>\n<p>Typical uses for <code>HTTP<\/code> Servlets include:<\/p>\n<ul>\n<li>Processing and\/or storing the data submitted by an <code>HTML<\/code> form<\/li>\n<li>Providing dynamic content i.e. returning the results of a database query to the client<\/li>\n<li>Managing state information on top of the stateless <code>HTTP<\/code> i.e. for an online shopping cart system which manages the shopping carts for many concurrent customers and maps every request to the right customer<\/li>\n<\/ul>\n<p>As Servlet technology uses the Java language, thus web applications made using Servlet are <strong>Secured<\/strong>, <strong>Scalable<\/strong>, and <strong>Robust<\/strong>.<\/p>\n<h3><a name=\"jspFramework\"><\/a>1.2 What is JSP?<\/h3>\n<p><strong>Java Server Pages<\/strong> is a technology used to develop the web-pages by inserting the Java code in the <code>HTML<\/code> pages. The JSP pages are easier to maintain because developers can separate the designing and the development phase. The following are the advantages of using JSP over Servlet:<\/p>\n<ul>\n<li>JSP provides an extension to the Servlet technology<\/li>\n<li>JSP can easily separate the business logic from the presentation logic<\/li>\n<li>JSP provide Action tags, JSTL tags, Custom tags, and the Implicit objects that helps a developer write less code than Servlet<\/li>\n<\/ul>\n<h3><a name=\"mongodbFramework\"><\/a>1.3 What is MongoDB?<\/h3>\n<ul>\n<li>MongoDB is a high-performance <em>NoSQL database<\/em> where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (<a href=\"https:\/\/en.wikipedia.org\/wiki\/BSON\" target=\"_blank\" rel=\"noopener\">BSN<\/a>))<\/li>\n<li>The documents in MongoDB doesn\u2019t need to have a schema defined beforehand. Instead, the fields (i.e. <em>records<\/em>) can be created on the go<\/li>\n<li>Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily<\/li>\n<li>This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability<\/li>\n<\/ul>\n<h4>1.3.1 Why MongoDB?<\/h4>\n<ul>\n<li>As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility<\/li>\n<li>This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents<\/li>\n<li>MongoDB offers <em>indexes<\/em> to improve the <em>search performance<\/em> within the NoSQL database<\/li>\n<li>To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences<\/li>\n<li><strong>Replication<\/strong>: MongoDB can give high availability with the replica sets<\/li>\n<\/ul>\n<p>Now, open up the Eclipse Ide, and we will create a simple application using the Servlet and JSP with the Mongo database.<\/p>\n<h2><a name=\"mongoDbAndJspServlet\"><\/a>2. MongoDB and JSP\/Servlet Example<\/h2>\n<p>Here is a step-by-step guide for implementing this tutorial.<\/p>\n<h3><a name=\"toolsUsed\"><\/a>2.1 Tools Used<\/h3>\n<p>We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.<\/p>\n<h3><a name=\"projectStructure\"><\/a>2.2 Project Structure<\/h3>\n<p>Firstly, let\u2019s review the final project structure if you are confused about where you should create the corresponding files or folder later!<\/p>\n<p><figure id=\"attachment_56604\" aria-describedby=\"caption-attachment-56604\" style=\"width: 380px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_structure_guide_1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-56604\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_structure_guide_1.jpg\" alt=\"Fig. 1: Application Project Structure\" width=\"380\" height=\"486\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_structure_guide_1.jpg 380w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_structure_guide_1-235x300.jpg 235w\" sizes=\"(max-width: 380px) 100vw, 380px\" \/><\/a><figcaption id=\"caption-attachment-56604\" class=\"wp-caption-text\">Fig. 1: Application Project Structure<\/figcaption><\/figure><\/p>\n<h3><a name=\"projectCreation\"><\/a>2.3 Project Creation<\/h3>\n<p>This section will show how to create a Java-based Maven project with Eclipse. In Eclipse Ide, go to <code>File -&gt; New -&gt; Maven Project<\/code>.<\/p>\n<p><figure id=\"attachment_56650\" aria-describedby=\"caption-attachment-56650\" style=\"width: 652px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-1_1.jpg\"><img decoding=\"async\" class=\"wp-image-56650 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-1_1.jpg\" alt=\"Fig. 2: Create Maven Project\" width=\"652\" height=\"605\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-1_1.jpg 652w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-1_1-300x278.jpg 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/a><figcaption id=\"caption-attachment-56650\" class=\"wp-caption-text\">Fig. 2: Create Maven Project<\/figcaption><\/figure><\/p>\n<p>In the New Maven Project window, it will ask you to select project location. By default, &#8216;Use default workspace location&#8217; will be selected. Just click on next button to proceed.<\/p>\n<p><figure id=\"attachment_56651\" aria-describedby=\"caption-attachment-56651\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-2_1.jpg\"><img decoding=\"async\" class=\"wp-image-56651 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-2_1.jpg\" alt=\"Fig. 3: Project Details\" width=\"599\" height=\"545\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-2_1.jpg 599w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-2_1-300x273.jpg 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><figcaption id=\"caption-attachment-56651\" class=\"wp-caption-text\">Fig. 3: Project Details<\/figcaption><\/figure><\/p>\n<p>Select the &#8216;Maven Web App&#8217; Archetype from the list of options and click next.<\/p>\n<p><figure id=\"attachment_56652\" aria-describedby=\"caption-attachment-56652\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-3_1.jpg\"><img decoding=\"async\" class=\"wp-image-56652 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-3_1.jpg\" alt=\"Fig. 4: Archetype Selection\" width=\"598\" height=\"541\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-3_1.jpg 598w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-3_1-300x271.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-56652\" class=\"wp-caption-text\">Fig. 4: Archetype Selection<\/figcaption><\/figure><\/p>\n<p>It will ask you to &#8216;Enter the group and the artifact id for the project&#8217;. We will enter the details as shown in the below image. The version number will be by default: <code>0.0.1-SNAPSHOT<\/code>.<\/p>\n<p><figure id=\"attachment_56608\" aria-describedby=\"caption-attachment-56608\" style=\"width: 521px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4.jpg\"><img decoding=\"async\" class=\"size-full wp-image-56608\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4.jpg\" alt=\"Fig. 5: Archetype Parameters\" width=\"521\" height=\"526\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4.jpg 521w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4-297x300.jpg 297w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-project-guide-4-70x70.jpg 70w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/a><figcaption id=\"caption-attachment-56608\" class=\"wp-caption-text\">Fig. 5: Archetype Parameters<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Click on Finish and the creation of a maven project is completed. If you see, it has downloaded the maven dependencies and a <code>pom.xml<\/code> file will be created. It will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;MongoDbJspServlet&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;MongoDbJspServlet&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\t&lt;packaging&gt;war&lt;\/packaging&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>Let\u2019s start building the application!<\/p>\n<h2><a name=\"applicationBuilding\"><\/a>3. Application Building<\/h2>\n<p>Let\u2019s create an application to understand the basic building blocks of this tutorial.<\/p>\n<h3><a name=\"dbAndTableCreation\"><\/a>3.1 Database &amp; Table Creation<\/h3>\n<p>To begin with the implementation, we will need to create a sample database and collection. The below script creates a database called <code>emp_records<\/code> with a collection of <code>emp<\/code>. Open the Mongo terminal and execute the script.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Database &amp; Collection creation script<\/em><\/span><\/p>\n<pre class=\"brush:bash;wrap-lines:false;\">&gt; use emp_records\r\n\r\n&gt; db.emp.insertMany( [\r\n\t{ _id: 1, \"id\" : \"admin\", \"pwd\" : \"admin@123\" },\r\n\t{ _id: 2, \"id\" : \"jcg\", \"pwd\" : \"jcg$23!5\" },\r\n\t{ _id: 3, \"id\" : \"april\", \"pwd\" : \"faith!31_b\" },\r\n\t{ _id: 4, \"id\" : \"daniel\", \"pwd\" : \"tom#1234\" },\r\n\t{ _id: 5, \"id\" : \"john\", \"pwd\" : \"sun!23#day\" }\r\n] )\r\n\r\n&gt; db.emp.find()\r\n<\/pre>\n<p>The script gives the below output.<\/p>\n<p><figure id=\"attachment_56609\" aria-describedby=\"caption-attachment-56609\" style=\"width: 569px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-db-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-56609\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-db-guide-1.jpg\" alt=\"Fig. 6: Database &amp; Collection creation\" width=\"569\" height=\"254\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-db-guide-1.jpg 569w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv-db-guide-1-300x134.jpg 300w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/a><figcaption id=\"caption-attachment-56609\" class=\"wp-caption-text\">Fig. 6: Database &amp; Collection creation<\/figcaption><\/figure><\/p>\n<h3><a name=\"mavenDependencies\"><\/a>3.2 Maven Dependencies<\/h3>\n<p>Here, we specify the dependencies for the MongoDB and the Servlet API. The rest dependencies will be automatically resolved by the Maven framework and the <strong>updated<\/strong> file will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;MongoDbJspServlet&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;MongoDbJspServlet&lt;\/artifactId&gt;\r\n\t&lt;packaging&gt;war&lt;\/packaging&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\t&lt;name&gt;MongoDbJspServlet Maven Webapp&lt;\/name&gt;\r\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\r\n\t&lt;dependencies&gt;\r\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.mongodb\/mongo-java-driver --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.mongodb&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;mongo-java-driver&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;3.6.3&lt;\/version&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/javax.servlet\/javax.servlet-api --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;javax.servlet-api&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;4.0.0&lt;\/version&gt;\t\t\t\r\n\t\t&lt;\/dependency&gt;\r\n\t&lt;\/dependencies&gt;\r\n\t&lt;build&gt;\r\n\t\t&lt;finalName&gt;${project.artifactId}&lt;\/finalName&gt;\r\n\t&lt;\/build&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<h3><a name=\"javaClassCreation\"><\/a>3.3 Java Class Creation<\/h3>\n<p>Let\u2019s create the required Java files. Right-click on <code>src\/main\/java<\/code> folder, <code>New -&gt; Package<\/code>.<\/p>\n<p><figure id=\"attachment_56644\" aria-describedby=\"caption-attachment-56644\" style=\"width: 775px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-5.jpg\"><img decoding=\"async\" class=\"wp-image-56644 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-5.jpg\" alt=\"Fig. 7: Java Package Creation\" width=\"775\" height=\"224\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-5.jpg 775w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-5-300x87.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-5-768x222.jpg 768w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/a><figcaption id=\"caption-attachment-56644\" class=\"wp-caption-text\">Fig. 7: Java Package Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open where we will enter the package name as: <code>com.jcg.mongodb.servlet<\/code>.<\/p>\n<p><figure id=\"attachment_56645\" aria-describedby=\"caption-attachment-56645\" style=\"width: 517px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-6_1.jpg\"><img decoding=\"async\" class=\"wp-image-56645 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-6_1.jpg\" alt=\"Fig. 8: Java Package Name (com.jcg.mongodb.servlet)\" width=\"517\" height=\"424\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-6_1.jpg 517w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-6_1-300x246.jpg 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a><figcaption id=\"caption-attachment-56645\" class=\"wp-caption-text\">Fig. 8: Java Package Name (com.jcg.mongodb.servlet)<\/figcaption><\/figure><\/p>\n<p>Once the package is created in the application, we will need to create the Controller and the Database classes. Right-click on the newly created package: <code>New -&gt; Class<\/code>.<\/p>\n<p><figure id=\"attachment_56646\" aria-describedby=\"caption-attachment-56646\" style=\"width: 752px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-7_1.jpg\"><img decoding=\"async\" class=\"wp-image-56646 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-7_1.jpg\" alt=\"Fig. 9: Java Class Creation\" width=\"752\" height=\"106\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-7_1.jpg 752w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-7_1-300x42.jpg 300w\" sizes=\"(max-width: 752px) 100vw, 752px\" \/><\/a><figcaption id=\"caption-attachment-56646\" class=\"wp-caption-text\">Fig. 9: Java Class Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open and enter the file name as: <code>Login<\/code>. The servlet controller class will be created inside the package: <code>com.jcg.mongodb.servlet<\/code>.<\/p>\n<p><figure id=\"attachment_56647\" aria-describedby=\"caption-attachment-56647\" style=\"width: 529px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-8_1.jpg\"><img decoding=\"async\" class=\"wp-image-56647 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-8_1.jpg\" alt=\"Fig. 10: Java Class (Login.java)\" width=\"529\" height=\"604\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-8_1.jpg 529w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-8_1-263x300.jpg 263w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><\/a><figcaption id=\"caption-attachment-56647\" class=\"wp-caption-text\">Fig. 10: Java Class (Login.java)<\/figcaption><\/figure><\/p>\n<p>Repeat the step (i.e. Fig. 9) and enter the filename as: <code>Util<\/code>. The Dao class to read the cookies will be created inside the package: <code>com.jcg.mongodb.servlet<\/code>.<\/p>\n<p><figure id=\"attachment_56648\" aria-describedby=\"caption-attachment-56648\" style=\"width: 529px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-9_1.jpg\"><img decoding=\"async\" class=\"wp-image-56648 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-9_1.jpg\" alt=\"Fig. 11: Java Class (Util.java)\" width=\"529\" height=\"601\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-9_1.jpg 529w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/mongodb-jsp_serv-project-guide-9_1-264x300.jpg 264w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><\/a><figcaption id=\"caption-attachment-56648\" class=\"wp-caption-text\">Fig. 11: Java Class (Util.java)<\/figcaption><\/figure><\/p>\n<h4>3.3.1 Implementation of Controller Class<\/h4>\n<p>Here is an example which shows how to access the Mongo database using Servlet. This class will fetch the <code>login_id<\/code> and <code>login_pwd<\/code> parameters from the request and passes it to the <code>searchUserInDb()<\/code> method for the successful validation. Let\u2019s see the simple code snippet that follows this implementation.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Login.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.mongodb.servlet;\r\n\r\nimport java.io.IOException;\r\n\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\n@WebServlet(\"\/loginServlet\")\r\npublic class Login extends HttpServlet {\r\n\r\n\tprivate static final long serialVersionUID = 1L;\r\n\r\n\t\/\/ This method is called by the servlet container to process a 'post' request\r\n\tpublic void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {\r\n\t\thandleRequest(req, resp);\r\n\t}\r\n\r\n\tpublic void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {\r\n\r\n\t\t\/\/ Reading post parameters from the request\r\n\t\tString param1 = req.getParameter(\"login_id\"), \r\n\t\t\t\tparam2 = req.getParameter(\"login_pwd\");\r\n\r\n\t\t\/\/ Checking for null and empty values\r\n\t\tif(param1 == null || param2 == null || \"\".equals(param1) || \"\".equals(param2)) {\r\n\t\t\treq.setAttribute(\"error_message\", \"Please enter login id and password\");\r\n\t\t\treq.getRequestDispatcher(\"\/index.jsp\").forward(req, resp);\r\n\t\t} else {\r\n\t\t\tboolean isUserFound = Util.searchUserInDb(param1, param2);\r\n\t\t\tif(isUserFound) {\t\t\t\t\r\n\t\t\t\treq.getRequestDispatcher(\"\/welcome.jsp\").forward(req, resp);\r\n\t\t\t} else {\r\n\t\t\t\treq.setAttribute(\"error_message\", \"You are not an authorised user. Please check with administrator.\");\r\n\t\t\t\treq.getRequestDispatcher(\"\/index.jsp\").forward(req, resp);\r\n\t\t\t}\t\r\n\t\t}\t\t\r\n\t}\r\n}\r\n<\/pre>\n<h4>3.3.2 Implementation of Database Class<\/h4>\n<p>The following code snippet shows the database connectivity implementation and validates the inputted login credentials with the database. If the credentials are <em>successfully<\/em> validated, a <em>success<\/em> response is sent otherwise an <em>error<\/em>. Do <em>remember<\/em>, to change the Database URL and Port no. according to the settings in your environment.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Util.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.mongodb.servlet;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\nimport org.bson.Document;\r\n\r\nimport com.mongodb.BasicDBObject;\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.FindIterable;\r\nimport com.mongodb.client.MongoCollection;\r\nimport com.mongodb.client.MongoDatabase;\r\n\r\npublic class Util {\r\n\r\n\t\/\/ Method to make a connection to the mongodb server listening on a default port\r\n\tprivate static MongoClient getConnection() {\r\n\t\tint port_no = 27017;\r\n\t\tString url = \"localhost\";\r\n\r\n\t\tMongoClient mongoClntObj = new MongoClient(url, port_no);\r\n\t\treturn mongoClntObj;\r\n\t}\r\n\r\n\t\/\/ Method to search a user in the mongodb\r\n\tpublic static boolean searchUserInDb(String loginId, String loginPwd) {\r\n\t\tboolean user_found = false;\r\n\t\tString db_name = \"emp_records\",\r\n\t\t\t\tdb_collection_name = \"emp\";\r\n\r\n\t\t\/\/ Get the mongodb connection\r\n\t\tMongoDatabase db = getConnection().getDatabase(db_name);\r\n\r\n\t\t\/\/ Get the mongodb collection.\r\n\t\tMongoCollection col = db.getCollection(db_collection_name);\r\n\r\n\t\t\/\/ Get the particular record from the mongodb collection\t\t\r\n\t\tList obj = new ArrayList();\r\n\t\tobj.add(new BasicDBObject(\"id\", loginId));\r\n\t\tobj.add(new BasicDBObject(\"pwd\", loginPwd));\r\n\r\n\t\t\/\/ Form a where query\r\n\t\tBasicDBObject whereQuery = new BasicDBObject();\r\n\t\twhereQuery.put(\"$and\", obj);\r\n\t\tSystem.out.println(\"Sql query is?= \" + whereQuery.toString());\r\n\r\n\t\tFindIterable cursor = col.find(whereQuery);\r\n\t\tfor(Document doc : cursor) {\r\n\t\t\tSystem.out.println(\"Found?= \" + doc);\r\n\t\t\tuser_found = true;\r\n\t\t}\r\n\t\treturn user_found;\r\n\t}\r\n}\r\n<\/pre>\n<h3><a name=\"jspViews\"><\/a>3.4 Creating JSP Views<\/h3>\n<p>Servlet supports many types of views for different presentation technologies. These include \u2013 <code>JSP<\/code>, <code>HTML<\/code>, <code>XML<\/code> etc. So let us write a simple view in <code>MongoDbJspServlet\/src\/main\/webapp\/<\/code>. To make the form works with Java servlet, we need to specify the following attributes for the <code>&lt;form&gt;<\/code> tag:<\/p>\n<ul>\n<li><code>method=\"post\"<\/code>: To send the form data as an HTTP POST request to the server. Generally, form submission should be done in HTTP POST method<\/li>\n<li><code>action=\"Servlet Url \"<\/code>: Specifies the relative URL of the servlet which is responsible for handling the data posted from this form<\/li>\n<\/ul>\n<h4>3.4.1 Index Page<\/h4>\n<p>This page contains the form to input the login credentials and displays an error message if the credentials are not validated or the service is not responding. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.jsp<\/em><\/span><\/p>\n<pre class=\"brush:html; wrap-lines:false;\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;MongoDb Servlet Example&lt;\/title&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n\r\n    &lt;!-- jQuery Files --&gt;\r\n    &lt;script type=\"text\/javascript\" src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.3.1\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script type=\"text\/javascript\" src=\"resource\/js\/form_login.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- CSS File --&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"mongoDbServlet\" class=\"container\"&gt;\r\n        &lt;h1 align=\"center\" class=\"text-primary\"&gt;MongoDb Jsp and Servlet Example&lt;\/h1&gt;\r\n        &lt;hr \/&gt;\r\n\r\n        &lt;!------ MONGODB JSP &amp; SERVLET EXAMPLE ------&gt;\r\n        &lt;div id=\"login_form\"&gt;\r\n            &lt;form id=\"user_login_form\" name=\"loginForm\" method=\"post\" action=\"loginServlet\"&gt;\r\n                &lt;!----- LOGIN FORM ------&gt;\r\n                &lt;div class=\"input-group\"&gt;\r\n                    &lt;span class=\"input-group-addon\"&gt;&lt;i class=\"glyphicon glyphicon-user\"&gt;&lt;\/i&gt;&lt;\/span&gt;\r\n                    &lt;input type=\"text\" class=\"form-control\" id=\"login_id\" placeholder=\"Enter login id ...\" name=\"login_id\" maxlength=\"6\"&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div&gt;\u00a0&lt;\/div&gt;\r\n                &lt;div class=\"input-group\"&gt;\r\n                    &lt;span class=\"input-group-addon\"&gt;&lt;i class=\"glyphicon glyphicon-lock\"&gt;&lt;\/i&gt;&lt;\/span&gt;\r\n                    &lt;input type=\"password\" class=\"form-control\" id=\"login_pwd\" placeholder=\"Enter password ...\" name=\"login_pwd\"&gt;\r\n                &lt;\/div&gt;\r\n\r\n                &lt;!----- SUBMIT BUTTON ------&gt;\r\n                &lt;div&gt;\u00a0&lt;\/div&gt;\r\n                &lt;button id=\"submit_btn\" type=\"submit\" class=\"btn btn-primary\"&gt;Confirm identity&lt;\/button&gt;\r\n            &lt;\/form&gt;\r\n        &lt;\/div&gt;\r\n        \r\n        &lt;h4 id=\"errMsg\" class=\"text-danger\" align=\"center\"&gt;${error_message}&lt;\/h4&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h4>3.4.2 Success Page<\/h4>\n<p>This page displays a <em>success<\/em> message if the login credentials are successfully validated. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>welcome.jsp<\/em><\/span><\/p>\n<pre class=\"brush:html; wrap-lines:false;\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;MongoDb Servlet Example&lt;\/title&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n\r\n    &lt;!-- CSS File --&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"mongoDbServlet\" class=\"container\"&gt;\r\n        &lt;h2 align=\"center\" class=\"text-success\"&gt;Welcome user!&lt;\/h2&gt;       \r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3><a name=\"deploymentDescriptor\"><\/a>3.5 Deployment Descriptor<\/h3>\n<p>In <code>web.xml<\/code> file we have declared the servlet to receive all kind of the requests and specifies the default page (i.e. <code>index.jsp<\/code>). Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>web.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;web-app\r\n\txmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\"\r\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee        http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\"\r\n\tversion=\"3.0\"&gt;\r\n\t\r\n\t&lt;display-name&gt;MongoDb JSP and SERVLET Example&lt;\/display-name&gt;\r\n\t\r\n\t&lt;servlet&gt;\r\n      &lt;servlet-name&gt;Login&lt;\/servlet-name&gt;\r\n      &lt;servlet-class&gt;com.jcg.mongodb.servlet.Login&lt;\/servlet-class&gt;\r\n   &lt;\/servlet&gt;\r\n   &lt;servlet-mapping&gt;\r\n      &lt;servlet-name&gt;Login&lt;\/servlet-name&gt;\r\n      &lt;url-pattern&gt;\/Login&lt;\/url-pattern&gt;\r\n   &lt;\/servlet-mapping&gt;\r\n   \t\r\n\t&lt;!-- Welcome File List --&gt;\r\n\t&lt;welcome-file-list&gt;\r\n\t\t&lt;welcome-file&gt;index.jsp&lt;\/welcome-file&gt;\r\n\t&lt;\/welcome-file-list&gt;\r\n&lt;\/web-app&gt;\r\n<\/pre>\n<h2><a name=\"runTheApplication\"><\/a>4. Run the Application<\/h2>\n<p>As we are ready with all the changes, let us compile the project and deploy the application on the Tomcat7 server. To deploy the application on Tomat7, right-click on the project and navigate to <code>Run as -&gt; Run on Server<\/code>.<\/p>\n<p><figure id=\"attachment_56615\" aria-describedby=\"caption-attachment-56615\" style=\"width: 803px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_deploy_guide_1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-56615\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_deploy_guide_1.jpg\" alt=\"Fig. 12: How to Deploy Application on Tomcat\" width=\"803\" height=\"414\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_deploy_guide_1.jpg 803w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_deploy_guide_1-300x155.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_deploy_guide_1-768x396.jpg 768w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/a><figcaption id=\"caption-attachment-56615\" class=\"wp-caption-text\">Fig. 12: How to Deploy Application on Tomcat<\/figcaption><\/figure><\/p>\n<p>Tomcat will deploy the application in its web-apps folder and shall start its execution to deploy the project so that we can go ahead and test it in the browser.<\/p>\n<h2><a name=\"projectDemo\"><\/a>5. Project Demo<\/h2>\n<p>Open your favorite browser and hit the following URL. The output page will be displayed.<\/p>\n<p><code>http:\/\/localhost:8080\/MongoDbJspServlet\/<\/code><\/p>\n<p>Server name (localhost) and port (8080) may vary as per your tomcat configuration. Developers can debug the example and see what happens after every step. Enjoy!<\/p>\n<p><figure id=\"attachment_56617\" aria-describedby=\"caption-attachment-56617\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_1.jpg\"><img decoding=\"async\" class=\"wp-image-56617\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_1.jpg\" alt=\"Fig. 13: Application Index Page\" width=\"820\" height=\"203\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_1.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_1-300x74.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_1-768x190.jpg 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-56617\" class=\"wp-caption-text\">Fig. 13: Application Index Page<\/figcaption><\/figure><\/p>\n<p>Here, if the login credentials are successfully validated, the users will get a <em>success<\/em> message as shown in Fig. 14.<\/p>\n<p><figure id=\"attachment_56618\" aria-describedby=\"caption-attachment-56618\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_2.jpg\"><img decoding=\"async\" class=\"wp-image-56618\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_2.jpg\" alt=\"Fig. 14: Success message\" width=\"820\" height=\"103\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_2.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_2-300x38.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_2-768x97.jpg 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-56618\" class=\"wp-caption-text\">Fig. 14: Success message<\/figcaption><\/figure><\/p>\n<p>If the server URL is not responding, the user will get an <em>error<\/em> message as shown in Fig. 15.<\/p>\n<p><figure id=\"attachment_56619\" aria-describedby=\"caption-attachment-56619\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_3.jpg\"><img decoding=\"async\" class=\"wp-image-56619\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_3.jpg\" alt=\"Fig. 15: Error message\" width=\"820\" height=\"219\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_3.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_3-300x80.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-jsp_serv_project_demo_guide_3-768x205.jpg 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-56619\" class=\"wp-caption-text\">Fig. 15: Error message<\/figcaption><\/figure><\/p>\n<p>That\u2019s all for this post. Happy Learning!!<\/p>\n<h2><a name=\"projectConclusion\"><\/a>6. Conclusion<\/h2>\n<p>In this section, developers learned how to create a simple application using the Servlet and JSP with the Mongo database. Developers can download the sample application as an Eclipse project in the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>7. Download the Eclipse Project<\/h2>\n<p>This was an example of MongoDB and Servlet\/JSP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDbJspServlet.zip\" target=\"_blank\" rel=\"noopener\"><strong>MongoDbJspServlet<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers, in this tutorial, we will learn how to connect the Servlet with the Mongo database. For this tutorial, we\u2019ll have a login form to validate the user\u2019s login credentials in the Mongo database. Table Of Contents 1. Introduction 1.1 What is Servlet? 1.2 What is JSP? 1.3 What is MongoDB? 2. MongoDB and &hellip;<\/p>\n","protected":false},"author":119,"featured_media":36154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1447],"tags":[881,1030,1194,647,1052],"class_list":["post-56603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-database","tag-jsp","tag-mongodb","tag-mysql","tag-servlet"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MongoDB and JSP\/Servlet Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB and JSP\/Servlet Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-02T12:00:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-18T12:24:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"MongoDB and JSP\/Servlet Example\",\"datePublished\":\"2018-04-02T12:00:15+00:00\",\"dateModified\":\"2018-04-18T12:24:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\"},\"wordCount\":1677,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"keywords\":[\"database\",\"jsp\",\"MongoDb\",\"mysql\",\"servlet\"],\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\",\"name\":\"MongoDB and JSP\/Servlet Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"datePublished\":\"2018-04-02T12:00:15+00:00\",\"dateModified\":\"2018-04-18T12:24:52+00:00\",\"description\":\"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MongoDB\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"MongoDB and JSP\/Servlet Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB and JSP\/Servlet Example - Java Code Geeks","description":"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB and JSP\/Servlet Example - Java Code Geeks","og_description":"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.","og_url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-04-02T12:00:15+00:00","article_modified_time":"2018-04-18T12:24:52+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"MongoDB and JSP\/Servlet Example","datePublished":"2018-04-02T12:00:15+00:00","dateModified":"2018-04-18T12:24:52+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/"},"wordCount":1677,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","keywords":["database","jsp","MongoDb","mysql","servlet"],"articleSection":["MongoDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/","url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/","name":"MongoDB and JSP\/Servlet Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","datePublished":"2018-04-02T12:00:15+00:00","dateModified":"2018-04-18T12:24:52+00:00","description":"Hello readers, in this tutorial, we will learn how to connect the Servlet with the MongoDb.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-and-jsp-servlet-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/"},{"@type":"ListItem","position":3,"name":"MongoDB","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/"},{"@type":"ListItem","position":4,"name":"MongoDB and JSP\/Servlet Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/56603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=56603"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/56603\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/36154"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=56603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=56603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=56603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}