PL I – ASSIGNMENT 7

ASSIGNMENT NO.7

Title:

Write a python/Java application using MongoDB to maintain the blog for composing the blog consists of text columns, images and videos. Also calculate the hit or users visited by drawing 2D graphs.

Requirements:

  1. Computer System with Windows/Linux/Open Source Operating System.
  2. JDK
  3. Eclipse / Netbeans (for J2EE)
  4. Web Server (Apache Tomcat /Glassfish)
  5. MongoDB Server and Client

Theory:

GridFS : GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document. By default GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.

When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You also can access information from arbitrary sections of files, which allows you to “skip” into the middle of a video or audio file.

GridFS is useful not only for storing files that exceed 16MB but also for storing any files for which you want access without having to load the entire file into memory.

Implement GridFS :To store and retrieve files using GridFS, use either of the following:

  • A MongoDB driver.
  • The mongofiles command-line tool in the mongo

GridFS Collections :GridFS stores files in two collections:

  • chunks stores the binary chunks.
  • files stores the file’s metadata.

GridFS places the collections in a common bucket by prefixing each with the bucket name. By default, GridFS uses two collections with names prefixed by fs bucket:

fs.files and fs.chunks

You can choose a different bucket name than fs, and create multiple buckets in a single database.

Each document in the chunks collection represents a distinct chunk of a file as represented in the GridFS store. Each chunk is identified by its unique ObjectId stored in its _id field.

The following is an example of the GridFS interface in Java. The example is for demonstration purposes only.

By default, the interface must support the default GridFS bucket, named fs, as in the following:

// returns default GridFS bucket (i.e. “fs” collection)

GridFS myFS = new GridFS(myDatabase);

 

// saves the file to “fs” GridFS bucket

myFS.createFile(new File(“/tmp/largething.mpg”));

Optionally, interfaces may support other additional GridFS buckets as in the following example:

// returns GridFS bucket named “contracts”

GridFS myContracts = new GridFS(myDatabase, “contracts”);

 

// retrieve GridFS object “smithco”

GridFSDBFile file = myContracts.findOne(“smithco”);

 

// saves the GridFS file to the file system

file.writeTo(new File(“/tmp/smithco.pdf”));

JavaServer Pages(JSP): JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.

Advantages of JSP:

Following is the list of other advantages of using JSP over other technologies:

  • vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
  • vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.
  • vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for “real” programs that use form data, make database connections, and the like.
  • vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.
  • Static HTML: Regular HTML, of course, cannot contain dynamic information

Web Server: The Apache Web server provides a full range of Web server features, including CGI, SSL, and virtual domains. Apache also supports plug-in modules for extensibility. Apache is free software, distributed by the Apache Software Foundation that promotes various free and open source advanced Web technologies. Web Server is required for processing JSP pages over server.

Sample Example for calculating hit counter in JSP:

This example shows how you can use JSP to count total number of hits on a particular page. If you want to count total number of hits of your website then you would have to include same code in all the JSP pages.

<%@ page import=”java.io.*,java.util.*” %>

<html><head>

<title>Applcation object in JSP</title>

</head><body>

<%    Integer hitsCount =       (Integer)application.getAttribute(“hitCounter”);

if( hitsCount ==null || hitsCount == 0 )

{       /* First visit */       out.println(“Welcome to my website!”);

hitsCount = 1;    }else{       /* return visit */

out.println(“Welcome back to my website!”);

hitsCount += 1;

}

application.setAttribute(“hitCounter”, hitsCount);%>

<center>

<p>Total number of visits: <%= hitsCount%></p>

</center>

</body></html> 

 

*********************PROGRAM **********************

AIM : Write an python/Java application using MongoDB to maintain the blog for composing the blog consists of text columns, images and videos also calculate the hit or users visited by drawing 2D graphs.

 //expt7.jsp

<%--
    Document   : index
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       
        <%@ page import="java.io.*"%>
        <%@ page import="java.net.*"%>
        <%@ page import="com.mongodb.*"%>
        <%@ page import="com.mongodb.gridfs.*"%>
        <%
           try
            {
                 int count=0;
                 MongoClient mongo = new MongoClient("localhost", 27017);
                 DB db = mongo.getDB("java1");
                DBCollection collection = db.getCollection("hit");
                DBCursor cursor = collection.find();
               
                 java.util.Date t1=new java.util.Date();
                 String today=t1.toString().substring(0,10);
                 BasicDBObject document;
                 if(cursor.count()==0)
                {
                   
                       count=1;
                       document = new BasicDBObject();
                       document.put("Date",today);
                       document.put("Count",count);
                       collection.insert(document);
                }
                else
                {
                   
                    String d="";
                 while(cursor.hasNext())
                 {
                  document = (BasicDBObject)cursor.next();
                  d=document.getString("Date");
                  String t=today.toString().substring(0,10);
                  String counter=document.getString("Count");
                  if(d.equals(t))
                    {
                                           
                       count=Integer.parseInt(counter);
                       collection.remove(document);
                        count++;
                       }
           
                    else
                          count++;
                     }
                    document = new BasicDBObject();
                    document.put("Date",today);
                    document.put("Count",count);
                    collection.insert(document);
            out.println("<h3>You are Today's visitior number "+count);
            }
            }
            catch(Exception e)
            {
                out.println("in Catch"+e);
            }
          
            %>
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *