0% found this document useful (0 votes)
13 views11 pages

WT 8-11

Uploaded by

trivendra055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

WT 8-11

Uploaded by

trivendra055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PROGRAM: 10

Objective: Develop a script that uses MongoDB's aggregation framework to perform operations like
grouping, filtering, and sorting. For instance, aggregate user data to find the average age of
users in different cities.

THEORY: The MongoDB aggregation framework in Node.js performs data aggregation operations such
as filtering, grouping and transforming documents within a collection.
Steps to Implement MongoDB Aggregation Framework
Step 1: Create a node.js project and Install Required Modules
To utilize the MongoDB aggregation framework in a Node.js application, Create a node.js project and install
the mongodb package using:
npm init
npm install mongodb
Step 2: Create an index.js File and modify index.js file
Step 3: Update package.json
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"mongodb": "^6.8.0"
}
Step 4: Run the application
Insert some placeholder data into your MongoDB collection and run the application using ‘npm start’.

SOURCE CODE:

Steps to Implement MongoDB Aggregation Framework


Step 1: Create a node.js project and Install Required Modules
To utilize the MongoDB aggregation framework in a Node.js application, Create a node.js project and
install the mongodb package using:
npm init
npm install mongodb
Step 2: Create an index.js File and modify index.js file
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'mydatabase';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('documents');

const pipeline = [
{ $match: { age: { $gte: 18 } } },
{ $group: { _id: "$city", totalPopulation: { $sum: "$population" } } },
{ $sort: { totalPopulation: -1 } },
{ $limit: 5 }
];

const result = await collection.aggregate(pipeline).toArray();


console.log(result);
}
main();

Step 3: Update package.json


"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"mongodb": "^6.8.0"
}
Step 4: Run the application
Insert some placeholder data into your MongoDB collection and run the application using ‘npm
start’.

OUTPUT
Program 11

OBJECTIVE: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3
and pwd4 respectively. Write a servlet for doing the following. Create a Cookie and add these four user id’s
and passwords to this Cookie. 2.

THEORY: A cookie is a small piece of information that is persisted between the multiple client requests. A
cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a
maximum age, and a version number. By default, each request is considered as a new request. In cookies
technique, we add cookie with response from the Servlet. So cookie is stored in the cache of the browser.
After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user
as the old user.

SOURCE CODE:
Login.html:

<html>

<body bgcolor="pink">

<form action="show" method="get">

<center>User Name: <input type="test" name="name"><br>

Password:<input type="password" name="pass"><br>

<input type="submit" name="b1">

<input type="Button" name="b2" value="Reset">

</center>

</form>

</body>

</html>

Login.java:

import java.io.*;

importjavax.servlet.*;

importjavax.servlet.http.*;

/** Example using servlet initialization. Here, the message

* to print and the number of times the message should be

* repeated is taken from the init parameters.

*/

public class login extends HttpServlet

{
public void doPost(HttpServletRequest request, HttpServletResponse response)

throwsServletException, IOException

response.setContentType("text/html");

String na=request.getParameter("name");

String pa=request.getParameter("pass");

PrintWriter out = response.getWriter();

Cookie nam1=new Cookie("user1","pace");

Cookie nam2=new Cookie("user2","phani");

Cookie nam3=new Cookie("user3","cse");

Cookie nam4=new Cookie("user4","ece");

Cookie pas1=new Cookie("pwd1","college");

Cookie pas2=new Cookie("pwd2","kumar");

Cookie pas3=new Cookie("pwd3","it");

Cookie pas4=new Cookie("pwd4","eee");

int flag=0;

String nam[]={nam1.getValue(),nam2.getValue(),nam3.getValue(),nam4.getValue()};

String pas[]={pas1.getValue(),pas2.getValue(),pas3.getValue(),pas4.getValue()};

for(int i=0;i<4;i++)

if(nam[i].equals(na)&&pas[i].equals(pa))

flag=1;

if(flag==1)

out.println("<title>The ShowMessage Servlet</title>");

out.println("<BODY BGCOLOR=\"#FDF5E6\">\n" +"<H1 ALIGN=CENTER>

WELCOME <br/>TO</br> " +na.toUpperCase() + "</H1>");

out.println("</BODY></HTML>");
}

else

out.println("<title>The ShowMessage Servlet</title>");

out.println("<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>

User is invalid </H1>");

out.println("</BODY></HTML>");

Web.xml:

<web-app>

<servlet>

<servlet-name>log </servlet-name>

<servlet-class>login </servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>log </servlet-name>

<url-pattern> /show </url-pattern>

</servlet-mapping>

</web-app>

Create a directory:

Create a directory “cookies”, in that directory copy login.html file and create a directory a

”WEB-INF”. In that WEB-INF directory again create directory “classes” and copy web.xml file.

Compile the servlet:

Compile the servlet file then copy the class file of that servlet into the directory

C:\Program Files\Apache Software Foundation\Tomcat 6.0\cookies\WEB-INF\classes.

Open the server:

1. Start tomcat by giving the following command at the instll_dir>tomcat>bin

Catalina.bat run

2. At the I.E(web browser) give the url as http://localhost:8080/ cookies /login.html


OUTPUT:
Login.html

Login.java
PROGRAM: 12

Objective:- Create a table which should contain at least the following fields: name, password, email-id,
phone number Write Servlet/JSP to connect to that database and extract data from the tables and display
them. Insert the details of the users who register with the web site, whenever a new user clicks the submit
button in the registration page.

THEORY: Servlets are the Java programs that runs on the Java-enabled web server or application server. They
are used to handle the request obtained from the web server, process the request, produce the response, then
send response back to the web server. Properties of Servlets : Servlets work on the server-side. A cookie is a
small piece of information that is persisted between the multiple client requests. A cookie has a name, a single
value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version
number.

SourceCode: Index.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>Simple Cookie example</h1>

<form action="CookieController" method="post">

Username: <input type="text" name="uname"> <br>

Password: <input type="password" name="pass"> <br>

<input type="submit" value="Login">

</form>

</body>

</html>

Servlet CookieController to create cookie

package com.candidjava;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/CookieController")

public class CookieController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String un = request.getParameter("uname");

String pw = request.getParameter("pass");

Cookie ck = new Cookie("mycookie", un);

response.addCookie(ck);

response.sendRedirect("home.jsp");

Display list of Cookie in browser

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>Retrieving Cookie from browser</h1>

<br>

<%

Cookie[] cks=request.getCookies();

for(Cookie ck:cks)
{

String cn=ck.getName();

String cv=ck.getValue();

%>

Cookie name : <b><%=cn %> </b><br>

Cookie Value : <b><%=cv %> </b><br>

<%

%>

</body>

</html>

OUTPUT:
Program 13

OBJECTIVE: Write a JSP which insert the details of the 3 or 4 users who register with the web site by using
registration form. Authenticate the user when he submits the login form using the user name and password
from the database

THEORY: JSP technology is used to create web application just like Servlet technology. It can be thought
of as an extension to Servlet because it provides more functionality than servlet such as expression language,
JSTL, etc.

A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because
we can separate designing and development. It provides some additional features such as Expression
Language, Custom Tags, etc.

SOURCE CODE:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Employee Registration Form</title>
</head>
<body>
<div align="center">
<h1>Employee Register Form</h1>
<form action="<%= request.getContextPath() %>/register" method="post">
<table style="width: 80%">
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName" /></td>
</tr>
<tr>
<td>UserName</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Contact No</td>
<td><input type="text" name="contact" /></td>
</tr>
</table>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>

Create employeedetails.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Employee Details</title>

</head>

<body>

<h1>User successfully registered!</h1>

</body>

</html>

OUTPUT:

You might also like