WT LAB FIE[1]
WT LAB FIE[1]
2
1. Write HTML program for designing your institute website. Display
departmental information of your institute on the website ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>KCNIT Website</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
header {
background-color: #2c3e50;
color: white;
padding: 15px 0;
text-align: center;
}
nav {
background-color: #34495e;
padding: 10px 0;
text-align: center;
}
nav a {
color: white;
text-decoration: none;
margin: 0 15px;
font-size: 18px;
}
nav a:hover {
text-decoration: underline;
}
.content {
padding: 20px;
max-width: 1000px;
margin: auto;
background: white;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.department {
margin: 20px 0;
}
.department h2 {
color: #2c3e50;
}
footer {
text-align: center;
3
padding: 10px 0;
background-color: #2c3e50;
color: white;
margin-top: 20px;
}
</style>
</head>
<body>
<header>
<h1>Welcome to XYZ Institute</h1>
</header>
<nav>
<a href="#">Home</a>
<a href="#departments">Departments</a>
<a href="#contact">Contact Us</a>
</nav>
<div class="content">
<section id="departments">
<h2>Our Departments</h2>
<div class="department">
<h3>Department of Computer Science</h3>
<p>The Computer Science department offers courses in programming, data science,
artificial intelligence, and more. Our faculty is dedicated to preparing students for careers in the
tech industry.</p>
</div>
<div class="department">
<h3>Department of Electronics</h3>
<p>The Electronics department provides hands-on experience in circuits, communication
systems, and IoT technology, helping students to innovate in modern electronic solutions.</p>
</div>
<div class="department">
<h3>Department of Mechanical Engineering</h3>
<p>Our Mechanical Engineering department focuses on thermodynamics, robotics, and
manufacturing processes, shaping engineers for diverse industries.</p>
</div>
<div class="department">
<h3>Department of Civil Engineering</h3>
<p>Students in the Civil Engineering department learn about sustainable construction,
structural design, and urban planning, contributing to modern infrastructure development.</p>
</div>
</section>
</div>
<footer>
<p>© 2024 KCNIT College. All rights reserved.</p>
</footer>
</body>
</html>
4
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>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
.container {
max-width: 600px;
margin: 50px auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #2c3e50;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-top: 15px;
font-weight: bold;
}
button {
margin-top: 20px;
padding: 10px 15px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
5
button:hover {
background-color: #34495e;
}
</style>
</head>
<body>
<div class="container">
<h2>Entry Form</h2>
<form action="#" method="POST">
<!-- Personal Information -->
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" required>
</body>
</html>
6
3. 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>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
}
.container {
max-width: 500px;
margin: 50px auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #2c3e50;
}
label {
font-weight: bold;
display: block;
margin-top: 15px;
}
input {
width: 100%;
padding: 10px;
margin-top: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
.error {
color: red;
font-size: 14px;
margin-top: 5px;
}
button {
margin-top: 20px;
padding: 10px;
7
width: 100%;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #34495e;
}
</style>
</head>
<body>
<div class="container">
<h2>Input Validation Form</h2>
<form id="validationForm" onsubmit="return validateForm()">
<!-- Full Name -->
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name">
<span class="error" id="nameError"></span>
<script>
function validateForm() {
let isValid = true;
return isValid;
}
</script>
</body>
</html>
9
4. 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 ?
1. Save the XML file and the XSL file in the same directory.
2. Open the XML file in Internet Explorer; the browser will apply the XSL stylesheet and
render the content.
10
<head>
<title>Institute Information</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
padding: 20px;
}
h1, h2 {
color: #2c3e50;
}
.department {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Institute Details</h1>
<xsl:for-each select="institute/department">
<div class="department">
<h2><xsl:value-of select="name"/></h2>
<p><strong>Head:</strong> <xsl:value-of select="head"/></p>
<p><strong>Courses:</strong></p>
<ul>
<xsl:for-each select="courses/course">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Link the Stylesheet to the XML File
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
11
5. Create a Java Bean for Employee information (EmpID, Name, Salary,
Designation and Department) ?
package com.example;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int empID;
private String name;
private double salary;
private String designation;
private String department;
// Default constructor
public Employee() {
}
// Parameterized constructor
public Employee(int empID, String name, double salary, String designation,
String department) {
this.empID = empID;
this.name = name;
this.salary = salary;
this.designation = designation;
this.department = department;
}
13
6. 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 ?
package com.example;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int empID;
private String name;
private double salary;
private String designation;
private String department;
// Default constructor
public Employee() {
}
// Parameterized constructor
public Employee(int empID, String name, double salary, String designation, String
department) {
this.empID = empID;
this.name = name;
this.salary = salary;
this.designation = designation;
this.department = department;
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function showMenu() {
console.log("\nChoose a task:");
console.log("1. Convert text to uppercase");
console.log("2. Calculate factorial of a number");
console.log("3. Generate random password");
console.log("4. Exit");
rl.question("Enter your choice: ", handleMenu);
}
function handleMenu(choice) {
switch (choice) {
case '1':
rl.question("Enter text: ", text => {
15
console.log("Uppercase: ", text.toUpperCase());
showMenu();
});
break;
case '2':
rl.question("Enter a number: ", num => {
const n = parseInt(num, 10);
if (isNaN(n) || n < 0) {
console.log("Invalid input! Please enter a non-negative integer.");
} else {
console.log(`Factorial of ${n}: `, factorial(n));
}
showMenu();
});
break;
case '3':
rl.question("Enter password length: ", len => {
const length = parseInt(len, 10);
if (isNaN(length) || length <= 0) {
console.log("Invalid input! Please enter a positive integer.");
} else {
console.log("Generated Password: ", generatePassword(length));
}
showMenu();
});
break;
case '4':
console.log("Exiting the utility. Goodbye!");
rl.close();
break;
default:
console.log("Invalid choice. Please try again.");
showMenu();
}
}
function factorial(num) {
if (num === 0 || num === 1) return 1;
return num * factorial(num - 1);
}
function generatePassword(length) {
return crypto.randomBytes(length).toString('base64').slice(0, length);
}
16
7. 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?
// Import MongoDB client
const { MongoClient } = require('mongodb');
try {
// Connect to the MongoDB server
await client.connect();
console.log("Connected to MongoDB");
const db = client.db(dbName);
const collection = db.collection(collectionName);
// Aggregation pipeline
const pipeline = [
{
$group: {
_id: "$city", // Group by city
averageAge: { $avg: "$age" }, // Calculate average age
totalUsers: { $sum: 1 } // Count total users in each city
}
},
{
$sort: { averageAge: -1 } // Sort by average age in descending order
},
{
$match: { totalUsers: { $gte: 5 } } // Filter cities with at least 5 users
}
];
} catch (error) {
console.error("Error running aggregation:", error);
} finally {
// Close the connection
await client.close();
}
}
17
8. 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 ?
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("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// Create cookies for user credentials
Cookie user1 = new Cookie("user1", "pwd1");
Cookie user2 = new Cookie("user2", "pwd2");
Cookie user3 = new Cookie("user3", "pwd3");
Cookie user4 = new Cookie("user4", "pwd4");
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// Get user ID and password from the login form
String userId = request.getParameter("userId");
String password = request.getParameter("password");
// Respond to user
response.setContentType("text/html");
if (isAuthenticated) {
response.getWriter().println("<h1>Welcome, " + userId + "!</h1>");
} else {
response.getWriter().println("<h1>Invalid credentials. Please try
again.</h1>");
}
}
}
HTML
19
9. 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 ?
try {
Class.forName("com.mysql.cj.jdbc.Driver"); // Load MySQL driver
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/website", "root",
"password");
String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
20
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
out.println("<h1>Registration successful!</h1>");
} else {
out.println("<h1>Registration failed. Please try again.</h1>");
}
} catch (Exception e) {
e.printStackTrace(out);
} finally {
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {}
if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
}
%>
</body>
</html>
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/website", "root",
"password");
if (rs.next()) {
out.println("<h1>Welcome, " + username + "!</h1>");
} else {
out.println("<h1>Invalid credentials. Please try again.</h1>");
}
} catch (Exception e) {
e.printStackTrace(out);
} finally {
if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {}
if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
}
%>
</body>
</html>
MySQL Table
CREATE DATABASE website;
USE website;
22
10. Design and implement a simple shopping cart example with session
tracking API ?
<h2>Your Cart</h2>
<form action="Checkout.jsp" method="POST">
<ul>
<%
HttpSession session = request.getSession();
List<String> cart = (List<String>) session.getAttribute("cart");
if (cart != null && !cart.isEmpty()) {
for (String item : cart) {
out.println("<li>" + item + "</li>");
}
} else {
out.println("<p>Your cart is empty.</p>");
}
%>
</ul>
<button type="submit">Proceed to Checkout</button>
</form>
</body>
</html>
if (cart == null) {
cart = new ArrayList<>();
session.setAttribute("cart", cart);
}
if (product != null) {
cart.add(product);
}
%>
<h1>Product added to cart: <%= product %></h1>
<a href="ShoppingCart.jsp">Back to Shopping Cart</a>
</body>
</html>
24