WebApp Lab Manual
WebApp Lab Manual
Dineshkumar Thangavel
QISCET
Skill Development Course
Table of Content
2 Snake Game 2
</body>
HTML </html>
<!DOCTYPE html> <div class="landing">
<html lang="en"> </div>
<head> <script src="script.js"></script>
</body>
<title>NavBar</title> </html>
<link rel="shortcut icon"
href="title_icon.png" type="image/x-
icon"> CSS
<link rel="stylesheet" :root{
href="https://cdnjs.cloudflare.com/aj --color1: #176B87;
ax/libs/font- --color2: #86B6F6;
awesome/6.5.1/css/all.min.css"> --color3: #B4D4FF;
<link rel="stylesheet" --color4: #EEF5FF;
href="style.css"> }
</head> *{
<body> margin: 0px;
padding: 0px;
<div class="navbar"> box-sizing: border-box;
}
<div class="logo">
<i id='logo' class="fa-solid fa- .navbar{
school fa-2x"></i>
</div> height:10vh;
background-color: var(--color4);
<div class="hamburger"> display: flex;
<i id="hamburger" class="fa-solid fa- justify-content: space-between;
bars fa-2x"></i> padding: 0px 10px;
</div> align-items: center;
}
<div id="nav-menu" class="nav-menu
hide"> .nav-menu a{
<a href="">Home</a> text-decoration: none;
<a href="">Service</a> color: var(--color1);
<a href="">Career</a> font-family: 'Franklin Gothic
<a href="">Contact Us</a> Medium', 'Arial Narrow', Arial, sans-
</div> serif;
margin: 0px 5px;
Web Application Development 1
.hamburger{
display: block;
Web Application Development 2
JS
<div class="score">
document.addEventListener('DOMContent <span id="scoreTitle">Score:</span>
Loaded', function () { <span id="score">0</span>
</div>
const hamburger =
document.getElementById('hamburger') <script src="script.js"></script>
const navMenu = </body>
document.getElementById('nav-menu') </html>
hamburger.addEventListener('click
CSS
', function(){
navMenu.classList.toggle('sho
w') :root{
navMenu.classList.toggle('hid --color1: #DCFFB7;
e') --color2: #FF004D;
}) --color3: #cc036b;
--color4: #1D2B53;
}) --color5: #DED0B6;
}
Snake Game *{
margin: 0px;
HTML padding: 0px;
font-family: 'Gill Sans', 'Gill
<!DOCTYPE html>
Sans MT', Calibri, 'Trebuchet MS',
<html lang="en">
sans-serif;
background-color: var(--color1);
<head>
text-align: center;
<meta charset="UTF-8">
}
<meta name="viewport"
content="width=device-width, initial-
h2{
scale=1.0">
font-size: xx-large;
<title>Snake Game</title>
color: var(--color4);
<link rel="stylesheet"
}
href="style.css">
</head>
#gameBoard{
border: 3px solid var(--color4);
<body>
margin: 10px 0px;
}
<h2>Snake Game</h2>
#scoreTitle, #score{
<div class="con">
font-size: x-large;
<canvas id="gameBoard" width="500px"
height="500px"> </canvas>
</div>
Web Application Development 3
snake.forEach((part)=>{
const score =
ctx.fillRect(part.x, part.y,
document.getElementById('score')
food, food)
let s = 0
ctx.strokeRect(part.x, part.y,
food, food)
const board =
})
document.getElementById('gameBoard')
}
let ctx = board.getContext('2d')
function displayFood(){
let width = board.width
xFood =
let height = board.height
Math.floor(Math.random()*(width/food)
let food = 25
)*food
let xFood = 0
yFood =
let yFood = 0
Math.floor(Math.random()*(height/food
let xs = food
))*food
let ys = 0
ctx.fillStyle = '#cc036b'
let started = false
ctx.fillRect(xFood, yFood, food,
food)
let snake = [{x:75, y:0},
}
{x:50, y:0},
{x:25, y:0},
function startGame(event){
{x:0, y:0}]
if(!started){
window.addEventListener('keydown',
started = true
startGame)//event
nextMove()
start()
}
function start(){
if(event.keyCode == 37 && xs!=food){
background()
xs = -food
displaySnake()
ys = 0
displayFood()
} }
else if(event.keyCode == 39 && xs!=-
function background(){ food){
xs = food
ctx.fillStyle = 'black'
ys = 0
ctx.fillRect(0, 0, width, height)
}
}
Web Application Development 4
function snakeMoving(){
// head
let head = {x: snake[0].x + xs, y:
snake[0].y + ys}
// paint the head
ctx.fillStyle = '#FF004D'
ctx.strokeStyle = 'black'
ctx.fillRect(head.x, head.y, food,
food)
ctx.strokeRect(head.x, head.y, food,
food)
// add the head
snake.unshift(head)
<label for="firstName">First
User Profile Name:</label>
<input type="text" id="firstName"
Registration name="firstName" required>
</div>
<div class="row">
HTML <label for="middleName">Middle
Name:</label>
<input type="text"
Form1:
id="middleName" name="middleName">
</div>
<!DOCTYPE html>
<html lang="en"> <div class="row">
<head> <label for="lastName">Last
<meta charset="UTF-8"> Name:</label>
<meta name="viewport" <input type="text" id="lastName"
content="width=device-width, initial- name="lastName" required>
scale=1.0"> </div>
<title>Personal Details</title>
<link rel="stylesheet" <div class="row">
href="style.css"> <label for="dob">Date of
</head> Birth:</label>
<body> <input type="date" id="dob"
<div class="con"> name="dob" placeholder="YYYY-MM-DD"
<h2>Registration Form</h2> pattern="\d{4}-\d{2}-\d{2}" required>
</div>
<form id="registrationForm"
action="reg1"> <div class="row">
<fieldset> <label for="age">Age:</label>
<input type="number" id="age"
<legend>Personal Details</legend> name="age" readonly>
</div>
<div class="row">
<label for="title">Title:</label> <div class="row">
<select id="title" name="title"> <label>Gender:</label>
<option <div class="genderItems">
value="mr">Mr</option> <input type="radio" id="male"
<option name="gender" value="male">
value="mrs">Mrs</option> <label
</select> for="male">Male</label>
</div> <input type="radio"
id="female" name="gender"
<div class="row"> value="female">
Web Application Development 6
<label
for="female">Female</label> <script src="script.js"></script>
</div> </body>
</div> </html>
</fieldset> Form2:
<fieldset>
<!DOCTYPE html>
<legend>Languages Known</legend>
<html lang="en">
<head>
<div class="language-container">
<meta charset="UTF-8">
<label id="lang"
<meta name="viewport"
for="language1">Language:</label>
content="width=device-width, initial-
<input type="text" id="language1"
scale=1.0">
name="languages[]" required>
<title>Educational Details</title>
<input type="checkbox"
<link rel="stylesheet"
id="language1-read"
href="style.css">
name="languagesRead[]" value="read">
</head>
<label for="language1-
read">Read</label>
<body>
<input type="checkbox"
<div class="con">
id="language1-write"
<h2>Registration Form</h2>
name="languagesWrite[]"
value="write">
<form id="registrationForm"
<label for="language1-
action="reg2">
write">Write</label>
<input type="checkbox"
<fieldset>
id="language1-speak"
<legend>Educational Details</legend>
name="languagesSpeak[]"
<div class="row">
value="speak">
<label
<label for="language1-
for="qualification">Qualification:</l
speak">Speak</label>
abel>
</div>
<select id="qualification"
name="qual">
<button type="button"
<option value="High School">High
id="addLanguage">Add
School</option>
Language</button>
<option
</fieldset>
value="Diploma">Diploma</option>
<option value="Bachelor of
<div class="btn-con">
Engineering">Bachelor of
<button type="submit">Next</button>
Engineering</option>
<button type="reset">Reset</button>
<option value="Bachelor of
</div>
Technology">Bachelor of
</form>
Technology</option>
</div>
Web Application Development 7
.con{ </body>
height: 100vh; </html>
}
</style>
</head> CSS
<body> :root{
<div class="con"> --color1:#DBE7C9;
<h2>Registration Form</h2> --color2:#789461;
--color3:#50623A;
<form id="registrationForm" --color4:#294B29;
action="reg3"> }
<fieldset>
<legend>LogIn Details</legend> * {
margin: 0px;
<div class="row"> padding: 0px;
<label box-sizing: border-box;
for="uname">UserName:</label> font-family:'Gill Sans', 'Gill
<input type="text" id="uname" Sans MT', Calibri, 'Trebuchet MS',
name="uname" required> sans-serif;
</div> }
} JS
document.getElementById('dob').addEve
input[type='radio'], ntListener('change', function() {
input[type='checkbox']{ const dob = new Date(this.value);
accent-color: var(--color4); const today = new Date();
} const age = today.getFullYear() -
dob.getFullYear();
.dashboard{ document.getElementById('age').value
height:100vh; = age;
width: 100%; });
display: flex; let languageCount = 1;
flex-direction: column; document.getElementById('addLanguage'
justify-content: center; ).addEventListener('click',
align-items: center; function() {
} const containers =
document.querySelectorAll('.language-
/* Style for the table */ container');
table { const container =
border-collapse: collapse; containers[containers.length - 1]; //
width: 60%; Select the last language container
}
languageCount++;
th, td {
border: 1px solid #dddddd; const languageDiv =
text-align: left; document.createElement('div');
padding: 8px; languageDiv.classList.add('language-
} container');
th { const label =
background-color: #f2f2f2; document.createElement('label');
} label.textContent = 'Language:';
languageDiv.appendChild(label);
/* Style for the user dashboard
heading */ const input =
h1 { document.createElement('input');
color: #333333; input.type = 'text';
} input.id = 'language' +
languageCount;
/* Style for the paragraph when no input.name = 'languages[]';
user data available */ input.required = true;
p { languageDiv.appendChild(input);
color: red;
font-style: italic; const checkboxes = ['read', 'write',
} 'speak'];
Web Application Development 11
@WebServlet("/reg1") import
public class RegisterServlet1 extends jakarta.servlet.ServletException;
HttpServlet { import
private static final long jakarta.servlet.annotation.WebServlet
serialVersionUID = 1L; ;
import
public void jakarta.servlet.http.HttpServlet;
service(HttpServletRequest request, import
HttpServletResponse response) throws jakarta.servlet.http.HttpServletReque
ServletException, IOException { st;
import
//create a new session jakarta.servlet.http.HttpServletRespo
HttpSession session = nse;
request.getSession(); import
jakarta.servlet.http.HttpSession;
//update form1 inputs into the import profile.User;
session
User user = new User(); import java.io.IOException;
user.setfName(request.getParamete @WebServlet("/reg2")
r("firstName")); public class RegisterServlet2 extends
user.setmName(request.getParamete HttpServlet {
r("middleName")); private static final long
user.setlName(request.getParamete serialVersionUID = 1L;
r("lastName"));
user.setDob(request.getParameter( public void
"dob")); service(HttpServletRequest request,
user.setAge(Integer.parseInt(requ HttpServletResponse response) throws
est.getParameter("age"))); ServletException, IOException {
user.setGender(request.getParamet
er("gender")); //get the prev session
HttpSession session =
session.setAttribute("user", request.getSession(false);
user); float a = 10;
//get the session attribute
Web Application Development 13
db.insert(user);
} catch (Exception e) { con =
// TODO Auto-generated catch DriverManager.getConnection(url,
block uname, pass);
e.printStackTrace(); }
}
//invalidate the session public User read(String uname) throws
session.invalidate(); SQLException, ClassNotFoundException
//respond {
response.getWriter().println("Reg
istration is Success"); //load the drive
} //create connection
this.connection();
}
//write query
query = "select * from
DAO CLASS user_profile where uname='"+uname
+"'";
Database:
//create statement
st = con.createStatement();
package profile;
//execute statement
import java.sql.*; ResultSet result =
st.executeQuery(query);
public class Database { User user = new User();
//process the result(select)
String url, uname, pass, db; while(result.next()) {
Connection con; user.setfName(result.getStrin
String query; g(1));
Statement st; user.setmName(result.getStrin
PreparedStatement pt; g(2));
user.setlName(result.getStrin
public Database(String db) { g(3));
super(); user.setDob(result.getString(
this.db = db; 4));
this.url = user.setAge(result.getInt(5))
"jdbc:mysql://localhost:3306/" + ;
this.db; user.setGender(result.getStri
this.uname = "root"; ng(6));
this.pass = "darwin";
} user.setQual(result.getString
(7));
public void connection() throws user.setInst(result.getString
SQLException, ClassNotFoundException (8));
{
Web Application Development 15
user.setMode(result.getString
(9)); pt.setString(7, user.getQual());
user.setYear(result.getInt(10 pt.setString(8, user.getInst());
)); pt.setString(9, user.getMode());
user.setPer(result.getFloat(1 pt.setInt(10, user.getYear());
1)); pt.setFloat(11, user.getPer());
user.setMobile(result.getLong pt.setLong(12, user.getMobile());
(12)); pt.setString(13,
user.setEmail(result.getStrin user.getEmail());
g(13)); pt.setString(14,
user.setAddress(result.getStr user.getAddress());
ing(14));
pt.setString(15,
user.setUname(result.getStrin user.getUname());
g(15)); pt.setString(16,
user.setPass(result.getString user.getPass());
(16));
} //execute pr statement
//close the connection int row = pt.executeUpdate();
con.close(); //close connection
return user;
} return row;
}
public int insert(User user) throws
SQLException, ClassNotFoundException public int delete(String uName)
{ throws SQLException,
//load the drive ClassNotFoundException {
//create connection //load the drive
this.connection(); //create connection
this.connection();
//write query
this.query = "insert into //write query
user_profile values(?, ?, ?, ?, ?, ?, this.query = "delete from
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; user_profile where uname=?" ;
if(result.getString(1).equals(pas
public int update(String address, sword)) {
String uName) throws SQLException, con.close();
ClassNotFoundException { return 2;
//load the drive }
//create connection else {
this.connection(); con.close();
return 3;
//write query }
this.query = "update user_profile }
set address=? where uname=?" ; }
} }
gender VARCHAR(10),
public void setEmail(String email) { language VARCHAR(50),
this.email = email;
read_language BOOLEAN,
}
write_language BOOLEAN,
public String getAddress() { speak_language BOOLEAN,
return address;
qualification VARCHAR(100) NOT NULL,
}
institution VARCHAR(100) NOT NULL,
public void setAddress(String mode_of_instruction VARCHAR(50),
address) {
this.address = address; year_of_passing INT(4) NOT NULL,
} pass_percentage DECIMAL(5,2) NOT NULL,
);
public String getPass() {
return pass;
}
TABLE
title VARCHAR(10),
middleName VARCHAR(50),
age INT,
Web Application Development 19
switch(result) {
package login; case 1->{
session.setAttribute("error",
import "username is wrong");
jakarta.servlet.RequestDispatcher; response.sendRedirect("login.
import jsp");
jakarta.servlet.ServletException; }
import case 2->{
jakarta.servlet.annotation.WebServlet RequestDispatcher rd =
; request.getRequestDispatcher("dashboa
import rd");
jakarta.servlet.http.HttpServlet; rd.forward(request,
import response);
jakarta.servlet.http.HttpServletReque }
st; case 3->{
import
session.setAttribute("error",
jakarta.servlet.http.HttpServletRespo "password is wrong");
nse; response.sendRedirect("login.
import jsp");
jakarta.servlet.http.HttpSession; }
import profile.Database; }
} catch (Exception e) {
import java.io.IOException; // TODO Auto-generated catch
import java.sql.SQLException; block
e.printStackTrace();
@WebServlet("/login") }
public class LoginServlet extends }
HttpServlet {
private static final long }
serialVersionUID = 1L;
public void
service(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
HttpSession session =
request.getSession();
Web Application Development 20
</tr> <tr>
<tr> <td>Mobile:</td>
<td>First Name:</td> <td>${user.mobile}</td>
<td>${user.fName}</td> </tr>
</tr> <tr>
<tr> <td>Email:</td>
<td>Middle Name:</td> <td>${user.email}</td>
<td>${user.mName}</td> </tr>
</tr> <tr>
<tr> <td>Address:</td>
<td>Last Name:</td> <td>${user.address}</td>
<td>${user.lName}</td> </tr>
</tr> </table>
<tr> </div>
<td>Date of Birth:</td>
<td>${user.dob}</td>
</tr> </body>
<tr> </html>
<td>Age:</td>
<td>${user.age}</td>
</tr>
<tr>
<td>Gender:</td>
<td>${user.gender}</td>
</tr>
<tr>
<td>Qualification:</td>
<td>${user.qual}</td>
</tr>
<tr>
<td>Institution:</td>
<td>${user.inst}</td>
</tr>
<tr>
<td>Mode of Study:</td>
<td>${user.mode}</td>
</tr>
<tr>
<td>Year of Study:</td>
<td>${user.year}</td>
</tr>
<tr>
<td>Percentage:</td>
<td>${user.per}</td>
</tr>