Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

EXPERIMENT NO 5

AIM: Write an XHTML document to include an anchor tag, that calls a PHP document also
write the called PHP document which returns a randomly chosen greeting from a list of five
different greetings. The greetings must be stored as constant strings in the script. A random
number between 0 and 4 can be computed with these line.
#set the seed for mtrand with the number of microseconds

#since the last full second of the clock mt_strand((double) microtime()*1000000);


$number=mtrand(0,4);
#computes a random integer 0-4.

Write the PHP script for above to count the number of visitors and display that number for
each visitor.
Hint: Use a file to store current count.

PROGRAM:
HTML FILE:
<html>
<head>
<title>Experiment5</title>
</head>
<body>
<center>
<br>
<br>
<br>
<br>
<br>
<br>

<h3>Click <a href="exp5.php"> Here!</a> to know Total Number of Visitor's visited this
site</h3>
</center>
</body>
</html>

PHP FILE:
<html>
<head>
<title>Greetings</title>
</head>
<body>
<center>
<?php
$visitorcount = file_get_contents("vc.txt");
++$visitorcount;
file_put_contents("vc.txt",$visitorcount);
echo "<br>";
echo "<br>";
echo "<br>";
echo"Welcome<br>";
echo"Your visitor number is ".$visitorcount."<br>";
$no=rand(0,4);

$greetings=array("Good Morning","Good Afternoon","Good Evening","Good Night","Good


Bye");
$msg=$greetings[$no];
echo"$msg";
?>
</center>
</body>
</html>
OUTPUT:
EXPERIMENT NO 6
AIM: Write the XHTML code using JavaScript Object Notation (JSON) to create the form
with the following capabilities
a) A text widget to collect the users name
b) Four check boxes, one each for the following items
i. Four 100 watt light bulbs for Rs 20=39
ii. Eight 100 watt bulbs for Rs 40=20
iii. Four 100 watt long life light bulbs for Rs 30=95
iv. Eight 100 watt long life light bulbs for Rs 70=49
c) A collection of 3 radio buttons that are labelled as follows
i. Visa
ii. Master Card
iii. Discover
Write a PHP script that computes the total cost of the ordered light bulbs for the above
program after adding 13.5% VAT. The program must inform the buyer of exactly what was
ordered in table

PROGRAM:
HTML FILE:
<html>
<head>
<title>Experiment 6 (Form)</title>
</head>
<body>
<center>
<form method="POST" action="exp6.php">
<h3>Generate the bill</h3>
<table border="7px" bordercolor="grey" bgcolor="skyblue">
<tr>
<td><center>Username</center></td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td colspan="3" bgcolor="khaki"><center>Items</center></td>
</tr>
<tr>
<td><input type="checkbox" name="option1">Four 100 watt light bulbs for Rs 20</td>
<td><input type="text" name="value1"></td>
</tr>
<tr>
<td><input type="checkbox" name="option2">Eight 100 watt light bulbs for Rs 40</td>
<td><input type="text" name="value2"></td>
</tr>
<tr>
<td><input type="checkbox" name="option3">Four 100 watt long life light bulbs for Rs
30</td>
<td><input type="text" name="value3"></td>
</tr>
<tr>
<td><input type="checkbox" name="option4">Eight 100 watt long life light bulbs for Rs
70</td>
<td><input type="text" name="value4"></td>
</tr>
<tr>
<td colspan="3" bgcolor="khaki"><center>Payment mode</center></td>
</tr>
<tr>
<td colspan="3"><input type="radio" name="G1" value="Visa">Visa</td>
</tr>
<tr>
<td colspan="3"><input type="radio" name="G1" value="Mastercard">Mastercard</td>
</tr>
<tr>
<td colspan="3"><input type="radio" name="G1" value="Rupay">Rupay</td>
</tr>
<tr>
<td colspan="3" bgcolor="khaki"><center><input type="Submit" name="Submit"
value="Submit"></center></td>
</tr>
</table>
</form>
</center>
</body>
</html>

PHP FILE:
<html>
<head>
<title>Order</title>
</head>
<body>
<?php
$uname=$_POST['username'];
$Pmode=$_POST['G1'];
$item1=$item2=$item3=$item4=0;
$price1=20;
$price2=40;
$price3=30;
$price4=70;
if(isset($_POST['option1']))
{
$item1=$_POST['value1'];
}
if(isset($_POST['option2']))
{
$item2=$_POST['value2'];
}
if(isset($_POST['option3']))
{
$item3=$_POST['value3'];
}
if(isset($_POST['option4']))
{
$item4=$_POST['value4'];
}
$total1=$item1*$price1;
$total2=$item2*$price2;
$total3=$item3*$price3;
$total4=$item4*$price4;
$total=$total1+$total2+$total3+$total4;
$total10=$total*13.5;
$total11=($total10/100)+$total;
echo "<b><center>Username: $uname<br>";
?>
<h3>Order Summary</h3>
<table border="1">
<tr>
<td><?php echo" Four 100 watt light bulbs";?></td>
<td><?php echo $price1;?></td>
<td><?php echo $item1;?></td>
<td><?php echo $total1;?></td>
</tr>
<tr>
<td><?php echo" Eight 100 watt light bulbs";?></td>
<td><?php echo $price2;?></td>
<td><?php echo $item2;?></td>
<td><?php echo $total2;?></td>
</tr>
<tr>
<td><?php echo" Four 100 watt long life light bulbs";?></td>
<td><?php echo $price3;?></td>
<td><?php echo $item3;?></td>
<td><?php echo $total3;?></td>
</tr>
<tr>
<td><?php echo" Eight 100 watt long lifelight bulbs";?></td>
<td><?php echo $price4;?></td>
<td><?php echo $item4;?></td>
<td><?php echo $total4;?></td>
</tr>
</table>
<?php echo"<br><b>Total Bill (Incl GST): </b>",$total11;?>
<?php echo"<br>Your payment mode is ",$Pmode;?>
</body>
</html>
OUTPUT:
EXPERIMENT NO 7
AIM: Write a XHTML code to provide a form that collects names and telephone numbers.
The phone numbers must be in the format ddd-ddd-dddd. Write a PHP script that checks the
submitted telephone number to be sure that it confirms to the required format and then returns
a response that indicates whether the number was correct.

PROGRAM:
HTML FILE:
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="POST" action="Exp7.php">
<center>
<h2>Basic Information</h2>
<table>
<tr>
<td>UNAME</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>PHONE NUMBER</td>
<td><input type="text" name="phoneno"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</center>
</form>
</body>
</html>

PHP FILE:
<?php
$uname = $_POST['uname'];
$phoneno = $_POST['phoneno'];
if(preg_match("/[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/",$phoneno)){
echo "<center>The Phone Number is valid</center>";
}
else{
echo "<center>The Phone Number is invalid</center>";
}
?>
OUTPUT:
EXPERIMENT NO 8
AIM: Write the XHTML code using JavaScript Object Notation (JSON) to accept from the
user name, phone no, mail-id, stored in database. Retrieve same information from database
using a separate PHP script.

PROGRAM:
HTML FILE:
<!DOCTYPE html>
<html>
<head>
<title>Eight Experiment</title>
</head>
<body>
<form method="POST" action="Exp8.php">
<h2>
<center>User Information</center>
</h2>
<center>
<table border="2" bgcolor="grey">
<tr bgcolor="skyblue">
<td>Name</td>
<td><input type="text" name="username"></td>
</tr>
<tr bgcolor="khaki">
<td>City</td>
<td><input type="text" name="city"></td>
</tr>
<tr bgcolor="skyblue">
<td>Phone No</td>
<td><input type="text" name="phoneno"></td>
</tr>
<tr bgcolor="khaki">
<td>E-mail</td>
<td><input type="text" name="email"></td>
</tr>
<tr bgcolor="grey">
<td colspan="2"><center><input type="Submit" name="Submit"
value="Submit"></center></td>
</tr>
</table>
</center>
</form>
</body>
</html>

PHP FILE:
<?php
$con = mysqli_connect('localhost','root','','My_database');
if(!$con){
die("could not connect to database".mysql_error());
}
$username = $_POST['username'];
$city = $_POST['city'];
$phoneno = $_POST['phoneno'];
$email = $_POST['email'];
$query = mysqli_query($con,"INSERT INTO
my_details(USER_NAME,CITY,PHONENO,EMAIL) VALUES
('$username','$city','$phoneno','$email')");
echo "<b><center>Display Records</b><br>";
$result = mysqli_query($con,"SELECT * FROM my_details");
while($row = mysqli_fetch_array($result)){
echo $row['USER_NAME']." | ".$row['CITY']." | ".$row['PHONENO']." |
".$row['EMAIL'];
echo "<br>";
}
mysqli_close($con);
?>
OUTPUT:
EXPERIMENT NO 9
AIM: 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.

PROGRAM:
HTML FILE:
<html>
<head>
<title>GREETINGS</title>
</head>
<body>
<form method="POST" action="Exp9_servlet">
<input type="submit" value="submit"/>
</form>
</body>
</html>

SERVLET.JAVA FILE:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/Exp9_servlet"})
public class Exp9_servlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String[] myString={"GOOD MORNING","GOOD AFTERNOON","GOOD
EVENING","GOOD NIGHT"};
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Random Wishing</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Random Wishing</h1>");
Random r=new Random();
out.println(myString[r.nextInt(myString.length)]);
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}
}
OUTPUT:
EXPERIMENT NO 10
AIM: Write 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.

PROGRAM:
HTML FILE:
<html>
<head>
<title>Exp-10</title>
</head>
<body><center>
<form method="POST" action="Exp10_servlet">
<h3>Enter the Data</h3>
<table border="2" bgcolor="grey">
<tr bgcolor="skyblue">
<td>USERNAME</td>
<td colspan="2"><input type="text" name="username"></td>
</tr>
<tr bgcolor="khaki">
<td colspan="3"><center>ITEMS</td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option1">Four 100 watt light bulbs for Rs 20.</td>
<td colspan="2"><input type="text" name="val1" ></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option2">Eight 100 watt light bulbs for Rs 40.</td>
<td colspan="2"><input type="text" name="val2"></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option3">Four 100 watt long life light bulbs for Rs
30.</td>
<td colspan="2"><input type="text" name="val3"></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option4">Eight 100 watt long life light bulbs for Rs
70.</td>
<td colspan="2"><input type="text" name="val4"></td>
</tr>
<tr bgcolor="khaki">
<td colspan="3"><center>Payments</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group1">Visa</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group2">Mastercard</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group3">Rupay</td>
</tr>
<tr bgcolor="grey">
<td colspan="3"><center><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
SERVLET.JAVA FILE:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/Exp10_servlet"})
public class Exp10_servlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String uname=request.getParameter("username");
int item1,item2,item3,item4;
int price1=20;
int price2=40;
int price3=30;
int price4=50;
item1=item2=item3=item4 = 0;
if(request.getParameter("option1")!=null){
String str1=request.getParameter("val1");
item1=Integer.parseInt(str1);
}
if(request.getParameter("option2")!=null){
String str2=request.getParameter("val2");
item2=Integer.parseInt(str2);
}
if(request.getParameter("option3")!=null){
String str3=request.getParameter("val3");
item3=Integer.parseInt(str3);
}
if(request.getParameter("option4")!=null){
String str4=request.getParameter("val4");
item4=Integer.parseInt(str4);
}
int total1=item1*price1;
int total2=item2*price2;
int total3=item3*price3;
int total4=item4*price4;
double total=(total1+total2+total3+total4);
total+=(total*2.0)/100;
out.println("<!DOCTYPE html>");
out.println("<head>");
out.println("<title>Servlet Order</title>");
out.println("</head>");
out.println("<body>");
out.println("<h4>Username:"+uname+"</h4>");
out.println("<h3>you have ordered following items:</h3>");
out.println("<table border>");
out.println("<tr><td>Qty</td>");
out.println("<td>Price</td>");
out.println("<td>Total</td></tr>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>"+item1+"</td>");
out.println("<td>"+price1+"</td>");
out.println("<td>"+total1+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>"+item2+"</td>");
out.println("<td>"+price2+"</td>");
out.println("<td>"+total2+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>"+item3+"</td>");
out.println("<td>"+price3+"</td>");
out.println("<td>"+total3+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>"+item4+"</td>");
out.println("<td>"+price4+"</td>");
out.println("<td>"+total4+"</td>");
out.println("</tr>");
out.println("</table><br><br>");
out.println("<i>Total bill including sales tax:</i>"+total);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
OUTPUT:
EXPERIMENT NO 11
AIM: Write and test a JSP document that displays the form of exercise 6 and produces the
same response document as exercise 10.

PROGRAM:
HTML FILE:
<html>
<head>
<title>Exp-11</title>
</head>
<body><center>
<form method="POST" action="Exp11.jsp" >
<h3>Enter the Data</h3>
<table border="2" bgcolor="grey">
<tr bgcolor="skyblue">
<td>USERNAME</td>
<td colspan="2"><input type="text" name="username"></td>
</tr>
<tr bgcolor="khaki">
<td colspan="3"><center>ITEMS</td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option1">Four 100 watt light bulbs for Rs 20.</td>
<td colspan="2"><input type="text" name="val1" ></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option2">Eight 100 watt light bulbs for Rs 40.</td>
<td colspan="2"><input type="text" name="val2"></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option3">Four 100 watt long life light bulbs for Rs
30.</td>
<td colspan="2"><input type="text" name="val3"></td>
</tr>
<tr bgcolor="skyblue">
<td><input type="checkbox" name="option4">Eight 100 watt long life light bulbs for Rs
70.</td>
<td colspan="2"><input type="text" name="val4"></td>
</tr>
<tr bgcolor="khaki">
<td colspan="3"><center>Payments</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group1" value="Visa">Visa</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group1" value="Mastercard">Mastercard</td>
</tr>
<tr bgcolor="skyblue">
<td colspan="3"><input type="radio" name="group1" value="Rupay">Rupay</td>
</tr>
<tr bgcolor="grey">
<td colspan="3"><center><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP FILE:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>jsp Page</title>
</head>
<body>
<h2>Displaying form data</h2>
<b>USERNAME:<%=request.getParameter("username")%></br>
<% int item1,item2,item3,item4;
int price1=20;
int price2=40;
int price3=30;
int price4=70;
item1=item2=item3=item4=0;
if(request.getParameter("option1")!=null){
String str1=request.getParameter("val1");
item1=Integer.parseInt(str1);
}
if(request.getParameter("option2")!=null){
String str2=request.getParameter("val2");
item2=Integer.parseInt(str2);
}
if(request.getParameter("option3")!=null){
String str3=request.getParameter("val3");
item3=Integer.parseInt(str3);
}
if(request.getParameter("option4")!=null){
String str4=request.getParameter("val4");
item4=Integer.parseInt(str4);
}
int total1=item1*price1;
int total2=item2*price2;
int total3=item3*price3;
int total4=item4*price4;
double total=(total1+total2+total3+total4);
total+=(total*2.0)/100;
%>
<h3> You have ordered following items</h3>
<table border="1">
<tr>
<th>qty</th>
<th>price</th>
<th>Total</th>
</tr>
<tr>
<td><%=item1%></td>
<td><%=price1%></td>
<td><%=total1%></td>
</tr>
<tr>
<td><%=item2%></td>
<td><%=price2%></td>
<td><%=total2%></td>
</tr>
<tr>
<td><%=item3%></td>
<td><%=price3%></td>
<td><%=total3%></td>
</tr>
<tr>
<td><%=item4%></td>
<td><%=price4%></td>
<td><%=total4%></td>
</tr>
</table></br>
<i>Total bill including sales tax:</i>
<%=total%><br>
<b>Your Payment Mode:<%=request.getParameter("group1")%></b>
</body>
</html>
OUTPUT:
EXPERIMENT NO 12
AIM: Write a markup document to create a form that collects favourite 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.

PROGRAM:
HTML FILE:
<!DOCTYPE html>
<html>
<head>
<title>Exp12</title>
</head>
<body>
<center>
<form method="POST" action="song">
<label>Song Name:</label>
<input type="text" name="Sname"><br>
<br>
<label>Composer Name:</label>
<input type="text" name="Cname"><br>
<br>
<label>Artist Name:</label>
<input type="text" name="Aname">
<br>
<br>
<center><input type="submit" name="submit" value="Submit"></center>
</form>
</center>
</body>
</html>
SERVLET.JAVA FILE:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation. WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns={"/song"})
public class song extends HttpServlet{
protected void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset-UTF-8");
PrintWriter out=response.getWriter();
String sname=request.getParameter("Sname");
String cname=request.getParameter("Cname");
String singer=request.getParameter("Aname");
String Song_file="E://songs.txt";
try
{
FileWriter fw=new FileWriter(Song_file,true);
fw.write("#SONG NAME\n");
fw.write(sname);
fw.write("\n##COMPOSER NAME\n");
fw.write(cname);
fw.write("\n");
fw.write("###ARTIST NAME\n");
fw.write(singer);
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet song</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Contents are written to the file</h1>" +Song_file);
FileReader fr=new FileReader(Song_file);
int i;
while((i=fr.read())!=-1)
out.println((char)i);
fr.close();
out.println("</body>");
out.println("</html");
}
}
OUTPUT:

E://songs.txt

You might also like