Web Technology Lab KCS 652
Web Technology Lab KCS 652
Manual
(KCS652)
1. Write HTML/Java scripts to display your CV in navigator, your Institute website, Department Website and
Tutorial website for specific subject.
2. Write an HTML program to design an entry form of student details and send it to store at database server
like SQL, Oracle or MS Access.
3. Write programs using Java script for Web Page to display browsers information.
4. Write a Java applet to display the Application Program screen i.e. calculator and other.
5. Writing 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.
6. Program to illustrate JDBC connectivity. Program for maintaining database by sending queries. Design and
implement a simple servlet book query with the help of JDBC & SQL. Create MS Access Database, Create on
ODBC link, Compile & execute JAVA JDVC Socket.
7. Install TOMCAT web server and APACHE. Access the above developed static web pages for books web site,
using these servers by putting the web pages developed.
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. Create a Cookie and add these four user id’s and
passwords to this Cookie.
8(2). Read the user id and passwords entered in the Login form and authenticate with the values available in
the cookies.
9. Install a database (Mysql or Oracle). Create a table which should contain at least the following fields: name,
password, email-id, phone number Write a java program/servlet/JSP to connect to that database and extract
data from the tables and display them. Insert the details of the users who register with the web site, whenever
a new user clicks the submit button in the registration page.
10. 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
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>Personal Information</h2>
<p>Email: [email protected]</p>
</div>
<h2>Education</h2>
<p>B.Tech in Computer Science and Engineering, Indian Institute of Technology Delhi, 2024</p>
<p>Percentage: 84%</p>
<p>Percentage: 90%</p>
</div>
<h2>Experience</h2>
</div>
<h2>Skills</h2>
</div>
<h2>Projects</h2>
<p>Developed a secure online voting system using blockchain technology for ensuring transparency and
security.</p>
<p>Created a web application that provides real-time weather updates using React and OpenWeatherMap
API.</p>
</div>
<h2>Contact</h2>
<p>Email: [email protected]</p>
</div>
</body>
</html>
Output
Objective: Write an HTML program to design an entry form of student details and send it to store
at database server like SQL, Oracle or MS Access.
Code:
To design HTML entry form of student details and send it to store at database server like SQL, Oracle or MS
Access using JSP(Java Server pages). You need to set up a Java Web application environment.
1. Set up Environment: Install a web server (like Apache Tomcat), Java Development Kit (JDK), and MS
Access JDBC driver.
2. HTML form: Create a HTML form to get student details.
3. JSP Page: Write a JSP page to get data from HTML page and store the data in MS Access database.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Entry Form</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f2f2f2;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.container h2 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input, .form-group select {
width: 100%;
padding: 8px;
www.CodeRevise.com Download without watermark => https://coderevise.com/lab-manual/
box-sizing: border-box;
}
.form-group button {
width: 100%;
padding: 10px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
.form-group button:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="container">
<h2>Student Entry Form</h2>
<form action="submit_student.jsp" method="post">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required>
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required>
</div>
<div class="form-group">
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" id="address" name="address" required>
</div>
<div class="form-group">
<label for="course">Course:</label>
<select id="course" name="course" required>
<option value="B.Tech">B.Tech</option>
<option value="B.Sc">B.Sc</option>
<option value="M.Tech">M.Tech</option>
<option value="M.Sc">M.Sc</option>
</body>
</html>
<%
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
conn = DriverManager.getConnection(dbURL);
String sql = "INSERT INTO students (first_name, last_name, dob, email, phone, address, course) VALUES (?,
?, ?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, firstName);
pstmt.setString(3, dob);
pstmt.setString(4, email);
pstmt.setString(5, phone);
pstmt.setString(6, address);
pstmt.setString(7, course);
if (rows > 0) {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
%>
Explanation:
JSP Page:
Database Connection:
Driver: JDBC driver to connect to MS Access database.
Prepared Statement: to insert the form data into the students table.
Database Setup:
Ensure that you have an MS Access database (database.accdb) with a table (students) created with
appropriate columns(following):
dob (Date/Time)
Output
Objective: Write programs using Java script for Web Page to display browsers information.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browser Information</title>
<style>
body {
margin: 20px;
.info {
margin: 10px 0;
padding: 10px;
background-color: #f9f9f9;
.info h2 {
margin: 0 0 10px;
</style>
</head>
<body>
<div class="info">
<h2>Browser Information</h2>
<p id="browserName"></p>
<p id="browserVersion"></p>
<p id="platform"></p>
<p id="language"></p>
<p id="cookiesEnabled"></p>
</div>
<script>
function displayBrowserInfo() {
window.onload = displayBrowserInfo;
</script>
</body>
</html>
Output
Objective: Write a Java applet to display the Application Program screen i.e. calculator and other.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
TextField display;
char operator;
@Override
setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
numberButtons[i].addActionListener(this);
panel.add(numberButtons[i]);
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
eqButton.addActionListener(this);
clrButton.addActionListener(this);
panel.add(addButton);
panel.add(subButton);
panel.add(mulButton);
panel.add(divButton);
panel.add(eqButton);
panel.add(clrButton);
add(panel, BorderLayout.CENTER);
display.setText(display.getText() + command);
display.setText("");
operand2 = Double.parseDouble(display.getText());
switch (operator) {
display.setText(String.valueOf(result));
operand1 = result;
} else {
operand1 = Double.parseDouble(display.getText());
operator = command.charAt(0);
display.setText("");
Create an HTML file to load the applet and save the following code in a file named
CalculatorApplet.html:
Code:
<html>
<head>
<title>Calculator Applet</title>
</head>
<body>
</body>
</html>
appletviewer CalculatorApplet.html
Output
Objective: Writing 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.
Step 1: Create the XML Document that contains the data and save this file as catalog.xml.
Step 2: Create the DTD file to Define the structure of the XML document and save this file as catalog.dtd
Step 3: Create the XSLT Stylesheet to transform the XML into HTML for display and save this file as
catalog.xsl.
Step 4: Open catalog.xml in Internet Explorer (Ensure all three files catalog.xml, catalog.dtd, and
catalog.xsl).
Internet Explorer will process the XSLT stylesheet and display the XML document as an HTML table.
Output
When you open catalog.xml in Internet Explorer, you will see a table displaying the book catalog with the
following columns: Title, Author, and Price.
<!DOCTYPE html>
<html>
<head>
<title>Book Catalog</title>
<style>
body {font-family: Arial, sans-serif;}
table {width: 100%; border-collapse: collapse;}
th, td {border: 1px solid #ddd; padding: 8px;}
th {background-color: #f2f2f2;}
</style>
</head>
<body>
<h2>Book Catalog</h2>
<table>
<tr>
Objective: Program to illustrate JDBC connectivity. Program for maintaining database by sending
queries. Design and implement a simple servlet book query with the help of JDBC & SQL. Create MS
Access Database, Create on ODBC link, Compile & execute JAVA JDVC Socket.
Solution:
BookId (Number)
BookName (Text)
BookWriter (Text)
Example Code:
import java.util.*;
import java.io.*;
import java.sql.*;
class SaveDataAccess
//***********************SAVE DATA***********************
out.print("BookId: ");
int BookId=sc.nextInt();
out.print("BookName: ");
String BookName=sc.next();
String BookWriter=sc.next();
out.println(sql);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:mydsn2");
Statement st=cn.createStatement();
st.executeUpdate(sql);
cn.close();
out.println("Record Saved");
//***********************PRINT DATA***********************
/*
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn =DriverManager.getConnection("Jdbc:Odbc:mydsn2");
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(sql1);
System.out.print("\n"+"BookId"+"\t"+"BookName"+"\t"+"BookWriter");
while(rs.next())
System.out.print("\n"+rs.getString("BookId")+"\t"+rs.getString("BookName")+"\t"+rs.getString("BookWriter"))
;
*/
cn.close();
javac SaveDataAccess.java
java SaveDataAccess
Prerequisites
Install JDK
Download and add the JDBC driver for your database to your project’s classpath.
USE Libraray;
price DECIMAL(7, 2)
);
<web-app>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql://localhost:3306/library</param-value>
<context-param>
<param-name>jdbcUser</param-name>
<param-value>yourUsername</param-value>
</context-param>
<context-param>
<param-name>jdbcPassword</param-name>
<param-value>yourPassword</param-value>
</context-param>
<context-param>
<param-name>jdbcDriver</param-name>
<param-value>com.mysql.cj.jdbc.Driver</param-value>
</context-param>
</web-app>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/BookQueryServlet")
@Override
jdbcUrl = getServletContext().getInitParameter("jdbcUrl");
jdbcUser = getServletContext().getInitParameter("jdbcUser");
jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
try {
Class.forName(getServletContext().getInitParameter("jdbcDriver"));
} catch (ClassNotFoundException e) {
@Override
response.setContentType("text/html");
out.println("<html><body>");
while (resultSet.next()) {
out.println("<tr>");
out.println("</tr>");
out.println("</table>");
out.println("</body></html>");
} catch (SQLException e) {
Objective: Install TOMCAT web server and APACHE. Access the above developed static web pages
for books web site, using these servers by putting the web pages developed.
>>Download the latest version of Apache HTTP Server from the official website.
Run the installer and follow the instructions to complete the installation.
Place your static web pages in the Apache web server's document root directory. On Windows, this is typically
C:\Program Files\Apache Group\Apache2\htdocs. On Ubuntu, this is usually /var/www/html.
>>Create an HTML file named 'index.html' (or any other name) with a link to your servlet:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book Query</title>
</head>
<body>
</form>
</body>
</html>
Open a web browser and navigate to your static web page served by Apache:
http://localhost/index.html
Objective: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2,
pwd3 and pwd4 respectively. Write a servlet for doing the following. Create a Cookie and add
these four user id’s and passwords to this Cookie.
8 (2) Read the user id and passwords entered in the Login form (week1) and authenticate with the
values (user id and passwords) available in the cookies.
>> create a servlet to generate the cookie and store the user IDs and passwords.
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("/UserCookieServlet")
public class UserCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Define user IDs and passwords
String[] userIds = {"user1", "user2", "user3", "user4"};
String[] passwords = {"pwd1", "pwd2", "pwd3", "pwd4"};
<web-app>
<servlet>
<servlet-name>UserCookieServlet</servlet-name>
<servlet-class>com.example.UserCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserCookieServlet</servlet-name>
<url-pattern>/UserCookieServlet</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="LoginServlet" method="post">
<label for="userId">User ID:</label>
<input type="text" id="userId" name="userId" 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>
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
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Retrieve user ID and password from the request
String userId = request.getParameter("userId");
String password = request.getParameter("password");
if (cookies != null) {
for (Cookie cookie : cookies) {
www.CodeRevise.com Download without watermark => https://coderevise.com/lab-manual/
if ("userCredentials".equals(cookie.getName())) {
userCredentials = cookie.getValue();
break;
}
}
}
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
http://localhost:8080/AppName/login.html
Objective: Install a database (Mysql or Oracle). Create a table which should contain at least the
following fields: name, password, email-id, phone number Write a java program/servlet/JSP to
connect to that database and extract data from the tables and display them. Insert the details of
the users who register with the web site, whenever a new user clicks the submit button in the
registration page.
USE UserDatabase;
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>User Registration</h1>
<label for="name">Name:</label>
<label for="email">Email:</label>
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/RegisterServlet")
String sql = "INSERT INTO users (name, password, email, phone_number) VALUES (?, ?, ?, ?)";
statement.setString(1, name);
statement.setString(2, password);
statement.setString(3, email);
statement.setString(4, phone);
statement.executeUpdate();
} catch (SQLException e) {
response.setContentType("text/html");
out.println("<html><body>");
out.println("<h1>Registration Successful</h1>");
out.println("</body></html>");
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DisplayUsersServlet")
@Override
response.setContentType("text/html");
out.println("<html><body>");
out.println("<h1>Registered Users</h1>");
out.println("<table border='1'><tr><th>ID</th><th>Name</th><th>Email</th><th>Phone
Number</th></tr>");
while (resultSet.next()) {
int id = resultSet.getInt("id");
} catch (SQLException e) {
out.println("</table>");
out.println("</body></html>");
<web-app>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.example.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DisplayUsersServlet</servlet-name>
<servlet-class>com.example.DisplayUsersServlet</servlet-class>
</servlet>
<servlet-name>DisplayUsersServlet</servlet-name>
<url-pattern>/DisplayUsersServlet</url-pattern>
</servlet-mapping>
</web-app>
http://localhost:8080/YourApp/register.html
Objective: 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
USE UserDatabase;
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>User Registration</h1>
<label for="name">Name:</label>
<label for="password">Password:</label>
</form>
</body>
</html>
<%
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String sql = "INSERT INTO users (name, password, email, phone_number) VALUES (?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setString(3, email);
pstmt.executeUpdate();
out.println("<h1>Registration Successful</h1>");
} catch (Exception e) {
e.printStackTrace();
out.println("<h1>Registration Failed</h1>");
} finally {
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<label for="password">Password:</label>
</form>
</body>
</html>
<%
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String sql = "SELECT * FROM users WHERE name = ? AND password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
out.println("<h1>Login Successful</h1>");
} else {
out.println("<h1>Login Failed</h1>");
} catch (Exception e) {
e.printStackTrace();
out.println("<h1>Login Failed</h1>");
} finally {
%>
http://localhost:8080/AppName/register.html (Register new user by filling out the form and submitting it)
Objective: Design and implement a simple shopping cart example with session tracking API.
>> Create Product List (products.html) to display products and allow users to select items to add to the cart.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product List</title>
</head>
<body>
<h1>Products</h1>
<div>
</div>
<div>
</div>
<div>
</div>
</form>
</body>
</html>
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/CartServlet")
@SuppressWarnings("unchecked")
if (cart == null) {
session.setAttribute("cart", cart);
if (products != null) {
cart.add(product);
@SuppressWarnings("unchecked")
if ("view".equals(action)) {
request.setAttribute("cart", cart);
request.getRequestDispatcher("cart.jsp").forward(request, response);
} else if ("remove".equals(action)) {
if (cart != null) {
cart.remove(product);
response.sendRedirect("CartServlet?action=view");
>> Create the JSP to Display the Cart (cart.jsp) to display the contents of the shopping cart.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shopping Cart</title>
</head>
<body>
<%
} else {
out.println("<ul>");
out.println("<li>" + product +
out.println("</ul>");
%>
</body>
</html>