Ay - 2019-20 Web Programming Lab
Ay - 2019-20 Web Programming Lab
BY
Mr. A. RAJESH,
Assistant Professor.
The document must have a paragraph of text that describes your home. Choose atleast three different
phrases (3 to 6 words) of this paragraph and make them change font, font style, color and font size
when the mouse cursor is placed over them. Each of the different phrases must change to different
fonts, font styles, colors and font sizes
<html>
<head>
<title> dynamic fonts </title>
<style type = "text/css">
.regtext { font : 16pt 'Times new Roman'}
.wordtext { color : blue; }
.regtext1 { font : 16pt 'arial' }
.wordtext1 { color : green; }
.regtext2 { font : 16pt 'Roman' }
.wordtext2 { color : red; }
</style>
</head>
<body>
<p class = "regtext" >
My apartment name is
<span class = "wordtext";
onmouseover = "this.style.color = 'red'; this.style.font = 'italic 18pt Times new Roman';"
onmouseout = "this.style.color = 'violet'; this.style.font = 'italic 18pt Times new Roman';">
Golden Grands
</span>
</p>
</span>
with a beautiful garden
</p>
</body>
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 2 of 53
WEB PROGRAMMING LAB
</html>
OUTPUT
and it has 3 bedrooms, 1 living room, 1 kitchen 1 club house with a beautiful garden
<html>
<head>
<title> Dynamic stacking of paragraphs </title>
<script type = "text/javascript">
var oldTop="p1";
function toTop(newTop)
{
var domTop=document.getElementById(oldTop).style;
var domNew=document.getElementById(newTop).style;
domTop.zIndex=0;
domNew.zIndex=10;
oldTop=newTop;
}
</script>
<style type = "text/css">
.para1{ color:brown; position:absolute; top:90px; left:150px;
zIndex=0; background-Color:lime; }
.para2{ color:black; position:absolute; top:130px; left:200px;
zIndex=0; background-Color:orange; }
.para3{ color:blue; position:absolute; top:170px;
left:250px; zIndex=0; background-Color:yellow; }
.para4{ color:white; position:absolute; top:210px; left:300px;
zIndex=0; background-Color:grey; }
</style>
</head>
<body bgcolor = "white">
<p class = "para1" id = "p1" onmouseover = "toTop('p1');" >
Iam sarala, <br> Iam a computer science student. <br> Reg no
102CS12017</br> Iam coming from Bommanahalli.</p>
Save file as .html file. And open file in browser. On movement of mouse, over any paragraph, it will
come on top.
XML File
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="p3.css"?>
<patients><patient>
<name>
<fname> sanjay </fname>
<mname> s</mname>
<lname> Srinivas</lname>
</name>
<insurance type="Secondary">
<id>20 </id>
<address> jayanagar </address>
</insurance>
<medproblems> Bp </medproblems>
<drugallergy>None</drugallergy>
</patient>
<patient>
<name>
<fname> rakshith </fname>
<mname> gm</mname>
<lname> gowda</lname>
</name>
<insurance type="Secondary">
<id>112 </id>
<address> vijayanagar </address>
</insurance>
<patient>
<name>
<fname> rudresh </fname>
<mname> g</mname>
<lname> gowda </lname>
</name>
<insurance type="Secondary">
<id> 114</id>
<address> jp</address>
</insurance>
<medproblems>cancer </medproblems>
<drugallergy>Swelling</drugallergy>
</patient>
<patient>
<name>
<fname> kumar </fname>
<mname> v</mname>
<lname>gowda</lname>
</name>
<ssn> 103</ssn>
<age> 41</age>
<roomnumber> 423 </roomnumber>
<insurance type="Primary">
<id> 113 </id>
p3.css
patient
{
display:block;
margin-left:5%;
border:solid blue 2px;
padding:4px;
}
name
{
display:block;
color:blue;
margin-left:10%;
font-family:bold times new roman;
font-size:25px;
}
ssn
{
display:block;
color:blue;
margin-left:10%;
font-family:arial;
font-size:14px;
}
age
{
display:block;
color:pink;
margin-left:10%;
font-family:Times new roman;
font-size:14px;
}
roomnumber
{
display:block;
color:green;
margin-left:10%;
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 8 of 53
WEB PROGRAMMING LAB
font-family:chiller;
font-size:24px;
}
id
{
display:block;
color:black;
margin-left:10%;
font-family:gigi;
font-size:20px;
}
gn
{
display:block;
color:voilet;
margin-left:10%;
font-family:arial;
font-size:14px;
}
address
{
color:brown;
font-family:script;
font-weight:italic;
font-size:30px;
margin-left:5%;
}
medproblems
{
display:block;
color:orange;
font-family:script;
font-weight:italic;
font-size:30px;
margin-left:5%
}
drugallergy
{
display:block;
color:gray;
font-family:script;
font-weight:bold;
font-size:30px;
margin-left:5%
}
OUTPUT
XML File
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="p4.xsl"?>
<patients><patient>
<name>
<fname> sanjay </fname>
<mname> s</mname>
<lname> Srinivas</lname>
</name>
</patient>
<patient>
<name>
<fname> rakshith </fname>
<mname> gm</mname>
<lname> gowda</lname>
</name>
<ssn> 101 </ssn>
<age> 41</age>
<roomnumber> 421 </roomnumber>
<insurance type="Primary">
<id> 111</id>
<address> jp nagar
</address>
</insurance>
<insurance type="Secondary">
<id>112 </id>
<address> vijayanagar </address>
</insurance>
<patient>
<name>
<fname> rudresh </fname>
<mname> g</mname>
<lname> gowda </lname>
</name>
<insurance type="Secondary">
<id> 114</id>
<address> jp</address>
</insurance>
<medproblems>cancer </medproblems>
<drugallergy>Swelling</drugallergy>
</patient>
<patient>
<name>
<fname> kumar </fname>
<mname> v</mname>
<lname>gowda</lname>
</name>
<ssn> 103</ssn>
<age> 41</age>
<roomnumber> 423 </roomnumber>
<insurance type="Primary">
<id> 113 </id>
<address> malleshwaram. </address>
</insurance>
<insurance type="Secondary">
<id> 115</id>
<address> j.p.v nagar</address>
</insurance>
<medproblems> coma </medproblems>
<drugallergy>None</drugallergy>
</patient>
</patients>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Save file as p4.xsl file
HTML File
<html>
<head>
<title>Preferred Greeting</title>
</head>
<body>
Do you prefer a formal greeting or an informal greeting? <br> >> <a
href="http://localhost/p5.php"> Display greeting </a> <br>
<br>
<br>
click <a href="http://localhost/countvisitors.php">here</a> to know Total no of vistors visited the
site
</body>
</html>
P5.php
<?php
$greeting = array();
$n = mt_rand(0, 4);
echo $greeting[$n];
?>
Open html file in browser to see below links. On click of links random greeting and no of visitors
visited the site can be seen.
Html file
<html>
<head>
<title>Order Form</title>
<script type="text/javascript" >
function loadjson()
{
var json = '{"h1": "<b><h2><pre>Select the items : Quantity</pre></b></h2>"}';
var obj = JSON.parse(json);
document.getElementById("heading").innerHTML=obj.h1;
document.getElementById("heading").innerText=obj.h1;
}
</script>
</head>
<body onload="loadjson();">
<form method="POST" action="http://localhost/p6.php">
User :
<input type="text" name="mname" size=30 /> <br/>
<label id="heading" ></label>
<table>
<TR>
<TD>
<input type="checkbox" name="option1" checked="checked"/>
<label id="chk1">Four 100 watt bulbs for Rs. 20.39 </label>
</TD>
<TD>
<input type="text" name="VAL1" size=2 />
</TD>
</TR>
<TR><TD>
<input type="checkbox" name="option2" />
<label id="chk2">Eight 100watt bulbs for Rs 40.20 </label></TD>
<TR>
<TD>
<input type="checkbox" name="option4" />
<label id="chk4">Eight 100watt long life bulbs for Rs 70.49 </label><br/>
</TD>
<TD>
<input type="text" name="VAL4" size=2/>
</TD>
</TR>
</table>
<input type="radio" name="paymode" value="visa" checked="checked" /> VISA <br/> <input
type="radio" name="paymode" value="mcard"/> MASTER CARD <br/> <input type="radio"
name="paymode" value="discover"/> DISCOVER <br/>
Save above file as .html file in any folder. Also download and save json2.js file in the same
folder location.
p6.php
<html>
<head>Display Form</head>
<body>
<?php
$name=$_POST["mname"];
$mode=$_POST["paymode"];
$item1=0;
$item2=0;
$item3=0;
$item4=0;
$price1=20.39;
$price2=30.95;
$price3=40.20;
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 18 of 53
WEB PROGRAMMING LAB
$price4=70.49;
if(isset($_POST['option1']))
$item1=$_POST['VAL1'];
if(isset($_POST['option2']))
$item2=$_POST['VAL2'];
if(isset($_POST['option3']))
$item3=$_POST['VAL3'];
if(isset($_POST['option4']))
$item4=$_POST['VAL4'];
$total1=$item1*$price1;
$total2=$item2*$price2;
$total3=$item3*$price3;
$total4=$item4*$price4;
$total=$total1+$total2+$total3+$total4;
$total+=($total*13.5)/100;
<table border=1>
<tr><th>Item</th><th>Qty</th><th>Price</th><th>Total</th></tr>
<tr><td>Four 100 watt bulbs for Rs. 20.39</td>
<td><?php print("$item1");?></td><td><?php
print("$price1");?></td> <td><?php print("$total1");?></td></tr>
</table>
</br>
</br>
<?php print("Total bill including 13.5% tax:<b> $total
</b>");?> <?php print("payment mode:<b> $mode </b>");?>
</body>
</html>
Save above file as p6.php and store in htdocs folder of the xampp(eg: C:\xampp\htdocs). Start apache.
Open html file in browser to order the items. On submit of the form one can view ordered items and
total bill generated including 13.5% tax.
p7.html
<html>
<head>
<title> Phone Form </title>
</head>
<body>
<form method="POST" action="http://localhost/p7.php">
Name :
<input type="text" name="uname" size=33 /> <br/>
Telephone Number :
<input type="text" name="phone" size=20 />(ddd-ddd-dddd) <br/>
p7.php file
<?php
$name = $_POST["uname"];
$pnum = $_POST["phone"];
if(preg_match("/^([0-9]{3})-([0-9]{3})-([0-9]{4})$/", $pnum))
{
print "Phone number is valid <br/>";
print "User name is : $name <br/>";
print "Phone number is : $pnum <br/>";
}
else
print "Phone number is not valid!";
?>
Save above file as p7.php in htdocs of xampp folder(eg: C:\xampp\htdocs\p7.php)
Start apache from xampp control panel.
Open html in browser. Enter details in html and submit. It will validate the date format as below.
HTML File
<!DOCTYPE html>
<html>
<body>
<head>
<script type="text/javascript">
function save()
{
uname=document.getElementById("uname").value;
phone=document.getElementById("uphone").value;
email=document.getElementById("email").value;
var myObj = { "name" : ""+ uname +"", "phone":"" + phone + "", "email" : "" + email + "" };
myJSON = JSON.stringify(myObj);
window.location = "http://localhost/p8.php?x=" + myJSON;
}
</script>
</head>
<form>
<h3> Student Information </h3>
<h3> Name : <input type="text" id="uname" /> </h3>
<h3> Phone : <input type="text" id="uphone" /> </h3>
<h3> Email : <input type="text" id="email" /> </h3>
</form>
</body>
</html>
p8.php file
<?php
$data = $_GET['x'];
$data_decode = json_decode($data);
$name = $data_decode->name;
$phone=$data_decode->phone;
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 23 of 53
WEB PROGRAMMING LAB
$email= $data_decode->email;
echo "<br/> <h4> Information Entered </h4>";
echo "------------------------------<br/>";
print "Name : ".$name."<br/>";
print "Phone : ".$phone."<br/>";
print "Email : ".$email."<br/>";
echo "------------------------------<br/>";
// Create connection
$link = mysqli_connect("localhost", "root", "","test");
// Check connection
if (!$link)
{
die("Connection failed: " . mysqli_connect_error());
}
mysqli_select_db($link, "test");
$query1 = "CREATE TABLE tb_text_book(uname varchar(15), phone varchar(30), email char(30))";
$result = mysqli_query($link,$query1);
if($result)
echo "(2) Query1 executed... <br/>";
else
echo "(2) Table not created : ".mysqli_error($link)."<br/>";
mysqli_close($link);
?>
Program 9
Write a servlet that returns a randomly chosen greeting from a list of five different greetings.
The greeting must be stored as constant strings in the program.
To create servlet Click on Source Packages New Servlet
p9.java file
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class p9 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String[] myStrings = {"This is the first servlet message", "This is the first servlet message", "Good
Morning", "Have a nice day","Hello"}; out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Random Message</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet RandomMessage</h1>");
Eg: http://localhost:8080/RandomGreetings/p9
Output
A servlet for the XHTML code of exercise 6 that computes the total cost of ordered light bulbs
after adding 2% sales tax. The servlet must inform the buyer of exactly what was ordered in
table
Create web application (in below example Examples web application is created) Create
html file as below
Click on finish
</script>
</head>
<body onload="loadjson();">
<form method="GET" action="http://localhost:8080/Examples/p10">
User :
<input type="text" name="mname" size=30 /> <br/>
<label id="heading" ></label>
<table>
<TR>
<TD>
<input type="checkbox" name="option1" checked="checked"/>
<label id="chk1">Four 100 watt bulbs for Rs. 20.39 </label>
</TD>
<TD>
<input type="text" name="VAL1" size=2 />
</TD>
</TR>
<TR><TD>
<input type="checkbox" name="option2" />
<label id="chk2">Eight 100watt bulbs for Rs 40.20 </label></TD> <TD>
<input type="text" name="VAL2" size=2/>
</TD>
</TR>
<TR>
<TD>
<input type="checkbox" name="option3" />
<label id="chk3">Four 100watt long life bulbs for Rs. 30.95 </label>
</TD>
<TD>
<input type="text" name="VAL3" size=2/>
</TD>
</TR>
<TR>
<TD>
<input type="checkbox" name="option4" />
<label id="chk4">Eight 100watt long life bulbs for Rs 70.49 </label><br/>
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 35 of 53
WEB PROGRAMMING LAB
</TD>
<TD>
<input type="text" name="VAL4" size=2/>
</TD>
</TR>
</table>
<input type="radio" name="paymode" value="visa" checked="checked" /> VISA <br/> <input
type="radio" name="paymode" value="mcard"/> MASTER CARD <br/> <input type="radio"
name="paymode" value="discover"/> DISCOVER <br/>
p10.java file
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class p10 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");//setting the content type PrintWriter
pw=res.getWriter();//get the stream to write the data
String UName=req.getParameter("mname");
double item1,item2,item3,item4;
double price1=20.39;
double price2=40.20;
double price3=30.95;
double price4=70.49;
item1=item2=item3=item4=0;
if(req.getParameter("option1")!=null)
{
String str1=req.getParameter("VAL1");
item1=Integer.parseInt(str1);
}
if(req.getParameter("option2")!=null)
{
String str2=req.getParameter("VAL2");
item2=Integer.parseInt(str2);
}
if(req.getParameter("option3")!=null)
{
String str3=req.getParameter("VAL3");
item3=Integer.parseInt(str3);
}
if(req.getParameter("option4")!=null)
{
String str4=req.getParameter("VAL4");
item4=Integer.parseInt(str4);
}
double total1=item1*price1;
double total2=item2*price2;
double total3=item3*price3;
double total4=item4*price4;
double total=total1+total2+total3+total4;
total+=(total*2.0)/100;
pw.println("<!DOCTYPE html>");
pw.println("<html>");
String itemname1,itemname2,itemname3,itemname4;
itemname1="Four 100 watt bulbs for Rs. 20.39"; itemname2="Eight
100watt bulbs for Rs 40.20"; itemname3="Four 100watt long life bulbs
for Rs. 30.95"; itemname4="Eight 100watt long life bulbs for Rs
70.49";
Write and test a JSP document that displays the form of exercise 6 and produces the same
response document as exercise 10.
p11.html
<html>
<head>
<title>Order Form</title>
<script type="text/javascript">
function loadjson()
{
var json = '{"h1": "<b><h2><pre>Select the items : Quantity</pre></b></h2>"}';
var obj = JSON.parse(json);
document.getElementById("heading").innerHTML=obj.h1;
document.getElementById("heading").innerText=obj.h1;
}
</script>
</head>
<body onload="loadjson();">
<form method="GET" action="http://localhost:8080/Examples/p11.jsp">
User :
<input type="text" name="mname" size=30 /> <br/>
<label id="heading" ></label>
<table>
<TR>
<TD>
<input type="checkbox" name="option1" checked="checked"/>
<label id="chk1">Four 100 watt bulbs for Rs. 20.39 </label>
</TD>
<TD>
<input type="text" name="VAL1" size=2 />
</TD>
</TR>
<TR><TD>
<input type="checkbox" name="option2" />
<label id="chk2">Eight 100watt bulbs for Rs 40.20 </label></TD> <TD>
<input type="text" name="VAL2" size=2 />
</TD>
</TR>
<TR>
<TD>
<input type="checkbox" name="option3" />
<label id="chk3">Four 100watt long life bulbs for Rs. 30.95 </label>
</TD>
<TD>
<input type="text" name="VAL3" size=2 />
</TD>
</TR>
<TR>
<TD>
<input type="checkbox" name="option4" />
<label id="chk4">Eight 100watt long life bulbs for Rs 70.49 </label><br/>
</TD>
<TD>
<input type="text" name="VAL4" size=2 />
</TD>
</TR>
</table>
<input type="radio" name="paymode" value="visa" checked="checked" /> VISA <br/> <input type="radio"
name="paymode" value="mcard"/> MASTER CARD <br/> <input type="radio" name="paymode"
value="discover"/> DISCOVER <br/>
p11.jsp
String itemname1,itemname2,itemname3,itemname4;
itemname1="Four 100 watt bulbs for Rs. 20.39"; itemname2="Eight
100watt bulbs for Rs 40.20"; itemname3="Four 100watt long life bulbs
for Rs. 30.95"; itemname4="Eight 100watt long life bulbs for Rs
70.49";
%>
Write a markup document to create a form that collects favorite popular songs, including the name
of the song, the composer and the performing artist or group. This document must call a servlet
when the form is submitted and another servlet to request a current list of survey results
import java.io.*;
import java.io.FileWriter;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.FileReader;
String sname=req.getParameter("sname");
String cname=req.getParameter("cname");
String singer=req.getParameter("singer");
DEPT. OF COMPUTER SCIENCE & ENGINEERING, MCET Page 45 of 53
WEB PROGRAMMING LAB
String artist="";
if(req.getParameter("fluteman")!=null)
{
artist=" Fluteman";
}
if(req.getParameter("tabalaman")!=null)
{
artist=artist + " Tabalaman";
}
if(req.getParameter("violinman")!=null)
{
artist=artist + " Violinman";
}
if(req.getParameter("supportingsinger")!=null)
{
artist=artist + " Supportingsinger";
}
String filename="d:\\song.txt";
FileWriter fw=new FileWriter(filename,true);
fw.write("\n");
try
{
fw.write("Song:" + sname + " Composer:"+ cname + " Singer:" + singer + " Artist:" + artist);
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
pw.println("<!DOCTYPE html>");
pw.println("<html>");
pw.println("<body>");
pw.println("<h2>Current Survey Result </h2>");
pw.println("</br>Song Name: " + sname + "</br> Composer :"+ cname + "</br> Singer :" + singer + "</br>
Artist: "+ artist);
pw.println("</br></br><a href='http://localhost:8080/Examples/survey'>click here </a> to view
previous survey details");
pw.println("</body></html>");
import java.io.*;
import java.io.FileWriter;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.FileReader;
String filename="d:\\song.txt";
pw.println("<!DOCTYPE html>");
pw.println("<html>");
pw.println("<body>");
pw.println("<h2>Previous survey result<h2>");
pw.println("<b>The most popular songs are </b> " );
pw.println("<br />");
}
br.close();
pw.println("</table>");
pw.println("</body></html>");
Right click on html file select Run File. It shows the below form
Program 13
Create a simple Java web application using Servlet and JDBC HTML File
import java.io.IOException;
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 java.io.*;
import java.sql.DatabaseMetaData;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class p13 extends HttpServlet
{
pw.println("<!DOCTYPE html>");
pw.println("<html>");
pw.println("<head>");
pw.println("<title>SERVLET DJBC CONNECTIVITY </title>");
pw.println("</head>");
pw.println("<body>");
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
}
catch(ClassNotFoundException e)
{
pw.print("class not found");
}
catch(SQLException e)
{
pw.print("conn failed ");
}
catch(Exception e)
{
pw.print("error");
}
try
{
String usr = request.getParameter("user");
String mail = request.getParameter("email");
String ph = request.getParameter("phone");
//Display values
pw.print("<tr>");
pw.print("<td> " + UserName + "</td><td> " + phone + "</td><td> " + email +
"</td>");
pw.print("</tr>");
}
pw.print("</table>");
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
pw.println("</body>");
pw.println("</html>");
}
}
Download and add sql jar file in Libraries (mysql-connector-java-5.1.18-bin) as below for
connecting to database