IT3401-Web Essentials Key April-May 2025
IT3401-Web Essentials Key April-May 2025
Answer Key
Part-A
1. Explain the working principle of a website with a diagram
The working principle of a website involves a user accessing a website through a browser.
The browser sends a request to a web server, which processes the request and retrieves
the necessary data from a database or stored files. The server then responds by sending
the website’s content back to the browser, which renders it for the user to view and
interact with.
3. Design an HTML form with fields for name, email and a submit button.
!DOCTYPE html>
<html>
<body>
<form action="submit_form.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
10. List out the steps for establishing connection between Java program and database.
Basic JDBC Workflow : Load the JDBC Driver, Establish a connection to the database,
Create a statement, Execute SQL queries, Process the results, Close the connection.
Part – B
11a. Explain in detail about the working of HTTP protocol.
Hyper Text Transfer Protocol (HTTP) takes part in web browser and web server
communication. Hence it is called a communication protocol. The HTTP protocol follows the
request response model.
The client makes a request for a desired web page by giving the URL in the address bar. This
request is submitted to the web server and then the web server gives response to the web
browser by returning the requested web page.
There is another commonly used method called POST. The POST method is typically used to
send information collected from a user form.
HTTP Version: The first HTTP version was HTTP/0.9 but the official version of HTTP was
HTTP/1.1.
Header Fields and Message body: The host header field is associated with the HTTP request.
The header fields are in the form of field name and field value.
HTTP Response Message Structure: The structure of response message is similar to the
request message structure. It is as follows
<status line>
<Header fields>
<Blank Line>
<Message Body>
Status Line: Status line is similar to the start line in the request message. It consists of three
fields.
The HTTP version denotes the HTTP version such as HTTP/1.1. The status code is a numeric
code indicating the type of response. The reason phrase is in the text string form and
presents the information about the status code. For example:
The header field in the response message is similar to that of the request message. The
message body consists of response message.
The word GET is an HTTP method indicating that the client wishes to obtain a resource from
the server. The remainder of the request provides the path name of the resource (e.g., an
HTML5 document) and the protocol’s name and version number (HTTP/1.1).
The client’s request also contains some required and optional headers. Any server that
understands HTTP (version 1.1) can translate this request and respond appropriately. The
below figure shows the web server responding to a request.
The entry domain name technicalpublication.org is searched within this database and if the
IP address for corresponding name is found then the IP address is returned to PC A. If it is not
found, then the name of another DNS is suggested. If the request is made for some invalid
domain name, then the error message is returned.
12a. Implement CSS animations to create a hover effect for a webpage button.
CSS Code:
.button {
background-color: #3498db;
color: white;
padding: 12px 24px;
font-size: 16px;
border: none;
cursor: pointer;
transition: all 0.3s ease-in-out;
border-radius: 5px;
}
.button:hover {
background-color: #2ecc71;
transform: scale(1.1);
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Button Hover Effect</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<button class="button">Hover Me!</button>
</body>
</html>
12b. Design an HTML5 form with fields for a student survey (name, rating, comments).
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Student Survey Form</title>
<style>
body {
font-family: Arial, sans-serif;
}
form {
width: 300px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
label {
font-weight: bold;
}
input, textarea, select {
width: 100%;
padding: 8px;
margin: 5px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
background-color: #28a745;
color: white;
cursor: pointer;
border: none;
}
input[type="submit"]:hover {
background-color: #218838;
}
</style>
</head>
<body>
<h2>Student Survey Form</h2>
<form action="submit_survey.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="rating">Rating:</label>
<select id="rating" name="rating" required>
<option value="">Select Rating</option>
<option value="Excellent">Excellent</option>
<option value="Good">Good</option>
<option value="Average">Average</option>
<option value="Poor">Poor</option>
</select>
<label for="comments">Comments:</label>
<textarea id="comments" name="comments" rows="4" placeholder="Write your
feedback..."></textarea>
<input type="submit" value="Submit Survey">
</form>
</body>
</html>
13a. “Implement a webpage with JavaScript that demonstrates event handling for a button
click. The button should change its color and display a message in the console when clicked.
Explain your code step-by-step”.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Event Handling</title>
<style>
.button {
background-color: #3498db;
color: white;
padding: 12px 24px;
font-size: 16px;
border: none;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease-in-out;
}
</style>
</head>
<body>
<h2>Click the Button to Change its Color</h2>
<button id="myButton" class="button">Click Me!</button>
<script>
// Select the button element
const button = document.getElementById("myButton");
// Add an event listener for the 'click' event
button.addEventListener("click", function() {
// Change the button's color
button.style.backgroundColor = "#2ecc71";
// Display a message in the console
console.log("Button Clicked: Color changed to green!");
});
</script>
</body>
</html>
13b. “Compare the efficiency of using addEventListener versus inline event handlers (e, g.
onClick) in JavaScript. Provide code examples for both approaches and analyze scenarios
where one method would be preferred over the other”.
JavaScript provides multiple ways to handle events on elements. Two common approaches
are:
1. Using addEventListener (Recommended for flexibility and maintainability)
2. Using Inline Event Handlers (Simple but less scalable)
Using addEventListener:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Event Handling with addEventListener</title>
</head>
<body>
<button id="myButton">Click Me!</button>
<script>
// Select the button element
const button = document.getElementById("myButton");
// Attach event listener for button click
button.addEventListener("click", function() {
alert("Button Clicked! (Using addEventListener)");
});
</script>
</body>
</html>
Advantages of addEventListener
Allows multiple event listeners on the same element
Supports various event types (e.g., click, mouseover, keydown)
Helps maintain cleaner code (separation of HTML & JavaScript)
Makes event management more structured and scalable
14b. Write a PHP script to process a login form (Validate username / password).
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Explanation
The HTML form collects the username and password, sending them to login.php.
The PHP script retrieves the form data using $_POST.
Session handling (session_start()) stores the username upon successful login.
htmlspecialchars() sanitizes input to prevent security vulnerabilities.
The script compares the credentials to predefined values (admin / password123).
Displays success or error messages based on user input.
15b(i). Write a program to explain the passing of values to a servlet from a HTML form using
doGet() / doPost().
HTML Form (User Input)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Servlet Data Passing</title>
</head>
<body>
<h2>Submit Your Details</h2>
<form action="MyServlet" method="get">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br><br>
<input type="submit" value="Submit (GET)">
</form>
<form action="MyServlet" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="Submit (POST)">
</form>
</body>
</html>
Explanation
Servlet Mapping (@WebServlet(“/MyServlet”)) registers the servlet.
doGet() retrieves values from URL parameters using request.getParameter().
doPost() retrieves form data sent in the request body.
Response (PrintWriter) generates an HTML response displaying the received values.
Part – C
16a. Evaluate the effectiveness of different JavaScript form validation techniques for a user
registration system. Design a Solution that:
i. Validates username (alphanumeric, 5 – 12 characters), password (8+
characters, special characters, and email (regex)
ii. Provides real-time feedback using DOM manipulation
iii. Justify your approach over server-side validation for this scenario.
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Real-time Form Validation</title>
<style>
.error { color: red; font-size: 14px; }
.valid { color: green; font-size: 14px; }
</style>
</head>
<body>
<h2>User Registration</h2>
<form id="registrationForm">
<label for="username">Username:</label>
<input type="text" id="username" required>
<span id="usernameError" class="error"></span><br><br>
<label for="password">Password:</label>
<input type="password" id="password" required>
<span id="passwordError" class="error"></span><br><br>
<label for="email">Email:</label>
<input type="email" id="email" required>
<span id="emailError" class="error"></span><br><br>
<input type="submit" value="Register">
</form>
<script src="validation.js"></script>
</body>
</html>
JavaScript Validation
document.addEventListener("DOMContentLoaded", function() {
const username = document.getElementById("username");
const password = document.getElementById("password");
const email = document.getElementById("email");
const usernameError = document.getElementById("usernameError");
const passwordError = document.getElementById("passwordError");
const emailError = document.getElementById("emailError");
// Username Validation (5-12 alphanumeric characters)
username.addEventListener("input", function() {
const regex = /^[a-zA-Z0-9]{5,12}$/;
if (regex.test(username.value)) {
usernameError.textContent = "✓ Valid username";
usernameError.className = "valid";
} else {
usernameError.textContent = "✗ Username must be 5-12 alphanumeric characters!";
usernameError.className = "error";
}
});
// Password Validation (8+ characters, includes special character)
password.addEventListener("input", function() {
const regex = /^(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/;
if (regex.test(password.value)) {
passwordError.textContent = "✓ Strong password!";
passwordError.className = "valid";
} else {
passwordError.textContent = "✗ Password must be at least 8 characters & include a
special character!";
passwordError.className = "error";
}
});
// Email Validation (Correct email format)
email.addEventListener("input", function() {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (regex.test(email.value)) {
emailError.textContent = "✓ Valid email format";
emailError.className = "valid";
} else {
emailError.textContent = "✗ Invalid email format!";
emailError.className = "error";
}
});
});
Limitations:
Client-side validation can be bypassed (users may disable JavaScript).
Server-side validation is essential for security (protection against SQL Injection, XSS).
When is Server-side Validation Required?
For final input verification before storing data
To prevent malicious attacks (e.g., SQL Injection, Cross-site scripting)
Handling sensitive data securely
16b. Create a student database with 4 records – name, regno, year, cgpa. Insert 4 rows into the
table created and write a html code and PHP script to retrieve a specific record of student
based on regno value entered.
Step 1:
Create a database named std_db. Create a table student for this database and insert the values
in this table.
PHP Document[DBDemo.php]
<?php
// Make a MySQL Connection
mysqli_connect(“localhost”,”root”,”mypassword”,”dbname”); or die(mysql_error());
mysqli_select_db(“test”) or die(mysql_error());
echo “Connected to database”;
mysqli_query($conn, “CREATE TABLE Student(Name VARCHAR(30), Regno INT, Year INT, CGPA
VARCHAR(30))”) or die(mysqli_error());
print”<br/>”;
echo “Table Created”;
//Insert a row of information into table “example”
mysqli_query($conn, “INSERT INTO Student(Name,Regno,Year, CGPA) VALUES
(‘A’,’105’,’2024’,8.5)”) or die (mysqli_error());
mysqli_query($conn, “INSERT INTO Student(Name,Regno,Year,CGPA) VALUES
(‘B’,’102’,’2024’,9.0)”) or die (mysqli_error());
mysqli_query($conn, “INSERT INTO Student(Name,Regno,Year,CGPA) VALUES
(‘C’,’103’,’2024’,8.98)”) or die (mysqli_error());
mysqli_query($conn, “INSERT INTO Student(Name,Regno,Year,CGPA) VALUES
(‘D’,’104’,’2024’,7.55)”) or die (mysqli_error());
print”<br/>”;
echo “Data Inserted”;
$result=mysqli_query($conn, “SELECT * from mytable”)
or die(mysqli_error());
print”<br/>”;
print”<b> User Database</b>”;
echo”<table border=’1’>”;
echo”<tr><th>Name</th><th>Regno</th> <th>Year</th><th>CGPA</th></tr>”;
while($row=mysqli_fetch_array($result))
{
//Print out the contents of each row into a table
echo”<tr><td>”;
echo $row[‘Name’];
echo”</td><td>”;
echo $row[‘Regno’];
echo”</td><td>”;
echo $row[‘Year’];
echo”</td><td>”;
echo $row[‘CGPA’];
echo”</td></tr>”;
}
echo “</table>”;
Step 2: Create an HTML form to accept the registration number, the HTML document is as
follows-
result.html
<!DOCTYPE html>
<html>
<head>
<title>STUDENT RESULT</title>
</head>
<body>
<form name = “myform” method= “post” action= “http://localhost/result.php”>
<input type= “text” name= “reg_no” />
<input type = “submit” value= “Submit” />
</form>
</body>
</html>
Step 3: Create a PHP Script to accept the registration number. This PHP script will connect to
MYSQL database and fetch the entire row of the student table based on the register number
entered.
Result.php
<?php
//Make a SQL Connection
$conn=mysqli_connect(“localhost”, “root”, “”, “dbname”);
if(!$conn)
{
die(‘error in connection’.mysqli_error());
}
//mysqli_select_db(“mydb”,$conn);
$reg_no = intval($_POST[‘reg_no’]);
$result=mysqli_query($conn, “SELECT * FROM student where Regno =‘$reg_no’”);
while($row=mysqli_fetch_array($result))
{
echo $row[‘Regno’]. “is”. $row[‘STATUS’];
echo “<br/>”;
}
mysqli_close($conn);
?>
Step 4: Load the HTML form created in Step 2 and click the submit button by entering some
registration number
Output:
Name Regno Year CGPA
A 105 2024 8.5