Web Tech Lab Record-1 - Edited
Web Tech Lab Record-1 - Edited
on
B.Tech.
In
Submitted By:
Admission Number YEAR : III
22SCSE1011726
Student Name Ansh Tripathi SEMESTER : VI
June 2024
1
Objective:
The objective of this lab is to develop an ability to design and
implementstatic and dynamic website.
OUTCOMES:
2
Page MARK
S. No. LIST OF EXPERIMENTS No.
3
Write a AJAX program in which user will enter a
8.
numberand table of that will be displayed
30
4
Experiment 1
Design the following static web pages required for an online book store web site.
1) HOME PAGE: The static home page must contain three frames.
2) LOGIN PAGE
3) CATOLOGUE PAGE: The catalogue page should contain the details of all the
books available in the web site in a table.
4) REGISTRATION PAGE
Aim: Design the following static web pages required for online book store.
1. Home page:- the static home page must contains three pages
2. Top frame:- logo and college name and links to homepage, login page,
registration page and catalogue page
3. Left frame:- at least four links for navigation which will display the catalogue
of Respective links
4. Right frame:- the pages to links in the left frame must be loaded here initially
it Contains the description of the website.
DESCRIPTION: In this program the entire web paged are created by using
basic HTML tags. Home page is divided into 3 frames by using <frameset> and
<frame> tags. A frame is used to display a web page within a web page.
<frameset>:
• The <frameset> tag defines a frameset.
• The <frameset> element holds one or more <frame> elements.
• Each <frame> element can hold a separate document.
• The <frameset> element specifies HOW MANY columns or rows there will
be in the frameset, and HOW MUCH percentage/pixels of space will occupy
each of them.
<frame>:
• The <frame> tag defines one particular window (frame) within a
<frameset>.
• Each <frame> in a <frameset> can have different attributes, such as
border, scrolling, the ability to resize, etc.
PROGRAM:
home.html:
<frameset rows="40%,*">
<frame src="top.html" noresize scrolling="NO" name="topframe">
<frameset cols="15%,*">
<frame src="left.html" noresize scrolling="NO" name="leftframe">
<frame src="right.html" noresize name="rightframe" scrolling="auto">
</frameset>
</frameset>
top.html:
<html>
<head>
5
<title>Top Frame</title>
</head>
<body bgcolor="YellowGreen ">
<img src="images/logo1.png" width="125" height="115" align="left">
left.html:
<html>
<body align="center" bgcolor="bisque"> <br>
<a href="cse.html" target="rightframe"><font
size="6">CSE</font></a><br><br>
<a href="ece.html" target="rightframe"><font
size="6">ECE</font></a><br><br>
<a href="eee.html" target="rightframe"><font
size="6">EEE</font></a><br><br>
<a href="mech.html" target="rightframe"><font
size="6">MECH</font></a><br>
</body>
</html>
right.html:
<html>
6
<body bgcolor="orange">
<center>
<img src="images/Books.jpg" height="170"><br>
<font face="Brush Script MT" size="5" color="blue">
<h1><b>Welcome to the Online Book Store!!!</b></font><br />
<font face="Brush Script MT" size="5" color="red">
<h2><b> "A Huge Collection Of Engineering E-Books"</b></h2></font>
</center>
</body>
</html>
cse.html:
<html>
<head><title>CSE</title></head>
<body bgcolor="cyan">
<center><font color="blue"><h1>Computer Science and
Engineering</h1></font></center>
<br>
<table align="center">
<tr>
<td>Text Books</td>
<td>
<select >
<option value="select the book" selected>Select the book
<option value="C&Ds">C&Ds
<option value="Ads">Ads
<option value="Java">Java
<option value="Oracle">Oracle
<option value="Ms SQL Server">Ms SQL Server
<option value="MySql">MySql
</select>
</td></tr>
<tr>
<td>Quantity</td>
<td><input type="text" id="q"></td>
</tr>
<tr>
<td></td>
<td>
<form method=post action="order.html">
<input type="submit" value=ok />
</form>
</td>
</tr>
</table>
<center>
<pre> Cost of one book is"500" + shipping "100" </pre>
</center>
</body>
</html>
7
ece.html:
<html>
<body bgcolor="Plum">
<h1><font color="blue">Electronics and Communication
Engineering</font></h1>
<h2>
<ul>
<li>Digital Circuits</li> <li>Signals and Systems</li> <li>Digital
Communication</li>
</ul>
</h2>
</body>
</html>
eee.html:
<html>
<body bgcolor="Plum">
<h1><font color="blue">Electrical and Electronics Engineering</font></h1>
<h2>
<ul type="square">
<li>Concepts in Electric Circuits</li>
<li>Introduction to Electronic Engineering</li>
<li>Electrical Power</li>
</ul>
</h2>
</body>
</html>
mech.html:
<html>
<body bgcolor="Plum">
<h1><font color="blue">Electronics and Communication
Engineering</font></h1>
<h2>
<ol type="I">
<li>Theory of Machines</li>
<li>Automation and Robotics</li>
<li>Engineering Fluid Mechanics</li>
</ol>
</h2>
</body>
</html>
catalogue.html:
<html>
<head>
<title> Catalogue </title>
</head>
<body bgcolor="pink">
<form action="order.html">
<table border="1" width="100%">
<tr>
<td>
8
<img src="images/wt.jpg" width=100 height=100/>
</td>
<td> Book: Web Technologies <br> Author: Uttam K. Roy <br>
Publication:Oxford
University Press</td> <td>531 </td>
<td> <input type="submit" value="Add to cart"/></td> </tr>
<tr>
<td> <img src="images/php.jpg" width=100 height=100/></td>
<td> Book: PHP & MySQL Web Development <br> Author:Luke Welling & Laura
Thompson <br> Publication:PEARSON</td> <td> 898
</td>
<td> <input type="submit" value="Add to cart"/></td> </tr>
</table> </form>
</body> </html>
login.html:
<html>
<body bg color="pink">
<basefont face="Cambria" size="4"> <br>
<center>
<img src="images/login.jpg" width="385" height="135" /><br />
<font face="Brush Script MT" size="7" color="purple">
<b>Enter Login Details:</b>
</font>
</center>
<form name="f1" method="post" action="right.html">
<table align="center" width="100" height="150" cellspacing=”15”>
<tr><td><b>Login ID:</b></td>
<td><input type="text" name="t1"></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input type="password" name="t2"></td>
</tr>
<tr align="center">
<td><input type="submit" name="b1" value="Submit"></td>
<td><input type="reset" name="b2" value="Reset"></td>
</tr>
</table> </form> </basefont> </body> </html>
registration.html:
<html>
<head><title>Registration Form</title></head>
<body bgcolor="#E4F0F8">
<center><font color="blue" size="6" face="arial">Registration
Form</font></center><br />
<form action="right.html">
First Name(Minimum 6 characters)<font color="red">* </font>
<input type='text' id='firstname' /><br /><br />
Last Name<font color="red"><font color="red">* </font> </font>
9
<input type='text' id='lastname' /><br /><br />
EmailAddress<font color="red">* </font>
<input type='text' id='email' /><br />
<font color="red">(one e-mail id only):</font>
<font color="redblue">e.g. [email protected]</font><br /><br/>
Password(minimum 6 characters)<font color="red">* </font>
<input type='password' id='pass'><br /><br/>
Address<font color="red">* </font>
<textarea rows="2" cols="20" id='addr' /></textarea> <br /> <br/>
Mobile No<font color="red">* </font>
<input type='text' id='mobileno' /><br />
Gender: <input type='radio' name="gender">male
<input type='radio' name="gender">female<br/><br />
<input type='Submit' value='submit' />
<input type='Reset' value='reset' />
</form> </body> </html>
order.html:
<html>
<head><title>order conformation</title></head>
<body bgcolor="cyan">
<center>
<pre><strong>
<b>Your order Is Conformed
</strong></pre>
<h2><b>THANK YOU...Visit Again</h2>
</center>
</body>
</html>
OUTPUT
10
11
Experiment 2
12
if(lengthRestriction1(mobileno, 10 , 10)){
return true;
}}}}
}
}
}
}}}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
13
function lengthRestriction(elem, min){
var uInput = elem.value;
if(uInput.length >= min){
return true;
}else{
alert("Please enter minimum " +min+ " characters");
elem.focus();
return false;
}
}
function emailValidator(elem, helperMsg)
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp))
{
return true;
}
else{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction1(elem, min, max)
{
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max)
{
return true;
}
else {
alert("Please enter 10 numbers only");
elem.focus();
return false;
}
}
</script>
<center><font color="blue" size="6" face="arial">Registration
Form</font></center><br />
<form onsubmit='return formValidator()' action="right.html">
First Name(Minimum 6 characters)<font color="red">* </font>
<input type='text' id='firstname' /><br /><br />
Last Name<font color="red"><font color="red">* </font> </font>
<input type='text' id='lastname' /><br /><br />
Email Address<font color="red">* </font>
<input type='text' id='email' /><br />
<font color="red">(one e-mail id only):</font>
14
<font color="redblue">e.g. [email protected]</font><br /><br/>
Password(minimum 6 characters)<font color="red">* </font>
<input type='password' id='pass'><br /><br/>
Address<font color="red">* </font>
<textarea rows="2" cols="20" id='addr' /></textarea> <br /> <br/>
Mobile No<font color="red">* </font>
<input type='text' id='mobileno' /><br />
Gender: <input type='radio' name="gender">male
<input type='radio' name="gender">female<br/><br />
<input type='Submit' value='submit' />
<input type='Reset' value='reset' />
</form>
</body>
</html>
15
Experiment 3
Develop and demonstrate the usage of inline, internal and external style
sheet using CSS.
Aim: Design a web page using CSS which includes the following:
1) Use different font styles
2) Control the repetition of image with background-repeat and no-repeat
property
3) Define style for links as a: link, a: active, a: hover, a: visited
4) Add customized cursors for links.
PROGRAM:
style.css
p.left
{
text-align:left;
color:blue;
font-family:Cambria;
font-size:large;
text-indent:20px;
}
p.center
{
text-align:center;
text-decoration:underline;
text-transform:uppercase;
letter-spacing:-3px;
word-spacing:20px;
font-size:larger;
}
p.right
{
text-align:right;
color:red;
font-family:Tahoma;
font-size:15pt;
text-decoration:overline;
font-style:italic;
}
b#headline
{
color:orange;
font-size:22px;
font-family:arial;
text-decoration:underline;
}
sample.html
<html>
16
<head>
<style type="text/css">
body
{
background-image:url('images/cse.png');
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed;
background-color:pink;
}
a:link { text-decoration:none;color:orange; }
a:visited { text-decoration:none;color:red; }
a:hover { text-decoration:underline;color:blue; }
a:active { text-decoration:underline;color:purple; }
h3 { color:green; }
.c1{cursor:crosshair}
.c2{cursor:pointer}
.c3{cursor:move}
.c4{cursor:text}
.c5{cursor:wait}
.c6{cursor:help}
</style>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="cyan">
<h1 style="color:blue;text-align:center;"> CSS (Inline, Internal and External) </h1>
<p>This Paragraph is a Not Styled</p>
<p class="left">This Paragraph is Styled by class "Left"</p>
<p class="center">This Paragraph is Styled by class "Center"</p>
<p class="right">This Paragraph is Styled by class "Right"</p>
<b>This is normal Bold</b> <br>
<b id="headline">This Bold Text is Styled </b>
17
This Paragraph is Styled by class "Center"
This is a link
The cursor over this element is plus sign
OUTPUT
18
Experiment 4
4: Develop and demonstrate JavaScript with POP-UP boxes and functions for
the following problems:
a) Input: Click on Display Date button using onclick( ) function
Output: Display date in the textbox
b) Input: A number n obtained using prompt
Output: Factorial of n number using alert
c) Input: A number n obtained using prompt
Output: A multiplication table of numbers from 1 to 10 of n using alert
d) Input: A number n obtained using prompt and add another number using
confirm
Output: Sum of the entire n numbers using alert
PROGRAM:
a) date.html
<html>
<body>
<script>
function display(){
var x="You have clicked";
var d=new Date();
var date=d.getDate();
var month=d.getMonth();
month++;
var year=d.getFullYear();
document.getElementById("dis").value=date+"/"+month+"/"+year;
}
</script>
<form>
<input type="text" id="dis" /><br />
<input type="button" value="Display Date" onclick="display()" />
</form>
<body>
</html>
OUTPUT:
b) factorial.html
<html>
<head>
<title>factorial</title>
<script language='javascript'>
function factorialcalc()
{
number = parseint(prompt("enter a number, i'll calculate its factorial", "whole
numbers bigger than zero, please"))
19
factorial = 1
for (i=1; i <= number; i++)
{
factorial = factorial * i
}
alert("the factorial of " + number + " is " + factorial)
}
</script>
</head>
<body><form name=frm>
<input type=button value='factorial' onclick="factorialcalc();">
</form>
</body>
</html>
c) multable.html
<html>
<head><title> Multiplication Table </title></head>
<body>
<script type="text/javascript">
<!--
var n=prompt("Enter positive value for n: "," ");
if(!isNaN(n)) {
var table="";
var number="";
for(i=1;i<=10;i++) {
number = n * i;
table += n + " * " + i + " = " + number + "\n";
}
alert(table);
}
else {
alert("Enter positive value");
n=prompt("Enter positive value for n: "," ");
}
document.write(n+" table values displayed using alert ..<br />");
// -->
</script>
</body>
</html>
OUTPUT
20
d) sum of n numbers.html
<html>
<head><title>sum of n numbers using popup boxes</title>
<script language='javascript'>
function addsum()
{
alert("you're going to give me a list of numbers. i'm going to add them together
for you");
var keepgoing = true
var sumofnums = 0
while (keepgoing) {
sumofnums = sumofnums + parseint(prompt("what's the next number to
add?",""))
keepgoing = confirm("add another number?")
}
alert("the sum of all your numbers is " + sumofnums)
}
</script>
</head>
<body>
<form name=frm>
<input type=button value='sum of n numbers' onclick="addsum();">
</form>
</body>
</html>
21
Experiment 5
<html>
<head>
<title>WT Lab manual program no. 3</title>
</head>
<style>
h1
{
color: red;
text-align: center;
}
.textbox1
{
color: blue;
font-size: 30px;
font-weight: bold;
}
</style>
<body>
<center>
<h1> Select the country name to find its capital</h1>
<form name="myform">
Select Country <select name="country" id="sbox1"
onClick="myFunction()" required>
<option value=""></option>
<option value="NEW DELHI">INDIA</option>
<option value="CANBERRA">AUSTRALIA</option>
<option value="WASHINGTON D.C">AMERICA</option>
<option value="LONDON">UNITEDKINGDOM</option>
<option value="BERLIN">GERMANY</option>
</select><br><br>
Capital <input type="text" class="textbox1" id="sbox2">
</form>
</center>
<script>
function myFunction()
{ var a=document.getElementById("sbox1").value;
document.getElementById("sbox2").value=a; }
</script> </body> </html>
OUTPUT:
22
23
Experiment 6
PROGRAM:
0-999.html
<html>
<head>
<title>HTML - Convert numbers to words using JavaScript</title>
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</SCRIPT>
<script>
function NumToWord(inputNumber, outputControl)
{
var str = new String(inputNumber)
var splt = str.split("");
var rev = splt.reverse();
var once = ['Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', '
Eight', ' Nine'];
var twos = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', '
Sixteen', '
Seventeen', ' Eighteen', ' Nineteen'];
var tens = ['', 'Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', '
Seventy', ' Eighty', '
Ninety'];
numLength = rev.length;
var word = new Array();
var j = 0;
for (i = 0; i < numLength; i++) {
switch (i) {
case 0:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
24
}
else {
word[j] = once[rev[i]];
}
word[j] = word[j];
break;
case 1:
aboveTens();
break;
case 2:
if (rev[i] == 0) {
word[j] = '';
}
else if ((rev[i - 1] == 0) || (rev[i - 2] == 0)) {
word[j] = once[rev[i]] + " Hundred ";
}
else {
word[j] = once[rev[i]] + " Hundred and";
}
break;
default: break;
}
j++;
}
function aboveTens() {
if (rev[i] == 0) { word[j] = ''; }
else if (rev[i] == 1) { word[j] = twos[rev[i - 1]]; }
else { word[j] = tens[rev[i]]; }
}
word.reverse();
var finalOutput = '';
for (i = 0; i < numLength; i++) {
finalOutput = finalOutput + word[i];
}
document.getElementById(outputControl).innerHTML = finalOutput;
}
</script>
</head>
<body>
<h1>HTML - Convert numbers to words using JavaScript</h1>
<input id="Text1" type="text" onkeypress="return isNumberKey(event)"
onkeyup="NumToWord(this.value,'divDisplayWords');" maxlength="3"
style="backgroundcolor: #efefef; border: 2px solid #CCCCC; font-size:
large" />
<br /> <br />
<div id="divDisplayWords" style="font-size: 30; color: Teal; font-family:
Arial;">
25
</div>
</body>
</html>
OUTPUT
26
Experiment 7
Create a form using various form elements and print the values using
Java Script
<html>
<head><title>cust_detail</title>
<script type="text/javascript">
function f1()
{
for(var i=0;i<document.frm1.r1.length;i++)
{
if(document.frm1.r1[i].checked)
{
var c=document.frm1.r1[i].value;
}
}
for(var j=0;j<document.frm1.c1.length;j++)
{
if (document.frm1.c1[j].checked)
{
var d=document.frm1.c1[j].value;
}
}
for(var k=0;k<document.frm1.m1.length;k++)
{
if(document.frm1.m1[k].selected)
var e=document.frm1.m1[k].value;
for(var l=0;l<document.frm1.m2.length;l++)
if(document.frm1.m2[l].selected)
var f=document.frm1.m2[l].value;
27
}
}
<body>
<form name="frm1">
<table>
<tr><td>name</td><td>
<input type="textbox" name="t1"></td></tr>
<tr><td>Fathers name</td><td>
<input type="textbox" name="t2"></td></tr>
<tr><td>date of birth</td><td>
<input type="textbox" name="t3"></td></tr>
<tr><td>Gender</td><td>
<input type="radio" name="r1" checked="checked"
value="male">male</td><td>
<input type="radio" name="r1" value="female">female</td></tr>
tr><td>Address</td><td>
<input type="textbox" name="t4"></td></tr>
<tr><td>Contact no.</td><td>
<input type="textbox" name="t5"></td></tr>
<tr><td>course</td><td>
<select name="m1" ><option name="B.tech" value="B.tech"
>B.tech</option>
<option name="M.tech" value="M.tech">M.tech</option></td></tr>
28
<input type="textbox" name="t7"></td></tr>
<tr><td><input type="submit" name="s1"
onClick="f1()"></td><td>
<input type="reset" name="s2"></td></tr>
</table>
</form>
</body>
</html>
OUTPUT
29
Experiment 8
Write a AJAX program in which user will enter a number and table
of that will be displayed
<html> <head> <script>
var request;
function sendInfo() {
var v=document.vinform.t1.value;
var url="index.jsp?val="+v;
if(window.XMLHttpRequest){
request=new XMLHttpRequest(); }
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
Try {
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send();
} catch(e)
{
alert("Unable to connect to server"); } }
function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('amit').innerHTML=val;
}
}
</script> </head>
<body>
<marquee><h1>This is an example of ajax</h1></marquee>
<form name="vinform">
<input type="text" name="t1">
<input type="button" value="ShowTable" onClick="sendInfo()">
</form>
<span id="amit"> </span>
</body>
</html>
30
31
Experiment 9
To achieve this, we need to do the following:
users.xml:
xml
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<id>1</id>
<name>John Doe</name>
<email>[email protected]</email>
<age>30</age>
</user>
<user>
<id>2</id>
<name>Jane Smith</name>
<email>[email protected]</email>
<age>25</age>
</user>
<!-- Add 8 more users here -->
<user>
<id>3</id>
<name>Mike Johnson</name>
<email>[email protected]</email>
<age>35</age>
</user>
<user>
<id>4</id>
<name>Emily Davis</name>
<email>[email protected]</email>
<age>28</age>
</user>
<user>
<id>5</id>
<name>Robert Brown</name>
<email>[email protected]</email>
<age>40</age>
</user>
32
<user>
<id>6</id>
<name>Linda Wilson</name>
<email>[email protected]</email>
<age>32</age>
</user>
<user>
<id>7</id>
<name>David Lee</name>
<email>[email protected]</email>
<age>29</age>
</user>
<user>
<id>8</id>
<name>Sarah Taylor</name>
<email>[email protected]</email>
<age>26</age>
</user>
<user>
<id>9</id>
<name>James Anderson</name>
<email>[email protected]</email>
<age>33</age>
</user>
<user>
<id>10</id>
<name>Laura Thomas</name>
<email>[email protected]</email>
<age>27</age>
</user>
</users>
Step 2: Create the JSP Program
We will create a JSP file named userDetails.jsp that will read the XML document
and return user details based on the provided user ID.
userDetails.jsp:
jsp
Copy code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*, javax.xml.parsers.*, org.w3c.dom.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Details</title>
</head>
<body>
<h1>Get User Details</h1>
<form action="userDetails.jsp" method="get">
<label for="userId">Enter User ID:</label>
33
<input type="text" id="userId" name="userId" required>
<input type="submit" value="Get Details">
</form>
<hr/>
<%
String userId = request.getParameter("userId");
if (userId != null && !userId.trim().isEmpty()) {
try {
File xmlFile = new File(application.getRealPath("/users.xml"));
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
if (!userFound) {
out.println("<p>User with ID " + userId + " not found.</p>");
}
} catch (Exception e) {
e.printStackTrace();
out.println("<p>Error processing the request.</p>");
}
}
%>
</body>
34
</html>
Explanation:
Form to Get User ID:
35
Experiment 10
Aim: Write a Servlet/ JSP program which takes value from HTML and print using Servlet/ JSP.
Index.html
<html>
<head>
</head>
<body>
<form name myform action="getParameter" method="post">
<input type="text" name="yourname" value="">
<input type="submit" value="submit">
</form>
</body>
</html>
getParameter.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
36
Experiment 11
a. Directory Structure:
xml
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>JSPServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>JSPServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
</web-app>
c. JSP Page (index.jsp):
jsp
Copy code
37
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="oracle.jdbc.driver.OracleDriver" %>
<html>
<head>
<title>JSP Oracle Database Example</title>
</head>
<body>
<h1>Users List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"your_username", "your_password");
stmt = conn.createStatement();
String sql = "SELECT id, username, password FROM users";
rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
%>
<tr>
<td><%= id %></td>
<td><%= username %></td>
<td><%= password %></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) {
e.printStackTrace(); }
try { if (stmt != null) stmt.close(); } catch (SQLException e) {
e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) {
e.printStackTrace(); }
}
%>
38
</table>
</body>
</html>
Deploy and Run the Application:
39
Experiment 12
To create a JSP (JavaServer Pages) program that prints the multiplication table
of a given number, you can follow these steps:
Directory Structure:
Copy code
your_project/
└── WebContent/
├── index.jsp
└── WEB-INF/
└── web.xml
web.xml Configuration (in WEB-INF/web.xml):
xml
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>JSPServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>JSPServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
</web-app>
JSP Page (index.jsp):
jsp
Copy code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
40
<title>Multiplication Table</title>
</head>
<body>
<h1>Multiplication Table</h1>
<form action="index.jsp" method="get">
<label for="number">Enter a number:</label>
<input type="text" id="number" name="number" required>
<input type="submit" value="Generate Table">
</form>
<hr/>
<c:if test="${not empty param.number}">
<h2>Table for Number: ${param.number}</h2>
<table border="1">
<tr>
<th>Multiplier</th>
<th>Result</th>
</tr>
<%
int number = Integer.parseInt(request.getParameter("number"));
for (int i = 1; i <= 10; i++) {
%>
<tr>
<td><%= number %> x <%= i %></td>
<td><%= number * i %></td>
</tr>
<%
}
%>
</table>
</c:if>
</body>
</html>
The JSP page checks if the number parameter is not empty using the JSTL
<c:if> tag.
If the number parameter is provided, it parses it to an integer.
Generating the Multiplication Table:
The multiplication table for the entered number is generated using a for loop in
JSP.
The table displays the results in a simple HTML table.
Running the Application:
Deploy your project to the Tomcat server.
Start Tomcat and access your JSP page via a web browser, typically at
http://localhost:8080/your_project/index.jsp.
By following these steps, you should have a JSP application that generates and
displays the multiplication table for a given number input by the user.
41
Experiment 13
JSP program without session Management
Index.html
<html>
<body>
<form action = "main.jsp" method = "GET">
First Name: <input type = "text" name = "first_name"> <br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
Main.jsp
withoutcookie.jsp
<html> <head> <title>Reading Cookies</title> </head>
<body>
<center>
<h1>Setting Cookies</h1>
</center>
<p>Name: <%= fname %></p>
</body>
</html>
42
Experiment 14
Index.html
<html>
<body>
</body>
</html>
<%
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name",
request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name",
request.getParameter("last_name"));
Main.jsp
<html>
<head>
<title>Setting Cookies</title>
</head>
<body>
<center>
<h1>Setting Cookies</h1>
</center>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("first_name")%>
</p></li>
43
<li><p><b>Last Name:</b>
<%= request.getParameter("last_name")%>
</p></li>
</ul>
<form action="navigate.jsp" method="post">
<label for="url">Enter URL:</label>
<input type="text" id="url" name="url">
<input type="submit" value="Go">
</form>
</body>
</html>
withCookie.html
<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<center>
<h1>Reading Cookies</h1>
</center>
<%
Cookie cookie = null;
Cookie[] cookies = null;
</html>
44
Experiment 15
Servlet Navigation
Important programs
Index.html
<form method="post" action="Validate">
Name:<input type="text" name="user" /><br/>
Password:<input type="password" name="pass" ><br/>
<input type="submit" value="submit">
</form>
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String name = request.getParameter("user");
String password = request.getParameter("pass");
if(password.equals(“abesit")) {
RequestDispatcher rd =
request.getRequestDispatcher("Welcome");
rd.forward(request, response);
} else {
out.println("<font color='red'><b>You have entered incorrect
password</b></font>");
RequestDispatcher rd =
request.getRequestDispatcher("index.html");
rd.include(request, response); }
}finally {
out.close(); } } }
welcome.java
import java.io.*;
import javax.servlet.*;
45
import javax.servlet.http.*;
public class Welcome extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<h2>Welcome user</h2>");
} finally {
out.close();
} }
}
46