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

wt record

The document outlines multiple programming tasks related to web development, including creating an online bookstore using HTML frames, styling with CSS, and JavaScript for DOM manipulation and form validation. It provides code snippets for HTML structure, CSS styles, and JavaScript functionalities, demonstrating the use of tables, frames, and form validations. Each program is dated and categorized under the Department of Computer Science and Engineering for the 2022-26 batch.
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)
7 views

wt record

The document outlines multiple programming tasks related to web development, including creating an online bookstore using HTML frames, styling with CSS, and JavaScript for DOM manipulation and form validation. It provides code snippets for HTML structure, CSS styles, and JavaScript functionalities, demonstrating the use of tables, frames, and form validations. Each program is dated and categorized under the Department of Computer Science and Engineering for the 2022-26 batch.
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/ 57

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :1 Date: 27-01-2025


Program Title: HTML Frames
HTML Frames: Frame Structure: Design a webpage for the online book store using HTML
frames. Include a navigation frame, a content frame for displaying book details, and
a footer frame for additional information.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Book Store</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>

<!-- Main Container for Content -->


<div class="container">
<!-- Top Navigation -->
<div class="navbar">
<iframe src="nav.html" name="nav" scrolling="no" noresize></iframe>
</div>

<!-- Main Content -->


<div class="content">
<iframe src="sidebar.html" name="sidebar" frameborder="0"></iframe>
<iframe src="register.html" name="content" scrolling="auto"></iframe>
</div>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 1


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<!-- Footer -->


<div class="footer">
<iframe src="footer.html" name="footer" scrolling="no" noresize></iframe>
</div>
</div>

</body>
</html>

Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 2


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :2 Date: 03-02-2025


Program Title: HTML & CSS
Task-1:
HTML tables: colspan, rowspan, colgroup, img and it’s css styling
.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Document</title>
</head>
<body>

<table class="table1">
<colgroup>
<col span="2" style="background-color: red;">
<col span="2">
<col span="1" style="visibility: collapse;">
<col span="2" style="background-color: blue;">
</colgroup>
<tr>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<th>SAT</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td>21</td>
</tr>
<tr>
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
<td>26</td>
<td>27</td>
<td>28</td>
</tr>
</table>
<table class="table2">
<tr>
<th colspan="2">Name</th>
</tr>
<tr>
<th rowspan="2">Phone</th>
<td>usdighdds</td>
</tr>
<tr>
<td>989</td>
</tr>
</table>
<a href="https://en.wikipedia.org/wiki/Politician"><img src="https://shorturl.at/MbG7Z"
alt="Hello guys."></a>
</body>
</html>
.css:
table{
border: 2px solid black;
width: 70%;
align-items: center;

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 5


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

border-collapse: collapse;
}
.table1{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 20px;
padding: 15px;
}
.table1 td,th{
border: 0.5px solid green;
text-align: center;
}
.table1 tr:nth-child(even) {
background-color: magenta;
}

.table2{
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-
serif;
font-size: 15px;
margin: 10px;
padding: 5px;
width: 40%;
}
.table2 td,th{
border: 1px solid turquoise;
}
a:hover{
background-color: hotpink;
}
a{

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 6


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

text-decoration: none;
padding: 8px;
margin: 20px;
border: 1px solid crimson;
align-self: center;
}
Output:

Task-2:
CSS Styling: Apply CSS styling to enhance the visual appeal of the frames and content.
Use appropriate styles for fonts, colors, and layout.
Code:
Nav.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigation</title>
<link rel="stylesheet" href="styles.css">
</head>
<body id="nav">
<nav>
<ul>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 7


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<li><a href="login.html" target="content">Login</a></li>


<li><a href="register.html" target="content">Register</a></li>
<li><a href="about.html" target="content">About Us</a></li>
</ul>
</nav>
</body>
</html>

Sidebar.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav>
<ul>
<li><a href="categories.html" target="content">categories</a></li>
<li><a href="bookdetails.html" target="content">books available</a></li>
</ul>
</nav>
</body>
</html>
Footer.html:
<!DOCTYPE html>
<html lang="en">
<head>
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 8
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Footer</title>
<link rel="stylesheet" href="styles.css">
</head>
<body id="footer">
<p>&copy; 2025 Online Book Store. All rights reserved.</p>
</body>
</html>

Bookdetails.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Store</title>
<link rel="stylesheet" href="styles.css">
<script defer src="scripts.js"></script>
</head>
<body>

<h1>Book Store</h1>

<h2>Available Books</h2>
<div id="book-list" class="books">
<!-- Books will be loaded dynamically here -->
</div>

<h2>Selected Book Details</h2>


WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 9
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<div id="book-details" style="width: 50%;">


<img id="book-image" src="" alt="Book Image" style="display: none; height: 40%;">
<p id="book-title">Click on a book to see details</p>
<p id="book-author"></p>
<p id="book-detail"></p>
<p id="price"></p>
<button id="add-to-cart" style="display: none;">Add to Cart</button>
</div>

<h2>Shopping Cart</h2>
<div id="cart">
<ul id="cart-items"></ul>
<p id="cart-total">Total: 0 items</p>
</div>

</body>
</html>

Styles.css:
/* Global Styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}

.container {
display: flex;
flex-direction: column;
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 10
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

height: 100vh;
}

/* Navbar Styles */
.navbar {
background-color: #333;
padding: 10px 0;
text-align: center;
}

.navbar iframe {
width: 100%;
border: none;
}

nav ul {
list-style: none;
padding: 0;
}

nav ul li {
display: inline-block;
margin-right: 15px;
margin-left: 2px;
}

nav ul li a {
color: white;
text-decoration: none;
font-weight: bold;
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 11
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

nav ul li a:hover {
text-decoration: underline;
}

/* Content Layout */
.content {
display: flex;
height: 100%;
margin: 10px;
}

.content iframe {
border: none;
}

iframe[name="sidebar"] {
width: 250px;
height: 90%;
margin-top: 10px;
border: 1px solid crimson;
}

iframe[name="content"] {
flex: 1;
height: 100%;
padding: 30px;

}
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 12
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

/* Sidebar Styles */
nav ul li a {
color: #333;
}

nav ul li a:hover {
color: #007bff;
}

/* Footer Styles */
.footer {
background-color: #333;
color: white;
text-align: center;
padding: 10px;
}

.footer p {
margin: 0;
}
p{
color: red;
font-size: 12px;
}
/* Books grid */
.books {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 13
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

}
.book {
padding: 15px;
border: 1px solid #ddd;
cursor: pointer;
text-align: center;
transition: transform 0.3s, box-shadow 0.3s;
}
.book:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* Book details */
#book-details {
margin-top: 20px;
padding: 20px;
border: 1px solid #ddd;
width: 50%;
}
#book-details p {
margin: 10px 0;
}

/* Add to cart button */


#add-to-cart {
background-color: #28a745;
color: white;
padding: 10px 20px;
border-radius: 5px;
}
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 14
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

#add-to-cart:hover {
background-color: #218838;
}
/* Cart section */
#cart-items {
list-style-type: none;
padding: 0;
}
#cart-items li {
padding: 10px;
border-bottom: 1px solid #ddd;
}
Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 15


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :3 Date: 10-02-2025


Program Title: JavaScript DOM and validations

DOM Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p{
font-size: 20px;
text-align: center;
}
h1{text-align: center;}
</style>
</head>
<body>
<h1>hi</h1>
<p>Hello</p>
<p id="d"></p>
<p id="c"></p>
<p id="b">old text</p>
<script>
document.getElementById('d').innerHTML="namaste";//inserting text
const element=document.getElementsByTagName('p');
document.getElementById('c').innerHTML="The text: "+element[0].innerHTML;//getting text
document.getElementById('b').innerHTML="new text";//changing text
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 16
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

let a=5,b=6;//variable scopes


document.writeln("value: ",(a+b));
document.writeln(z);
var z=10;
</script>
</body>
</html>
Output:

Validations:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
<script>
function isNotEmpty(value) {
return value.trim() !== "";
}

function isAllLetters(value) {
return /^[A-Za-z]+$/.test(value);
}

function isAllNumbers(value) {

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 17


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

return /^\d+$/.test(value);
}

function isFloatingNumber(value) {
return /^-?\d*\.?\d+$/.test(value);
}

function isAlphanumeric(value) {
return /^[A-Za-z0-9]+$/.test(value);
}

function isValidEmail(value) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
}

function isValidIPAddress(value) {
return /^(\d{1,3}\.){3}\d{1,3}$/.test(value);
}

function validateForm() {
let text = document.getElementById("text").value;
let number = document.getElementById("number").value;
let email = document.getElementById("email").value;
let ip = document.getElementById("ip").value;

alert(
`Not Empty: ${isNotEmpty(text)}\n` +
`All Letters: ${isAllLetters(text)}\n` +
`All Numbers: ${isAllNumbers(number)}\n` +
`Floating Number: ${isFloatingNumber(number)}\n` +
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 18
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

`Alphanumeric: ${isAlphanumeric(text)}\n` +
`Valid Email: ${isValidEmail(email)}\n` +
`Valid IP Address: ${isValidIPAddress(ip)}`
);
return false;
}
</script>
</head>
<body>
<form onsubmit="return validateForm()">
<label>Text: <input type="text" id="text"></label><br>
<label>Number: <input type="text" id="number"></label><br>
<label>Email: <input type="text" id="email"></label><br>
<label>IP Address: <input type="text" id="ip"></label><br>
<button type="submit">Validate</button>
</form>
</body>
</html>
Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 19


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :4 Date: 17-02-2025


Program Title: Javascript for the bookstore application
a) User Registration Form:
Create a registration form for users signing up on the online bookstore. Validate
the following:
Username: Should not be empty and must be at least 5 characters long.
Password: Should not be empty and must be at least 8 characters long.
Confirm Password: Should match the entered password.
b) Forgot Password Form:
Implement a form for users to reset their password. Validate the following:
Email Address: Should be a valid email format.
Provide feedback if the email address is not associated with any account.
c) Dynamically Content Loading: Use JavaScript to dynamically load book details into the
content frame when a user
clicks on a book in the navigation frame.
d) Shopping Cart Functionality: Implement a simple shopping cart using JavaScript. Allow
users to add books to their cart and view the cart's contents.
Code:
Login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="scripts.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 20


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<form id="loginForm">
<label for="loginUsername">Username</label>
<input type="text" id="loginUsername" name="username" placeholder="Username"><br>

<label for="loginPassword">Password</label>
<input type="password" id="loginPassword" name="password" placeholder="Password"><br>

<input type="button" value="Enter" onclick="validateLogin()">


<p id="loginErrorMsg" style="color: red;"></p>
</form>

</body>
</html>
Register.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="scripts.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form id="registerForm">
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Username">
<br>
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Password">
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 21
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<br>
<label for="confirmPassword">Confirm Password</label>
<input type="password" id="confirmPassword" name="confirmPassword" placeholder="Re-
enter Password">
<br>
<input type="button" value="Register" onclick="validateForm()">
<p id="errorMsg" style="color: red;"></p>
</form>

</body>
</html>
Styles.css:
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 350px;
text-align: center;
}

input,
label {
margin: 8px 0;
padding: 8px;
width: 100%;
box-sizing: border-box;
}

input[type="text"],

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 22


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

input[type="password"] {
border: 1px solid #ccc;
border-radius: 4px;
}

input[type="button"] {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
padding: 10px;
}

input[type="button"]:hover {
background-color: #45a049;
}

p{
color: red;
font-size: 12px;
}

Javascript:
function validateForm() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let confirmPassword = document.getElementById("confirmPassword").value;
let errorMsg = document.getElementById("errorMsg");

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 23


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

errorMsg.innerHTML = ""; // Clear previous errors

if (username.length < 5) {
errorMsg.innerHTML = "Username must be at least 5 characters long.";
return;
}

if (password.length < 8) {
errorMsg.innerHTML = "Password must be at least 8 characters long.";
return;
}

if (password !== confirmPassword) {


errorMsg.innerHTML = "Passwords do not match.";
return;
}

localStorage.setItem('username', username);
localStorage.setItem('password', password);

alert("Registration Successful!");
window.location.href="login.html";
}

function validateLogin() {
let username1 = document.getElementById("loginUsername").value;
let password1 = document.getElementById("loginPassword").value;
let errorMsg = document.getElementById("loginErrorMsg");
let username=localStorage.getItem('username');
let password=localStorage.getItem('password');
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 24
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

// console.log(username)
// console.log(password)

errorMsg.innerHTML = ""; // Clear previous errors

if (username1.trim() === "") {


errorMsg.innerHTML = "Username cannot be empty.";
return;
}

if (password1.length < 8) {
errorMsg.innerHTML = "Password must be at least 8 characters long.";
return;
}
if(username!==username1 || password!==password1){
errorMsg.innerHTML="wrong user";
return;
}

alert("Login Successful!");
window.location.href="bookdetails.html";
}

//dynamic book loading


const books = [
{
title: "The Alchemist",
author: "Paulo Coelho",

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 25


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

description: "A journey of self-discovery through the eyes of a shepherd who seeks treasure in
Egypt.",
price: 15.99,
image: "https://m.media-amazon.com/images/I/51Z0nLAfLmL.jpg"
},
{
title: "1984",
author: "George Orwell",
description: "A dystopian novel about a totalitarian regime that controls every aspect of life.",
price: 12.99,
image: "https://m.media-amazon.com/images/I/41IXbIHb3-L.jpg"
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
description: "A powerful novel about racial injustice and childhood in the Deep South.",
price: 14.49,
image: "https://m.media-amazon.com/images/I/81OthjkJBuL.jpg"
},
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
description: "A tragic love story set in the Roaring Twenties, filled with wealth and decadence.",
price: 10.99,
image: "https://m.media-amazon.com/images/I/71VJUD5hvxL.jpg"
},
{
title: "Moby Dick",
author: "Herman Melville",
description: "A thrilling tale of obsession and revenge between a sailor and a great white whale.",

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 26


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

price: 18.50,
image: "https://m.media-amazon.com/images/I/71A-9sHnY2L.jpg"
},
{
title: "Pride and Prejudice",
author: "Jane Austen",
description: "A timeless romance novel exploring love, class, and family in 19th-century
England.",
price: 9.99,
image: "https://m.media-amazon.com/images/I/91hhCXj9dbL.jpg"
},
{
title: "The Catcher in the Rye",
author: "J.D. Salinger",
description: "A story of teenage rebellion and self-identity told through the eyes of Holden
Caulfield.",
price: 13.25,
image: "https://m.media-amazon.com/images/I/81OthjkJBuL.jpg"
},
{
title: "The Hobbit",
author: "J.R.R. Tolkien",
description: "A fantasy adventure following Bilbo Baggins as he embarks on an epic quest.",
price: 16.75,
image: "https://m.media-amazon.com/images/I/91b0C2YNSrL.jpg"
}
];

const bookList = document.getElementById("book-list");


const bookTitle = document.getElementById("book-title");

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 27


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

const bookAuthor = document.getElementById("book-author");


const detail = document.getElementById("book-detail");
const price = document.getElementById("price");
const addToCartBtn = document.getElementById("add-to-cart");
const cartItems = document.getElementById("cart-items");
const cartTotal = document.getElementById("cart-total");
const bookImage = document.getElementById("book-image");

let cart = [];

function loadBooks() {
books.forEach((book, index) => {
const bookDiv = document.createElement("div");
bookDiv.classList.add("book");
bookDiv.textContent = book.title;
bookDiv.addEventListener("click", () => displayBookDetails(book, index));
bookList.appendChild(bookDiv);
});
}

function displayBookDetails(book, index) {


bookTitle.innerHTML = `Title: ${book.title}`;
bookAuthor.innerHTML = `Author: ${book.author}`;
detail.innerHTML = `Details: ${book.description}`;
price.innerHTML = `Price: $${book.price.toFixed(2)}`;
bookImage.src = book.image;
bookImage.style.display = "block";

addToCartBtn.style.display = "block";

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 28


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

// Remove previous event listener before adding a new one


addToCartBtn.onclick = null; // Reset previous event
addToCartBtn.onclick = () => addToCart(book);
}

function addToCart(book) {
cart.push(book);
updateCart();
}

function updateCart() {
cartItems.innerHTML = "";
cart.forEach((book, index) => {
const li = document.createElement("li");
li.textContent = `${book.title} - $${book.price.toFixed(2)}`;
cartItems.appendChild(li);
});
cartTotal.textContent = `Total: ${cart.length} items`;
}

document.addEventListener("DOMContentLoaded", loadBooks);

Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 29


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 30


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 31


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :5 Date: 24-02-2025


Program Title: XML

XML Structure and Design: Book Catalog XML: Design an XML structure to represent
the catalog of books in the online store. Include details such as book title, author,
genre, price, and availability.
books.xml:
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="books.xsd">

<book>
<title>The Alchemist</title>
<author>Paulo Coelho</author>
<description>A journey of self-discovery through the eyes of a shepherd who seeks treasure in
Egypt.</description>
<price>15.99</price>
<image>https://m.media-amazon.com/images/I/51Z0nLAfLmL.jpg</image>
</book>

<book>
<title>1984</title>
<author>George Orwell</author>
<description>A dystopian novel about a totalitarian regime that controls every aspect of
life.</description>
<price>12.99</price>
<image>https://m.media-amazon.com/images/I/41IXbIHb3-L.jpg</image>
</book>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 32


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<description>A powerful novel about racial injustice and childhood in the Deep
South.</description>
<price>14.49</price>
<image>https://m.media-amazon.com/images/I/81OthjkJBuL.jpg</image>
</book>

<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<description>A tragic love story set in the Roaring Twenties, filled with wealth and
decadence.</description>
<price>10.99</price>
<image>https://m.media-amazon.com/images/I/71VJUD5hvxL.jpg</image>
</book>

<book>
<title>Moby Dick</title>
<author>Herman Melville</author>
<description>A thrilling tale of obsession and revenge between a sailor and a great white
whale.</description>
<price>18.50</price>
<image>https://m.media-amazon.com/images/I/71A-9sHnY2L.jpg</image>
</book>

<book>
<title>Pride and Prejudice</title>
<author>Jane Austen</author>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 33


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<description>A timeless romance novel exploring love, class, and family in 19th-century
England.</description>
<price>9.99</price>
<image>https://m.media-amazon.com/images/I/91hhCXj9dbL.jpg</image>
</book>

<book>
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<description>A story of teenage rebellion and self-identity told through the eyes of Holden
Caulfield.</description>
<price>13.25</price>
<image>https://m.media-amazon.com/images/I/81OthjkJBuL.jpg</image>
</book>

<book>
<title>The Hobbit</title>
<author>J.R.R. Tolkien</author>
<description>A fantasy adventure following Bilbo Baggins as he embarks on an epic
quest.</description>
<price>16.75</price>
<image>https://m.media-amazon.com/images/I/91b0C2YNSrL.jpg</image>
</book>

</books>

books.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 34


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="image" type="xs:anyURI"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

BookValidator.java:
package pro1;

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;

public class BookValidator {


public static void main(String[] args) {
try {
// Files
File xmlFile = new File("C:/Users/abhishek/Downloads/wt lab/books.xml");
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 35
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

File xsdFile = new File("C:/Users/abhishek/Downloads/wt lab/books.xsd");

// Schema Factory and Validation


SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(xsdFile);
Validator validator = schema.newValidator();

// Validate
validator.validate(new StreamSource(xmlFile));
System.out.println("XML is valid against the XSD.");

} catch (Exception e) {
System.out.println("XML validation failed:");
System.out.println(e.getMessage());
}
}
}
Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 36


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :6 Date: 03-03-2025


Program Title: Java Servlet Programs

Implement the Servlet program to demonstrate:


1. Hello world:
.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;

public class Helloworld extends GenericServlet{


public void service(ServletRequest req,ServletResponse res) throws
ServletException,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body>");
out.println("</html>");
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-
app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<servlet>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 37


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<description></description>
<display-name>Helloworld</display-name>
<servlet-name>Helloworld</servlet-name>
<servlet-class>pro1.Helloworld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Helloworld</servlet-name>
<url-pattern>/Helloworld</url-pattern>
</servlet-mapping>
</web-app>
Output:

2. Init Parameters:
a. Single:
.java:
package pro1;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class Initparam extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletConfig config=getServletConfig();
String name=config.getInitParameter("uname");
out.print("<html><body>");
out.print("<h2>Uname is: " + name + "</h2>");
out.print("</body></html>");
out.close();
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 38
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-
app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<servlet>
<description></description>
<display-name>Initparam</display-name>
<servlet-name>Initparam</servlet-name>
<servlet-class>pro1.Initparam</servlet-class>
<init-param>
<param-name>uname</param-name>
<param-value>vnrvjiet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Initparam</servlet-name>
<url-pattern>/Initparam</url-pattern>
</servlet-mapping>
</web-app>
Output:

b. Enumeration:
.java:
package pro1;
import jakarta.servlet.*;

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 39


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

import jakarta.servlet.http.*;
import java.io.*;
import java.util.*;

public class InitparamEnum extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletConfig config=getServletConfig();
out.print("<html><body>");
Enumeration<String>e=config.getInitParameterNames();
String str="";
while(e.hasMoreElements()) {
str=e.nextElement();
out.println("Name:"+str+" value:"+config.getInitParameter(str)+"<br>");
}
out.print("</body></html>");
out.close();
}
}
Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<servlet>
<description></description>
<display-name>InitparamEnum</display-name>
<servlet-name>InitparamEnum</servlet-name>
<servlet-class>pro1.InitparamEnum</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>vnrvjiet</param-value>
</init-param>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 40


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<init-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitparamEnum</servlet-name>
<url-pattern>/InitparamEnum</url-pattern>
</servlet-mapping>
</web-app>
Output:

3. Servlet parameters:
a. Single:
.java:
package pro1;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class ReadParam extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
// Call doPost() for handling both GET and POST requests
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username = request.getParameter("username"); // Retrieve form parameters


String email = request.getParameter("email");
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 41
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

out.print("<html><body>");
out.print("<h2>Form Submission Results</h2>");
out.print("<p><b>Username:</b> " + username + "</p>");
out.print("<p><b>Email:</b> " + email + "</p>");
out.print("</body></html>");

out.close();
}
}
.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ReadParam" method="post">
Username: <input type="text" name="username"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<welcome-file-list>
<welcome-file>ParamFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ReadParam</display-name>
<servlet-name>ReadParam</servlet-name>
<servlet-class>pro1.ReadParam</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReadParam</servlet-name>
<url-pattern>/ReadParam</url-pattern>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 42


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

</servlet-mapping>
</web-app>

Output:

b. Enumeration:
.html:
<!DOCTYPE html>
<html>
<head>
<title>Servlet Parameter Example</title>
</head>
<body>
<h2>Enter Your Details</h2>
<form action="Servletenum" method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>

Age: <input type="number" name="age"><br><br>

Gender: <br>
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<br><br>

Favorite Languages (Select multiple): <br>


<input type="checkbox" name="language" value="Java"> Java <br>
<input type="checkbox" name="language" value="Python"> Python <br>
<input type="checkbox" name="language" value="C++"> C++ <br>
<br>

Country:
<select name="country">
<option value="USA">USA</option>
<option value="India">India</option>
<option value="UK">UK</option>
</select>
<br><br>

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


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

.java:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 43


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

package pro1;

import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Servletenum extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Retrieving parameters
String name = request.getParameter("name");
String email = request.getParameter("email");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
String country = request.getParameter("country");

// Retrieving multiple values (Checkboxes)


String[] languages = request.getParameterValues("language");

// Printing output
out.println("<html><body>");
out.println("<h2>User Details</h2>");
out.println("Name: " + name + "<br>");
out.println("Email: " + email + "<br>");
out.println("Age: " + age + "<br>");
out.println("Gender: " + gender + "<br>");
out.println("Country: " + country + "<br>");

out.println("Favorite Languages: ");


if (languages != null) {
for (String lang : languages) {
out.println(lang + " ");
}
} else {
out.println("No language selected.");
}

out.println("</body></html>");
}
}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 44
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<welcome-file-list>
<welcome-file>ParamFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Servletenum</display-name>
<servlet-name>Servletenum</servlet-name>
<servlet-class>pro1.Servletenum</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servletenum</servlet-name>
<url-pattern>/Servletenum</url-pattern>
</servlet-mapping>
</web-app>

Output:

4. Cookies (Setting, getting and deleting):


.java:
package pro1;

import jakarta.servlet.*;

import jakarta.servlet.http.*;

import java.io.*;

public class CookieServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String action = request.getParameter("action");

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 45


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

out.print("<html><body>");

out.print("<h2>Cookie Servlet</h2>");

if ("set".equalsIgnoreCase(action)) {

// Set a cookie

Cookie cookie = new Cookie("username", "JohnDoe");

cookie.setMaxAge(60 * 60 * 24); // Cookie expires in 1 day

response.addCookie(cookie);

out.print("<p><b>Cookie 'username' set!</b></p>");

} else if ("get".equalsIgnoreCase(action)) {

// Get all cookies

Cookie[] cookies = request.getCookies();

if (cookies != null) {

out.print("<h3>Stored Cookies:</h3>");

for (Cookie cookie : cookies) {

out.print("<p><b>Name:</b> " + cookie.getName() + ", <b>Value:</b> " + cookie.getValue() +


"</p>");

} else {

out.print("<p>No cookies found!</p>");

} else if ("delete".equalsIgnoreCase(action)) {

// Delete the "username" cookie

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (Cookie cookie : cookies) {

if ("username".equals(cookie.getName())) {

cookie.setMaxAge(0); // Expire it
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 46
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

response.addCookie(cookie);

out.print("<p><b>Cookie 'username' deleted!</b></p>");

} else {

out.print("<p>No cookies to delete!</p>");

} else {

out.print("<p>Invalid action! Use ?action=set, ?action=get, or ?action=delete</p>");

out.print("<br><br><a href='CookieServlet?action=set'>Set Cookie</a> | ");

out.print("<a href='CookieServlet?action=get'>Get Cookies</a> | ");

out.print("<a href='CookieServlet?action=delete'>Delete Cookie</a>");

out.print("</body></html>");

out.close();

Web.xml:
<servlet>
<description></description>
<display-name>CookieServlet</display-name>
<servlet-name>CookieServlet</servlet-name>
<servlet-class>pro1.CookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieServlet</servlet-name>
<url-pattern>/CookieServlet</url-pattern>
</servlet-mapping>

Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 47


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 48


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program No :7 Date: 24-03-2025


Program Title: Java Servlet Programs

Implement the servlet program to demonstrate:


1. HTTP Session:
First.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class First extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

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


HttpSession session = request.getSession();
session.setAttribute("username", name);

out.println("<html><body>");
out.println("<h3>Hello " + name + ", your session is created.</h3>");
out.println("<a href='Second'>Go to Second Servlet</a>");
out.println("</body></html>");
}
}
Second.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Second extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
String name = (String) session.getAttribute("username");

PrintWriter out = response.getWriter();


out.println("Welcome back via session: " + name);
}
}

.html:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 49


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<!DOCTYPE html>
<html>
<head><title>HTTP Session</title></head>
<body>
<h2>Login using HTTP Session</h2>
<form action="First" method="post">
Name: <input type="text" name="name" required>
<input type="submit" value="Login">
</form>
</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<welcome-file-list>
<welcome-file>NewFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>First</display-name>
<servlet-name>First</servlet-name>
<servlet-class>pro1.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Second</display-name>
<servlet-name>Second</servlet-name>
<servlet-class>pro1.Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/Second</url-pattern>
</servlet-mapping>
</web-app>

Output:

2. Hidden form fields:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 50


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

First.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class First extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

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


HttpSession session = request.getSession();
session.setAttribute("username", name);

out.println("<html><body>");
out.println("<form action='Second'>");
out.println("<input type='hidden' name='user' value='" + name + "'>");
out.println("<input type='submit' value='Go to Second'>");
out.println("</form>");
out.println("</body></html>");
}
}

Second.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Second extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
String name = (String) session.getAttribute("username");

PrintWriter out = response.getWriter();


out.println("Welcome back via session: " + name);
}
}

.html:
<!DOCTYPE html>
<html>
<head><title>HTTP Session</title></head>
<body>
<h2>Login using HTTP Session</h2>
<form action="First" method="post">

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 51


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Name: <input type="text" name="name" required>


<input type="submit" value="Login">
</form>
</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<welcome-file-list>
<welcome-file>NewFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>First</display-name>
<servlet-name>First</servlet-name>
<servlet-class>pro1.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Second</display-name>
<servlet-name>Second</servlet-name>
<servlet-class>pro1.Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/Second</url-pattern>
</servlet-mapping>
</web-app>

Output:

3. URL rewriting:
First.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 52


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

public class First extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
response.sendRedirect("Second?user=" + name);
}
}

Second.java:
package pro1;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Second extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Get the value directly from URL query string


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

out.println("<html><body>");
if (name != null && !name.isEmpty()) {
out.println("<h3>Welcome via URL Rewriting, " + name + "!</h3>");
} else {
out.println("<h3>No user data found in URL.</h3>");
}
out.println("</body></html>");
}
}

.html:
<!DOCTYPE html>
<html>
<head><title>HTTP Session</title></head>
<body>
<h2>Login</h2>
<form action="First" method="post">
Name: <input type="text" name="name" required>
<input type="submit" value="Login">
</form>
</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 53


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<welcome-file-list>
<welcome-file>NewFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>First</display-name>
<servlet-name>First</servlet-name>
<servlet-class>pro1.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Second</display-name>
<servlet-name>Second</servlet-name>
<servlet-class>pro1.Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/Second</url-pattern>
</servlet-mapping>
</web-app>

Output:

4. Request dispatcher:
First.java:
package pro1;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;

public class First extends HttpServlet {


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 54


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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


request.setAttribute("user", name);

RequestDispatcher rd = request.getRequestDispatcher("Second");
rd.forward(request, response);
}
}
Second.java:
package pro1;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;

public class Second extends HttpServlet {


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name = (String) request.getAttribute("user");


out.println("<h2>Hello, " + name + "! Welcome to RequestDispatcher example.</h2>");
}

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
doPost(request, response); // support GET
}
}
WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 55
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

.html:
<!DOCTYPE html>
<html>
<head>
<title>Request Dispatcher Example</title>
</head>
<body>
<form action="First" method="post">
<label for="name">Enter your name:</label>
<input type="text" name="username" id="name" required>
<input type="submit" value="Submit">
</form>
</body>
</html>
Web.xml:

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">
<display-name>pro1</display-name>
<welcome-file-list>
<welcome-file>NewFile.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>First</display-name>
<servlet-name>First</servlet-name>
<servlet-class>pro1.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Second</display-name>
<servlet-name>Second</servlet-name>
<servlet-class>pro1.Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/Second</url-pattern>

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 56


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

</servlet-mapping>
</web-app>

Output:

WEB TECHNOLOGIES LABORATORY 2022-26 BATCH 57

You might also like