0% found this document useful (0 votes)
20 views

WT Lab Vikas

Uploaded by

Jack Sikhare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

WT Lab Vikas

Uploaded by

Jack Sikhare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Department of Computer Science & Engineering

Subject: Web Technology Lab (BCS552)


Lab File

B.Tech. CSE 3rd Year ( Sem.- V)

Academic Session 2024-25

Submitted By,
Student Name: Vikas Singh
Roll no.: 2202220100191
Faculty Name: Ms.Tanmayee Tilekar

1|Page
Department of Computer Science & Engineering

2|Page
user4 having the passwords pwd1, pwd2,
pwd3 and pwd4 respectively. Write a servlet
for doing the following:

1. Create a Cookie and add these four user


id’s and passwords to this Cookie.
2. Read the user id and passwords entered in
the Login form and authenticate with the
values available in the cookies.
10. 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.

11. 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.
12. Design and implement a simple shopping cart
example with session tracking API.

1. Write HTML program for designing your institute website. Display departmental
information of your institute on the website.

3|Page
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>Institute Website</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<header>

<h1>Welcome to Our Institute</h1>

</header>

<nav>

<ul>

<li><a href="#department1">Department 1</a></li>

<li><a href="#department2">Department 2</a></li> <li><a

href="#department3">Department 3</a></li>

</ul>

</nav>

<main>

<section id="department1">

<h2>Department 1</h2>

<p>Information about Department 1.</p>

4|Page
</section>

<section id="department2">

<h2>Department 2</h2>

<p>Information about Department 2.</p>

</section>

<section id="department3">

<h2>Department 3</h2>

<p>Information about Department 3.</p>

</section>

</main>

<footer>

<p>&copy; 2023 Institute Name</p>


</footer>

</body>

</html>

Output:
5|Page

2. Write HTML program to design an entry form for student details/employee


information/faculty details.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Entry Form</title>
</head>
<body>

<h1>Entry Form</h1>
<form action="submit_form.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>

<label for="phone">Phone Number:</label>


<input type="tel" id="phone" name="phone" required><br>

<input type="submit" v alue="Submit">


</form>
</body>
</html>

Output:
6|Page

3. Develop a responsive website using CSS and HTML. Website may be for
tutorial/blogs/commercial website.
HTML:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>Responsive Website</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<header>

<h1>My Blog</h1>

</header>

<nav>

<ul>

<li><a href="#">Home</a></li>

<li><a href="#">About</a></li>

<li><a href="#">Contact</a></li>

</ul>

</nav>

<main>

<article>
<h2>Blog Post Title</h2>

<p>This is a sample blog post.</p>

7|Page
</article>

</main>

<footer>

<p>&copy; 2023 My Blog</p>

</footer>

</body>

</html>

CSS: body { font-family: Arial, sans-serif;

header, nav, main, footer

{ padding: 20px;

margin: 10px;

navul { list-style-type:

none;

navul li { display:

inline; margin-right:

10px;

@media (max-width: 600px) {


navul li { display:

block; margin:

5px 0;

Output:

9|Page

4. Write programs using HTML and Java Script for validation of input data.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>Input Validation</title>

10 | P a g e
<script> functionvalidateForm() { const name = document.getElementById("name").value;

if (name === "") { alert("Name must be filled out"); return false;

return true;

</script>

</head>

<body>

<h1>Form Validation </h1>

<form onsubmit="return validateForm()" method="POST">

<label for="name">Name:</label>

<input type="text" id="name" name="name"><br>

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

</form>

</body>

</html> Output:
11 | P a g e

5. Write a program in XML for creation of DTD, which specifies set of rules. Create a style
sheet in CSS/ XSL & display the document in internet explorer.

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

CSS:
note {

12 | P a g e
font-family: Arial, sans-serif;
}

Output:
6. Create a Java Bean for Employee information (Employer ID, Name, Salary, Designation
and Department).

public class Employee { privateintempID; private String name; private double


salary; private String designation;
private String department;

// Getters and Setters publicintgetEmpID() {


returnempID;
}

public void setEmpID(intempID) { this.empID =


empID;
}

public String getName() { return


name;
}

public void setName(String name) {


this.name = name;
}

public double getSalary() { return


salary;
}

public void setSalary(double salary) { this.salary =


salary;
}

public String getDesignation() { return


designation;
}

public void setDesignation(String designation) { this.designation =


designation;
}

public String getDepartment() {


return department;
}

14 | P a g e
public void setDepartment(String department) { this.department =
department;
}
}

Output:

7. Build a command-line utility using Node.js that performs a specific task, such as
converting text to uppercase, calculating the factorial of a number, or generating random
passwords.

constreadline = require('readline');
constrl = readline.createInterface({ input:
process.stdin,
output: process.stdout
});

rl.question('Enter a number: ', (num) => {


const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1));
console.log(`Factorial of ${num} is ${factorial(num)}`);
rl.close();
});
Output:

15 | P a g e

16 | P a g e
8. 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.

db.users.aggregate([

$group: {

_id: "$city",

averageAge: { $avg: "$age" }

},

$sort: { averageAge: 1 }

}
]);

Output:

17 | P a g e
15 | P a g e

9. Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3
and pwd4 respectively. Write a servlet for doing the following:
1. Create a Cookie and add these four user id’s and passwords to this Cookie.
2. Read the user id and passwords entered in the Login form and authenticate with the
values available in the cookies.

importjavax.servlet.*; importjavax.servlet.http.*; import


java.io.*;

public class LoginServlet extends HttpServlet { protected


void doPost(HttpServletRequest request, response) throws
ServletException, IOException { Cookie[] cookies = HttpServletResponse
request.getCookies();
String userId = request.getParameter("userId"); String
password = request.getParameter("password"); boolean
authenticated = false;

for (Cookie cookie : cookies) {


if (cookie.getName().equals(userId) &&cookie.getValue().equals(password))
{ authenticated = true; break;
}
}

PrintWriter out = response.getWriter();


if (authenticated) {
out.println("Login successful!");
} else {
out.println("Invalid credentials.");
}
}
}

Output:
19 | P a g e
18 | P a g e

10. 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.

importjavax.servlet.*; importjavax.servlet.http.*; import


java.io.*;
importjava.sql.*;

public class UserServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException { try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user",
"password");
Statement stmt = con.createStatement();
ResultSetrs = stmt.executeQuery("SELECT * FROM users");

PrintWriter out = response.getWriter();


while (rs.next()) {
out.println("Name: " + rs.getString("name")); out.println("Email: " +
rs.getString("email"));
out.println("Phone: " + rs.getString("phone"));
}
con.close();
} catch (Exception e) { e.printStackTrace();
}
}}
Output:
11.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.

Login Form:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>User Login</h2>
<form action="login.jsp" method="post">
Email: <input type="email" name="email" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>

<% if (request.getMethod().equalsIgnoreCase("POST")) {
String email = request.getParameter("email");
String password = request.getParameter("password");
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user",
"password");
PreparedStatementps = con.prepareStatement("SELECT * FROM users WHERE
email=? AND password=?"); ps.setString(1, email); ps.setString(2, password);
ResultSetrs = ps.executeQuery();

if (rs.next()) {
out.println("Login successful! Welcome, " + rs.getString("name"));
} else {
out.println("Invalid email or password.");
}

21 | P a g e
con.close();
} catch (Exception e) { e.printStackTrace();
out.println("Login failed: " + e.getMessage());
}
}
%>
Register:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>User Registration</h2>
<form action="register.jsp" method="post">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Register">
</form>
</body>
</html>

<%
if (request.getMethod().equalsIgnoreCase("POST")) { String
name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user",
"password");
PreparedStatementps = con.prepareStatement("INSERT INTO users (name, email,
password) VALUES (?, ?, ?)"); ps.setString(1, name); ps.setString(2, email);
ps.setString(3, password); ps.executeUpdate(); con.close();
out.println("Registration successful!");
} catch (Exception e)
{ e.printStackTrace();

22 | P a g e
out.println("Registration failed: " + e.getMessage());
}
}
%>

Output:

12. Design and implement a simple shopping cart example with session tracking API.

23 | P a g e
Cart:
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Shopping Cart</title>
</head>
<body>
<h2>Shopping Cart</h2>
<%
List<String> cart = (List<String>) session.getAttribute("cart");
if (cart == null) {
cart = new ArrayList<>();
}

String item = request.getParameter("item"); if


(item != null) { cart.add(item);
session.setAttribute("cart", cart);
}

if (cart.isEmpty()) {
out.println("Your cart is empty.");
} else {
out.println("Items in your cart:<br>"); for
(String cartItem : cart) {
out.println(cartItem + "<br>");
}
}
%>
<form action="cart.jsp" method="get">
<label for="item">Add Item:</label>
<input type="text" name="item" required>
<input type="submit" value="Add to Cart">
</form>
<a href="checkout.jsp">Checkout</a>
</body>
</html>

Checkout :

<%@ page import="java.util.*" %>

24 | P a g e
<%@ page contentType="text/html;charset=UTF- 8" language="java" %>

<html>

<head>

<title>Checkout</title>

</head>

<body>

<h2>Checkout</h2>

<%

List<String> cart = (List<String>) session.getAttribute("cart"); if (cart == null ||

cart.isEmpty()) { out.println("Your cart is empty. Please add items to your cart before

checking out.");

} else

{ out.println("Items in

your cart:<br>"); for

(String cartItem : cart)

{ out.println(cartItem +

"<br>");

out.println("<h3>Thank you for your purchase!</h3>"); session.removeAttribute("cart"); //

Clear the cart after checkout

%>

<a href="cart.jsp">Back to Cart</a>

</body>

</html>

25 | P a g e
Output:
25 | P a g e

You might also like