SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input,...
Transcript of SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input,...
![Page 1: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/1.jpg)
SREE CHAITANYA COLLEGE OF ENGINEERING
COMPUTER SCIENCE & ENGINEERING
WT LAB MANUAL ( R13 REGULATION)
1. Install the following on the local machine
a. Apache Web Server
b. Tomcat Application Server locally
c. Install MySQL
d. Install PHP and configure it to work with Apache web server and MySQL
2. Write an HTML page including any required Javascript that takes a number from one text field in the range
of 0 to 999 and shows it in another text field in words. If the number is out of range, it should show “out of
range” and if it is not a number, it should show “not a number” message in the result box.
CODE:
<html>
<head>
<title> WT Lab manual program no. 1 </title>
</head>
<body>
<form name="myform">
<center>
<h1> WT Lab manual program no. 1 </h1>
Enter a number <input type="text" name="tfield1" title="0 to 999" id="tfield1" /><br><br>
<input type="button" value="Click Here! " onClick="myFunction()"> <br><br>
Display Field <input type="text" id="tfield2" name="tfield2" readonly="true">
</center>
</form>
<script>
function myFunction()
{
Var inwords = ["zero","one","two","three","four","five","six","seven","eight","nine"];
var a=document.getElementById("tfield1").value;
var sp=a.split('');
var nlength=a.toString().length;
document.getElementById("tfield2").value=null;
if(isNaN(a))
{
//window.alert("not a number");
//document.write("Not a Number");
document.getElementById("tfield2").value="not a number";
}
![Page 2: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/2.jpg)
else
{
if(a>999 || a<0)
{
//document.write("Out of range");
//window.alert("out of range");
document.getElementById("tfield2").value="out of range";
}
else
{
var i=0;
var b=sp[i];
var c=sp[i+1];
var d=sp[i+2];
if(nlength==1)
document.getElementById("tfield2").value=inwords[b];
else if(nlength==2)
document.getElementById("tfield2").value=inwords[b]+" "+inwords[c];
else if(nlength==3)
document.getElementById("tfield2").value=inwords[b]+" "+inwords[c]+" "+inwords[d];
}
}
}
</script>
</body>
</html>
OUTPUT:
CASE 1:
![Page 3: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/3.jpg)
CASE 2:
CASE 3:
![Page 4: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/4.jpg)
3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user
clicks on submit button, it should show number of characters, words and lines in the text entered using an
alert message. Words are separated with white spaces and lines are separated with new line character.
CODE:
<html>
<head> <title>WT Lab manual program no. 2</title> </head>
<body>
<form name="myform2">
<center>
<h1> WT Lab manual program no. 2 </h1>
Enter a multi-line text: <textarea cols="50" rows="5" name="tfiled1" title="enter multi-line text"
id="tfield1"></textarea><br /><br />
<input type="button" value="Click Here!" onClick="myFunction()" />
</center>
</form>
<script>
function myFunction()
{
var text=document.getElementById("tfield1").value;
var tlength=text.toString().length;
var sp=text.split('');
var wordcount=0;
for(var i=0; i<tlength;i++)
{
if(sp[i]==" " || sp[i]=="\n")
{
wordcount++;
}
}
if(i!=0)
wordcount++;
var lines=0;
for(var j=0; j<tlength;j++)
{
if(sp[j]=="\n")
{
lines++;
}
}
if(j!=0)
lines++;
window.alert("Number of characters in text are: "+tlength+"\n"+"Number of words in text are:
"+wordcount+"\n"+"Number of lines in text are: "+lines);
}
</script>
</body>
</html>
![Page 5: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/5.jpg)
OUTPUT:
![Page 6: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/6.jpg)
4. Write an HTML page that contains a selection box with a list of 5 countries. When the user selects a country, its
capital should be printed next in the list. Add CSS to customize the properties of the font of the capital (color,
bold and font size).
CODE:
<html>
<head>
<title>WT Lab manual program no. 3</title>
</head>
<style>
h1
{
color: orange;
text-align: center;
}
.textbox1
{
color: orange;
font-size: 20px;
font-weight: bold;
}
</style>
<body>
<center>
<h1> WT Lab manual program no. 3 </h1>
<form name="myform">
Select Country <select name="country" id="sbox1" onClick="myFunction()" required>
<option value=""></option>
<option value="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;
![Page 7: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/7.jpg)
}
</script>
</body>
</html>
OUTPUT:
![Page 8: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/8.jpg)
5. Create an XML document that contains 10 users information. Write a Java Program, which takes User Id as
input and returns the user details by taking the user information from XML document using (a) DOM parser
and (b) SAX parser.
CODE:
users.xml
<usersinformation>
<user>
<rollno>501</rollno>
<name>aaa</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>502</rollno>
<name>bbb</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>503</rollno>
<name>ccc</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>504</rollno>
<name>ddd</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>505</rollno>
<name>eee</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>506</rollno>
<name>fff</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>507</rollno>
<name>ggg</name>
<branch>cse</branch>
<college>scce</college>
</user>
![Page 9: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/9.jpg)
<user>
<rollno>508</rollno>
<name>hhh</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>509</rollno>
<name>iii</name>
<branch>cse</branch>
<college>scce</college>
</user>
<user>
<rollno>510</rollno>
<name>jjj</name>
<branch>cse</branch>
<college>scce</college>
</user>
</usersinformation>
Wt5dom.java
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.Scanner;
public class Wt5dom
{
public static void main(String args[]) throws Exception
{
DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance();
DocumentBuilder b=fac.newDocumentBuilder();
Document doc=b.parse(new File("users.xml"));
doc.getDocumentElement().normalize();
Element root=doc.getDocumentElement();
Scanner in=new Scanner(System.in);
System.out.println("Enter User ID:");
int n=in.nextInt();
int flag=0;
NodeList nl=doc.getElementsByTagName("user");
for(int i=0;i<nl.getLength();i++)
![Page 10: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/10.jpg)
{
Node node=nl.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element e=(Element)node;
int
x=Integer.parseInt(e.getElementsByTagName("rollno").item(0).getTextContent());
if(x==n)
{
System.out.println(root.getNodeName());
System.out.println("--------------------------------");
System.out.println("rollno:\t"+e.getElementsByTagName("rollno").item(0).getTextContent());
System.out.println("name:\t"+e.getElementsByTagName("name").item(0).getTextContent());
System.out.println("branch:\t"+e.getElementsByTagName("branch").item(0).getTextContent());
System.out.println("college:"+e.getElementsByTagName("college").item(0).getTextContent());
flag=1;
break;
}
else
{
flag=0;
}
}
}
if(flag==0)
System.out.println("User not available");
}
}
OUTPUT:
![Page 11: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/11.jpg)
6. Implement the following web applications using (a) PHP, (b) Servlets and (c) JSP.
6 (i) (a). A user validation web application, where the user submits the login name and password to the server. The
name and password are checked against the data already available in Database and if the data matches, a
successful login page is returned. Otherwise failure message is shown to the user.
USING PHP
CODE:
db.php <html>
<body>
<?php
$severname="localhost";
$username="root";
$password="";
$conn=new mysqli($severname,$username,$password);
if($conn->connect_error)
{
die("connection failed".$conn->connect_error);
}
echo "Connected successfully <br>";
//Create database
$sql = "CREATE DATABASE reg";
if(mysqli_query($conn,$sql))
echo "Database created successfully<br>";
else
echo "error";
$servername="localhost";
$dbname="reg";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br>";
// sql to create table
$sql = "CREATE TABLE Guests (
name VARCHAR(30) NOT NULL,
pwd VARCHAR(30) NOT NULL)";
if (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
![Page 12: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/12.jpg)
$sql = "INSERT INTO Guests (name, pwd)
VALUES ('cse', '5')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
OUTPUT OF db.php:
Login6i.html
<html>
<head> <title> Login Page </title>
<body>
<center>
<h1> Login Page </h1>
<form action="loginform.php" method="post">
<table>
<tr> <td> <label> Name: </label> </td>
<td> <input type="text" name="uname" /> </td>
</tr>
<tr> <td> <label> Password: </label> </td>
<td> <input type="password" name="upwd" /> </td>
</tr>
<tr> <td> <input type="submit" value="submit" /> </td>
<td> <input type="reset" value="reset" /> </td>
</tr>
</table>
</form>
</center>
</body>
</html>
![Page 13: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/13.jpg)
loginform.php
<html>
<head> <title> Registration page </title> </head>
<body>
<?php
$name=$_POST["uname"];
$pwd=$_POST["upwd"];
$conn=mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("reg") or die("mysql_error()");
$query=mysql_query("SELECT * from guests where name='$name'");
while($row=mysql_fetch_array($query))
{
$duser=$row['name'];
$dpwd=$row['pwd'];
}
if($pwd==$dpwd && $name==$duser)
echo "welcome $name branch";
else
echo "invalid user";
?>
</body>
</html>
OUTPUT:
![Page 14: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/14.jpg)
6 (ii) (a). Modify the above program to use an xml instead of database
CODE:
login6ii.html
<htm`l>
<head> <title> Login Page </title>
<body>
<center>
<h1> Login Page </h1>
<form action="loginform6ii.php" method="post">
<table>
<tr> <td> <label> Name: </label> </td>
<td> <input type="text" name="uname" /> </td>
</tr>
<tr> <td> <label> Password: </label> </td>
<td> <input type="password" name="upwd" /> </td>
</tr>
<tr> <td> <input type="submit" value="submit" /> </td>
<td> <input type="reset" value="reset" /> </td>
</tr>
</table>
</form>
</center>
</body>
</html>
Userlogin.xml
<Authentication>
<user>
<username>cse</username>
<password>5</password>
</user>
<user>
<username>eee</username>
<password>2</password>
</user>
</Authentication>
loginform6ii.php <html>
<head> <title> Registration page 6ii </title> </head>
<body>
<?php
$myxml=simplexml_load_file("userlogin6ii.xml");
$username=$_POST['uname'];
$password=$_POST['upwd'];
$xmlusername="";
$xmlpassword="";
![Page 15: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/15.jpg)
for($i=0;$i<count($myxml);$i++)
{
$xmlusername=$myxml->user[$i]->username;
$xmlpassword=$myxml->user[$i]->password;
if($xmlusername==$username && $xmlpassword==$password)
{
echo "welcome $username";
die();
}
}
echo "Invalid username or password";
?>
</body>
</html>
OUTPUT:
![Page 16: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/16.jpg)
6 (iii) a. Modify the above program to use AJAX to show the result on the same page below the submit button.
<html>
<head>
<script type="text/javascript">
var xmlHttp;
function ajaxFunction() {
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4 && xmlHttp.status==200) {
document.myForm.time.value=xmlHttp.responseText;
}
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
</script>
</head>
<body>
<center>
<form name="myForm">
Name:<input type="text" name="username" onkeyup='ajaxFunction()'/><br><br>
<input type="submit" value="submit"><br><br>
Time: <input type="text" name="time"/>
</form>
</center>
</body>
</html>
OUTPUT:
![Page 17: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/17.jpg)
6 (iv) (a). A simple calculator web application that takes two numbers and an operator (+,-,/,* and %) from an
HTML page and returns the result page with the operation performed on the operands.
USING PHP
CODE:
Cal6iv.html <html>
<head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="cal6iv.php" method="post">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="submit" value="+" name="add">
<input type="submit" value="-" name="sub">
<input type="submit" value="*" name="mul">
<input type="submit" value="/" name="div">
<input type="submit" value="%" name="mod">
</form>
</center>
</body>
</html>
Cal6iv.php
<html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
$x=$_POST['v1'];
$y=$_POST['v2'];
if(isset($_POST['add']))
{
echo "<h1> The result is ",$x+$y,"</h1>";
}
else if(isset($_POST['sub']))
{
echo "<h1> The result is ",$x-$y,"</h1>";
}
else if(isset($_POST['mul']))
{
echo "<h1> The result is ",$x*$y,"</h1>";
}
else if(isset($_POST['div']))
{
![Page 18: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/18.jpg)
echo "<h1> The result is ",$x/$y,"</h1>";
}
else if(isset($_POST['mod']))
{
echo "<h1> The result is ",$x%$y,"</h1>";
}
?>
</center>
</body>
</html>
OUTPUT:
![Page 19: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/19.jpg)
6 (v) (a). Modify the above program such that it stores each query in a database and checks the database first for
the result. If the query is already available in DB, it returns the value that was previously computed (from DB) or it
computes the result and returns it after storing the new query and result in DB.
USING PHP
CODE:
db6v.php
<html>
<body>
<?php
$severname="localhost";
$username="root";
$password="";
$conn=new mysqli($severname,$username,$password);
if($conn->connect_error)
{
die("connection failed".$conn->connect_error);
}
echo "Connected successfully <br>";
//Create database
$sql = "CREATE DATABASE calc_data";
if(mysqli_query($conn,$sql))
{
echo "Database created successfully<br>";
}
else
{
echo "error";
}
$servername="localhost";
$dbname="calc_data";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br>";
// sql to create table
$sql = "CREATE TABLE calc_queires (
v1 VARCHAR(30) NOT NULL,v2 VARCHAR(30) NOT NULL,oper VARCHAR(3), res VARCHAR(30) )";
if (mysqli_query($conn, $sql))
echo "Table MyGuests created successfully<br>";
else
echo "Error creating table: " . mysqli_error($conn);
![Page 20: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/20.jpg)
$sql = "INSERT INTO calc_queires (v1,v2,oper,res) VALUES ('3', '3','+','6')";
if (mysqli_query($conn, $sql))
echo "New record created successfully";
else
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
mysqli_close($conn);
?>
</body>
</html>
OUTPUT OF db6v.php:
cal6v.html
<html>
<head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="cal6v.php" method="post">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="submit" value="+" name="add">
<input type="submit" value="-" name="sub">
<input type="submit" value="*" name="mul">
<input type="submit" value="/" name="div">
<input type="submit" value="%" name="mod">
</form>
</center>
</body>
</html>
cal6v.php
<html>
<head> <title> Result </title> </head>
![Page 21: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/21.jpg)
<body>
<center>
<?php
$x=$_POST['v1'];
$y=$_POST['v2'];
if(isset($_POST['add']))
{
$op='+';
$res=$x+$y;
}
else if(isset($_POST['sub']))
{
$op='-';
$res=$x-$y;
}
else if(isset($_POST['mul']))
{
$op='*';
$res=$x*$y;
}
else if(isset($_POST['div']))
{
$op='/';
$res=$x/$y;
}
else if(isset($_POST['mod']))
{
$op='%';
$res=$x%$y;
}
$dbc=mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("calc_data") or die("mysql_error()");
$query=mysql_query("SELECT * from calc_queires where v1='$x' and v2='$y' and oper='$op'");
while($row=mysql_fetch_assoc($query))
{
$val1=$row['v1'];
$val2=$row['v2'];
$oper=$row['oper'];
$result=$row['res'];
}
if($val1==$x && $val2==$y && $op==$oper)
{
echo "<h1> The result is ",$result,"</h1>";
}
else
{
$write=mysql_query("INSERT INTO calc_queries (v1,v2,oper,res) VALUES
![Page 22: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/22.jpg)
('$x','$y','$op','$res')");
echo "</h1> The result is",$res,"</h1>";
}
?>
</center>
</body>
</html>
OUTPUT:
![Page 23: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/23.jpg)
6 (vi) (a). A web application that takes a name as input and on submit it shows a hello <name> page where name is
taken from the request. It shows the start time at the right top corner of the page and provides a logout button.
On clicking this button, it should show a logout page with Thank You <name > message with the duration of usage
(hint: Use session to store name and time).
USING PHP
CODE:
session.html <html>
<head> <title> SESSION LOGIN </title> </head>
<body>
<center>
<form action="sessionlogin.php" method="post">
Enter Name: <input type="text" name="uname"> <br>
<input type="submit" value="LOGIN" name="register">
</form>
</center>
</body>
</html>
sessionlogin.html <html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
session_start();
$name=$_POST['uname'];;
$_SESSION['uname']=$name;
$time=time();
echo "<p align='right'> $time </p>";
echo "Hello"." ".$name." "."You are logged in";
$_SESSION['time']=time();
?>
<form action="sessionlogout.php" method="post">
<input type="submit" value="LOGOUT" name="log">
</form>
</center>
</body>
</html>
![Page 24: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/24.jpg)
sessionlogin.html <html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
session_start();
$nowSince=time()-$_SESSION['time'];
$name=$_SESSION['uname'];
echo "<h1> Thank you $name </h1>"."The duration of your session is"," ",$nowSince."
"."seconds";
session_destroy();
?>
</center>
</body>
</html>
OUTPUT:
![Page 25: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/25.jpg)
6 (vii) (a). A web application that takes name and age from an HTML page. If the age is less than 18, it should
send a page with “Hello <name>, you are not authorized to visit the site” message, where <name> should be
replaced with the entered name. Otherwise it should send “Welcome <name> to this site” message.
USING PHP
CODE:
usereligible.html
<html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<h1> USER ELIGIBILITY </h1>
<form action="eligibilitycheck.php" method="post">
<input type="text" name="uname" placeholder="Enter your name"> <br>
<input type="text" name="uage" placeholder="Enter your age"> <br>
<input type="submit" vvalue="submit" name="sub"<>
</form>
</center>
</body>
</html>
eligibilitycheck.php
<html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
$age=$_POST['uage'];
$name=$_POST['uname'];
if($age<=18)
{
echo "<h1>Hello"." ".$name." "."you are not authorized to this site</h1>";
}
else
{
echo "<h1> welcome".$name."to this site</h1>";
}
?>
</center>
</body>
![Page 26: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/26.jpg)
</html>
OUTPUT:
![Page 27: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/27.jpg)
6 (viii) a. A web application for implementation:
The user is first served a login page which takes user’s name and password. After submitting the details the
server checks these values against the data from a database and takes the following decisions
If name and password matches, serves a welcome page with user’s full name .
If name matches and password doesn’t match, then serves “password mismatch” page.
If name is not found in the database, serves a registration page, where user’s full name is asked and on
submitting the full name, it stores, the login name, password and full name in database (hint: use session for
storing the submitted login name and password)
USING PHP
CODE:
Db.php
<html>
<body>
<?php
$severname="localhost";
$username="root";
$password="";
$conn=new mysqli($severname,$username,$password);
if($conn->connect_error)
{
die("connection failed".$conn->connect_error);
}
echo "Connected successfully <br>";
//Create database
$sql = "CREATE DATABASE reg6viii";
if(mysqli_query($conn,$sql))
{
echo "Database created successfully<br>";
}
else
{
echo "error";
}
![Page 28: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/28.jpg)
$servername="localhost";
$dbname="reg6viii";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br>";
// sql to create table
$sql = "CREATE TABLE Guests (
name VARCHAR(30) NOT NULL,
pwd VARCHAR(30) NOT NULL,
fname VARCHAR(50) NOT NULL)";
if (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
$sql = "INSERT INTO Guests (name, pwd, fname)
VALUES ('cse', '5','compuer science and engineering')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
Login6viiia.html <html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<h1> LOGIN PAGE </h1>
<form action="login6viiia.php" method="post">
<input type="text" name="uname" placeholder="Enter your name"> <br>
<input type="password" name="pass" placeholder="Enter your age"> <br>
<input type="submit" value="Login" name="login">
![Page 29: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/29.jpg)
</form>
</center>
</body>
</html>
Login6viiia.php <html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
$name=$_POST['uname'];
$pwd=$_POST['pass'];
$dbc=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("reg6viii") or die(mysql_error());
$query=mysql_query("select * from guests where name='$name'");
while($row=mysql_fetch_assoc($query))
{
$dbuser=$row['name'];
$dbpwd=$row['pwd'];
$dbfull=$row['fname'];
}
if($name==$dbuser && $pwd==$dbpwd)
{
echo "Welcome\t<h1>",$dbfull,"</h1>\tyou are successfully logged in ";
}
else
{
echo "<h1> User doesn't exist </h1>"."<a href='reg6viiia.html'> Click here to register </a>";
}
?>
</center>
</body>
</html>
![Page 30: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/30.jpg)
reg6viiia.html
<html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<h1> REGISTRATION PAGE </h1>
<form action="reg6viiia.php" method="post">
User Name:<input type="text" name="uname" placeholder="Enter your name"> <br>
Password:<input type="password" name="pass"> <br>
Full Name:<input type="text" name="fname" placeholder="Enter your name"> <br>
<input type="submit" value="Register" name="register">
</form>
</center>
</body>
</html>
reg6viiia.php
<html>
<head> <title> Result </title> </head>
<body>
<center>
<?php
$name=$_POST['uname'];
$pwd=$_POST['pass'];
$fname=$_POST['fname'];
$dbc=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("reg6viii") or die(mysql_error());
$write=mysql_query("insert into guests values ('$name','$pwd','$fname')");
if(isset($write))
{
echo "<h1> You are registered successfully</h1>"."<a href='login6viiia.html'> Click here to login</a>";
}
?>
</center>
</body>
</html>
![Page 31: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/31.jpg)
OUTPUT:
CASE 1:
CASE 2:
![Page 32: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/32.jpg)
![Page 33: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/33.jpg)
6(ix) a. A web application that lists all cookies stored in the browser on clicking “List Cookies” button. Add cookies
if necessary.
USING PHP
Cookieset.php <html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<?php
$exp=time()+86400;
setcookie("CSE LAB","FIRST FLOOR",$exp);
setcookie("MCA LAB","GROUND FLOOR",$exp);
setcookie(("ECE LAB","SECOND FLOOR",$exp);
?>
</center>
</body>
</html>
Cookie.php
<html>
<head> <title> Cookie </title> </head>
<body>
<center>
<form action="" method="post">
<input type="submit" value="LIST COOKIES" name="list">
</form>
</center>
<?php
error_reporting(0);
if($_POST['list'])
{
foreach($_COOKIE as $key=>$val)
{
echo "<center>".$key."is at".$val."in SCCE <br> </center>";
}
}
?>
</body>
</html>
OUTPUT:
![Page 34: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/34.jpg)
6. Implement the following web applications using (a) PHP, (b) Servlets and (c) JSP.
6 (i) (b). A user validation web application, where the user submits the login name and password to the server.
The name and password are checked against the data already available in Database and if the data matches, a
successful login page is returned. Otherwise failure message is shown to the user.
NOTE: The following Servlets and JSP programs used MS Access database
USING SERVLET
CODE:
login6ib.html <html>
<body>
<form method="post" action="log">
<center>
<table>
<tr>
<td> username: </td>
<td> <input type="text" name="uname"> </td>
</tr>
<tr>
<td> password </td>
<td> <input type="password" name="pwd"> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="login">
<input type="reset" value="reset">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
Loginservlet6ib.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class LoginServlet6ib extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
![Page 35: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/35.jpg)
process(req,res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
process(req,res);
}
public void process(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
try
{
String un=req.getParameter("uname");
String pass=req.getParameter("pwd");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:scce");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM guests1");
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println();
String dbun,dbpwd;
while(rs.next())
{
dbun = rs.getString(2);
dbpwd = rs.getString(3);
if(dbun.equals(un) && dbpwd.equals(pass))
{
pw.println("<h1> Hi "+un+"..successfully logged in</h1>");
break;
}
else if(dbun.equals(un))
{
pw.println("<h1> Invalid password</h1>");
break;
}
else if(dbpwd.equals(pass))
{
pw.println("<h1> Invalid username</h1>");
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
![Page 36: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/36.jpg)
}
}
web.xml <?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>hello1</servlet-name>
<servlet-class>LoginServlet6ib</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello1</servlet-name>
<url-pattern>/log</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
![Page 37: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/37.jpg)
6 (ii) (b). Modify the above program to use an xml instead of database
USING SERVLETS
Login6iib.html <html>
<body>
<center>
<form method="post" action="init1">
<table>
<tr>
<td> username: </td>
<td> <input type="text" name="uname"> </td>
</tr>
<tr>
<td> password </td>
<td> <input type="password" name="pwd"> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="login">
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
InitParamServlet6iib.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class InitParamServlet6iib extends HttpServlet
{
String u,p;
public void init(ServletConfig config) throws ServletException
{
u=config.getInitParameter("un");
p=config.getInitParameter("pwd");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
String un=req.getParameter("uname");
![Page 38: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/38.jpg)
String pass=req.getParameter("pwd");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
if(u.equals(un) && p.equals(pass))
out.println("<h1> Hi "+un+" Successfully logged in");
else
out.println("<h1> Hi "+un+" Invalid username or password");
}
}
web.xml
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>hello2</servlet-name>
<servlet-class>InitParamServlet6iib</servlet-class>
<init-param>
<param-name> un </param-name>
<param-value> cse </param-name>
</init-param>
<init-param>
<param-name> pwd </param-name>
<param-value> 5 </param-name>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello2</servlet-name>
<url-pattern>/init1</url-pattern>
</servlet-mapping>
</web-app>
Output
![Page 39: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/39.jpg)
6 (iv) (b) . A simple calculator web application that takes two numbers and an operator (+,-,/,* and %) from an
HTML page and returns the result page with the operation performed on the operands.
USING SERVLET
CODE:
Cal6ivb.html <html> <head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="calc" method="post">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="radio" value="+" name="add">
<input type="radio" value="-" name="sub">
<input type="radio" value="*" name="mul">
<input type="radio" value="/" name="div">
<input type="radio" value="%" name="mod">
</form>
</center>
</body>
</html>
CalcServlet6ivb.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class CalcServlet6ivb extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
process(req,res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
process(req,res);
}
public void process(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
res.setContentType("text/html");
try
![Page 40: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/40.jpg)
{
PrintWriter out=res.getWriter();
int n01,n02;
String op;
int result=0;
n01=Integer.parseInt(req.getParameter("v1"));
n02=Integer.parseInt(req.getParameter("v2"));
op=req.getParameter("add");
if(op.equals("+"))
result=n01+n02;
if(op.equals("-"))
result=n01-n02;
if(op.equals("*"))
result=n01*n02;
if(op.equals("/"))
result=n01/n02;
if(op.equals("%"))
result=n01%n02;
out.println("<h1> Result= "+result+"</h1>");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>calulatoriv</servlet-name>
<servlet-class>CalcServlet6ivb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>calulatoriv</servlet-name>
<url-pattern>/calc</url-pattern>
</servlet-mapping>
</web-app>
![Page 41: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/41.jpg)
OUTPUT:
![Page 42: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/42.jpg)
6 (v) (b). Modify the above program such that it stores each query in a database and checks the database first for
the result. If the query is already available in DB, it returns the value that was previously computed (from DB) or it
computes the result and returns it after storing the new query and result in DB.
USING SERVLET
CODE:
cal6vb.html
<html>
<head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="calcdb" method="post">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="submit" value="+" name="add">
<input type="submit" value="-" name="sub">
<input type="submit" value="*" name="mul">
<input type="submit" value="/" name="div">
<input type="submit" value="%" name="mod">
</form>
</center>
</body>
</html>
CalcDbServlet6vb.java import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class CalcDbServlet6vb extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
try
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
int n01,n02;
String op;
n01=Integer.parseInt(req.getParameter("v1"));
n02=Integer.parseInt(req.getParameter("v2"));
op=req.getParameter("add");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:scce6");
Statement st=con.createStatement();
![Page 43: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/43.jpg)
ResultSet rs=st.executeQuery("SELECT * FROM calc");
if(rs.next())
{
out.println("<h1> operation already exixts in the database");
out.println("<br> Result is ="+rs.getString(5));
}
else
{
int result=0;
if(op.equals("+"))
result=n01+n02;
if(op.equals("-"))
result=n01-n02;
if(op.equals("*"))
result=n01*n02;
if(op.equals("/"))
result=n01/n02;
if(op.equals("%"))
result=n01%n02;
int x=st.executeUpdate("insert into guests2 values('"+n01+"','"+n02+"','"+op+"','"+result+"')");
if(x>0)
out.println("<h1> Result"+res+"</h1>");
else
out.println("<h1> Error in insertion</h1>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Web.xml
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>calulatorv</servlet-name>
<servlet-class>CalcDbServlet6vb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>calulatorv</servlet-name>
<url-pattern>/calcdb</url-pattern>
![Page 44: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/44.jpg)
</servlet-mapping>
</web-app>
OUTPUT:
![Page 45: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/45.jpg)
6(iv) b. A web application that takes a name as input and on submit it shows a hello <name> page where name is
taken from the request. It shows the start time at the right top corner of the page and provides a logout button.
On clicking this button, it should show a logout page with Thank You <name > message with the duration of usage
(hint: Use session to store name and time).
USING SERVLET
Session6vib.html
<html>
<head> <title> SESSION LOGIN </title> </head>
<body>
<center>
<form action="session6vib" method="get">
Enter Name: <input type="text" name="uname"> <br>
<input type="submit" value="LOGIN" name="register">
</form>
</center>
</body>
</html>
Session16vib.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Session16vib extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
try
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<form method=get action=session26vib>");
Date d=new Date();
out.println("<p align=right> Time:"+d.getTime()+"</p>");
String un=req.getParameter("uname");
HttpSession session=req.getSession();
session.setAttribute("user",un);
session.setAttribute("time",d.getTime());
out.println("Hello\t"+un);
out.println("<br><br> <input type=submit value=logout>");
out.println("</form");
}
![Page 46: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/46.jpg)
catch(Exception e)
{
e.printStackTrace();
}
}
}
Session26vib.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Session26vib extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
try
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession();
Date d2=new Date();
String un=(String)session.getAttribute("user");
Long t1=(Long)session.getAttribute("time");
Long t2=d2.getTime();
session.invalidate();
out.println("Thank you\t"+un);
out.println("<br><br> Session duration: '"+(t2-t1)/(60*60)+"'seconds");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Web.xml <?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>session1</servlet-name>
<servlet-class>Session16vib</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session1</servlet-name>
![Page 47: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/47.jpg)
<url-pattern>/session6vib</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>session2</servlet-name>
<servlet-class>Session26vib</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session2</servlet-name>
<url-pattern>/session26vib</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
![Page 48: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/48.jpg)
6(Vii) b. A web application that takes name and age from an HTML page. If the age is less than 18, it should send a
page with “Hello <name>, you are not authorized to visit the site” message, where <name> should be replaced
with the entered name. Otherwise it should send “Welcome <name> to this site” message.
USING SERVLETS
CODE:
Usereligibilty6viib.html
<html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<h1> USER ELIGIBILITY </h1>
<form action="checkage" method="get">
<input type="text" name="uname" placeholder="Enter your name"> <br>
<input type="text" name="uage" placeholder="Enter your age"> <br>
<input type="submit" value="submit" name="sub"<>
</form>
</center>
</body>
</html>
CheckAge6viib.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CheckAge6viib extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
try
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
int age=Integer.parseInt(req.getParameter("uage"));
String name=req.getParameter("uname");
if(age<=18)
{
out.println("<h1> Hello\t " +name+"\tyou are not eligible </h1>");
}
else
{
out.println("<h1> Welcome " +name+"to this site </h1>");
![Page 49: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/49.jpg)
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Web.xml
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>checkage</servlet-name>
<servlet-class>CheckAge6viib</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checkage</servlet-name>
<url-pattern>/checkage</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
![Page 50: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/50.jpg)
6. Implement the following web applications using (a) PHP, (b) Servlets and (c) JSP.
6 (i) (c). A user validation web application, where the user submits the login name and password to the server. The
name and password are checked against the data already available in Database and if the data matches, a
successful login page is returned. Otherwise failure message is shown to the user.
USING JSP
CODE:
Login6ic.html
<html>
<body>
<form name="f1" method="get" action="Login6ic.jsp">
<center>
<b><h1> Login Page </h1></b><br>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pwd"><br>
<br> <br>
<input type="submit" value="Login">
<input type="reset">
</center>
</form>
</body>
</html>
Login6ic.jsp
<%@page language="java" import="java.sql.*" errorPage=""%>
<%
String u,p;
u=request.getParameter("uname");
p=request.getParameter("pwd");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:scce");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from guests1");
String dbun,dbpwd;
while(rs.next())
{
dbun = rs.getString(2);
dbpwd = rs.getString(3);
if(dbun.equals(u) && dbpwd.equals(p))
{
out.println("<h1> Hi "+u+"..successfully logged in</h1>");
break;
}
![Page 51: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/51.jpg)
else if(dbun.equals(u))
{
out.println("<h1> Invalid password</h1>");
break;
}
else if(dbpwd.equals(p))
{
out.println("<h1> Invalid username</h1>");
break;
}
}
con.close();
%>
OUTPUT:
![Page 52: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/52.jpg)
6 (ii) (c). Modify the above program to use an xml instead of database
USING JSP
Loginjsp6iic.html <html>
<body>
<center>
<form method="get" action="Login6ic.jsp">
<table>
<tr>
<td> username: </td>
<td> <input type="text" name="uname"> </td>
</tr>
<tr>
<td> password </td>
<td> <input type="password" name="pwd"> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="login">
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Login6iic.jsp
<%@page language="java" import="java.sql.*" errorPage=""%>
<%
String u,p;
u=config.getInitParameter("uname");
p=config.getInitParameter("pwd");
String un=request.getParameter("uname");
String pass=request.getParameter("pwd");
if(u.equals(un) && p.equals(pass))
out.println("<h1> Welcome "+u+" </h1>");
else
out.println("<h1> Hi "+u+"..Invalid username or password</h1>");
%>
Web.xml
![Page 53: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/53.jpg)
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>login6iic</servlet-name>
<jsp-file>/Login6ic.jsp</jsp-file>
<Init-param>
<param-name>uname</param-name>
<param-value>cse</param-value>
</Init-param>
<Init-param>
<param-name>pwd</param-name>
<param-value>5</param-value>
</Init-param>
</servlet>
<servlet-mapping>
<servlet-name>login6iic</servlet-name>
<url-pattern>/Login6iic.jsp</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
![Page 54: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/54.jpg)
6(iv)(c) . A simple calculator web application that takes two numbers and an operator (+,-,/,* and %) from an HTML
page and returns the result page with the operation performed on the operands.
USING JSP
CODE:
Calc6ivc.html
<html>
<head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="calc6ivc.jsp" method="get">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="submit" value="+" name="add">
<input type="submit" value="-" name="sub">
<input type="submit" value="*" name="mul">
<input type="submit" value="/" name="div">
<input type="submit" value="%" name="mod">
</form>
</center>
</body>
</html>
Calc6ivc.jsp
<%@page language="java" import="java.sql.*" errorPage=""%>
<%
int n01,n02;
String op;
n01=Integer.parseInt(request.getParameter("v1"));
n02=Integer.parseInt(request.getParameter("v2"));
op=request.getParameter("add");
int result=0;
if(op.equals("+"))
result=n01+n02;
if(op.equals("-"))
result=n01-n02;
if(op.equals("*"))
result=n01*n02;
if(op.equals("/"))
result=n01/n02;
![Page 55: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/55.jpg)
if(op.equals("%"))
result=n01%n02;
out.println("<h1> Result= "+result+"</h1>");
%>
OUTPUT:
![Page 56: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/56.jpg)
6(v) (c). Modify the above program such that it stores each query in a database and checks the database first for
the result. If the query is already available in DB, it returns the value that was previously computed (from DB) or it
computes the result and returns it after storing the new query and result in DB.
USING JSP
CODE:
Calc6vc.html
<html>
<head> <title> Calculator </title> </head>
<body>
<center> <h1> BASIC CALCULATOR </h1>
<form action="calc6vc.jsp" method="post">
<input type="text" name="v1" placeholder="Enter a value">
<input type="text" name="v2" placeholder="Enter a value"><br><br>
<input type="submit" value="+" name="add">
<input type="submit" value="-" name="sub">
<input type="submit" value="*" name="mul">
<input type="submit" value="/" name="div">
<input type="submit" value="%" name="mod">
</form>
</center>
</body>
</html>
Calc6v.jsp
<%@page language="java" import="java.sql.*" "errorPage=""%>
<%
int n01,n02;
String op;
n01=Integer.parseInt(request.getParameter("v1"));
n02=Integer.parseInt(request.getParameter("v2"));
op=request.getParameter("add");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:scce6");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM calc");
if(rs.next())
{
out.println("<h1> operation already exixts in the database");
out.println("<br> Result is ="+rs.getString(5));
}
else
{
![Page 57: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/57.jpg)
int result=0;
if(op.equals("+"))
result=n01+n02;
if(op.equals("-"))
result=n01-n02;
if(op.equals("*"))
result=n01*n02;
if(op.equals("/"))
result=n01/n02;
if(op.equals("%"))
result=n01%n02;
int x=st.executeUpdate("insert into guests2 values('"+n01+"','"+n02+"','"+op+"','"+result
+"')");
if(x>0)
out.println("<h1> Result"+result+"</h1>");
else
out.println("<h1> Error in insertion</h1>");
}
%>
OUTPUT:
![Page 58: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/58.jpg)
6(vi) (c). A web application that takes a name as input and on submit it shows a hello <name> page where name is
taken from the request. It shows the start time at the right top corner of the page and provides a logout button.
On clicking this button, it should show a logout page with Thank You <name > message with the duration of usage
(hint: Use session to store name and time).
USING JSP
CODE:
Session6vic.html
<html>
<head> <title> SESSION LOGIN </title> </head>
<body>
<center>
<form action="session16vic.jsp" method="get">
Enter Name: <input type="text" name="uname"> <br>
<input type="submit" value="LOGIN" name="register">
</form>
</center>
</body>
</html>
Session16vic.jsp
<%@page language="java" import="java.util.*" errorPage=""%>
<form method="get" action="session26vic.jsp">
<%
Date d=new Date();
%>
<p align="right"> Time;<%=d.getTime()%></p>
<%
String un=request.getParameter("uname");
session.setAttribute("user",un);
session.setAttribute("time",d.getTime());
%>
Hello <%=un%>
<br><br>
<input type="submit" value="logout">
</form>
Session26vic.jsp
<%@page language="java" import="java.util.*" errorPage=""%>
<%
![Page 59: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/59.jpg)
Date d2=new Date();
String un=(String)session.getAttribute("user");
Long t1=(Long)session.getAttribute("time");
Long t2=d2.getTime();
%>
Thank you <%=un%>
<br><br>
Session duration: <%=(t2-t1)/(60*60)%> seconds
<% session.invalidate();%>
OUTPUT:
![Page 60: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/60.jpg)
6 (Vii)(c). A web application that takes name and age from an HTML page. If the age is less than 18, it should
send a page with “Hello <name>, you are not authorized to visit the site” message, where <name> should be
replaced with the entered name. Otherwise it should send “Welcome <name> to this site” message.
USING JSP
CODE:
Usereligible6viic.html
<html>
<head> <title> User Acess </title> </head>
<body> <br>
<center>
<h1> USER ELIGIBILITY </h1>
<form action="checkage6viic.jsp" method="get">
<input type="text" name="uname" placeholder="Enter your name"> <br>
<input type="text" name="uage" placeholder="Enter your age"> <br>
<input type="submit" value="submit" name="sub"<>
</form>
</center>
</body>
</html>
Checkage6viic.jasp
<%@page language="java" import="java.sql.*" errorPage=""%>
<%
String name;
int age;
name=request.getParameter("uname");
age=Integer.parseInt(request.getParameter("uage"));
if(age<=18)
{
out.println("<h1> Hello\t " +name+"\tyou are not eligible </h1>");
}
else
{
out.println("<h1> Welcome " +name+"to this site </h1>");
}
%>
![Page 61: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/61.jpg)
OUTPUT:
CASE 1:
CASE 2:
![Page 62: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/62.jpg)
6(ix) (c). A web application that lists all cookies stored in the browser on clicking “List Cookies” button. Add
cookies if necessary.
USING JSP
CODE:
cookie6ixc.html
<html>
<head> <title> Calculator </title> </head>
<body>
<form action="cookie16ixc.jsp" method="post">
Enter your name:<input type="text" name="name1" placeholder="Enter name">
<input type="submit" value="Add cookie">
</form>
</center>
</body>
</html>
cookie16ixc.jsp
<%@page language="java" import="java.sql.*" errorPage=""%>
<%
String name=request.getParameter("name1");
Cookie c1=new Cookie ("firstname",name);
response.addCookie(c1);
c1.setMaxAge(50*50);
%>
<form method="get" action="cookie26ixc.jsp">
<input type="submit" value="List Cookies">
</form>
Cookie26ixc.jsp
<%@page language="java" import="java.sql.*" errorPage=""%>
<title> List of Cookies </title>
<h1> List of Cookies </h1>
<%
Cookie[] cookies=request.getCookies();
![Page 63: SREE CHAITANYA COLLEGE OF ENGINEERING · 2018-08-28 · 3. Write an HTML page that has one input, which can take multi line text and submit button. Once the user clicks on submit](https://reader036.fdocuments.us/reader036/viewer/2022070701/5e5adaeb2c37bf4ece507989/html5/thumbnails/63.jpg)
%>
<table border=1>
</tr>
<%
out.println("<td> Cookie Name </td> <td> Cookie value </td>");
for(int i=0;i<cookies.length;i++)
{
out.println("<h2> <tr> <td> "+cookies[i].getName()+"</td><td>"+cookies[i].getValue()+"</td></tr>");
}
%>
</tr>
</table>
OUTPUT: