Professional Documents
Culture Documents
Develop and execute the following programs using HTML and PHP.
Create Database using MYSQL wherever necessary.
7. Program to query the database and to display the result on a web page.
8. Program to accept book information viz. Accession number, title, authors, edition and publication
from a web page and to store those in a database.
9. Program to search a book for a title given by the user on the web page and display the search results
with proper headings.
14. Program to request server information viz. Request Method, URL, Protocol and
Remote address.
15. Program to accept User name and address and display them in a web page by
passing parameters.
Page 1 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
1. a) Program to display various Server information like – Server Name, Server Software,
Server protocol, CGI Version etc .
#! /usr/bin/perl
Solution no. 2
#! /usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html>\n\n";
print "<body><h1>About the Server</h1>";
foreach ($key(keys(%ENV))
{
print "<hr>";
print "$key : $ENV{$key}";
print "<hr>";
}
print "</body></html>\n";
1. b) Program to accept UNIX command from a HTML form and to display the
output of the command executed.
#!/usr/bin/perl
use CGI ':standard';
use CGI::Carp qw(warningsToBrowser);
if(param)
{
print header();
print start_html(-title=>"Unix Command",-bgcolor=>"Green",-text=>"blue");
$cmd=param("command");
$res=`$cmd`;
print b("output of $cmd is:"),br()",
"<pre>",
b("$res"),
"</pre>";
print start_form();
print submit(-value=>"Back");
print end_form();
print end_html();
}
Page 2 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
else
{
print header();
print start_html(-title=>"Unix Command",-bgcolor=>"yellow",-text=>"blue");
print start_form(),textfield(-name=>"command",-value=>" "),
submit(-name=>"submit",-value=>"Execute"),reset();
print end_form();
print end_html();
}
#!/usr/bin/perl
use CGI ':standard';
use CGI::Carp qw(warningsToBrowser);
if(param)
{
print header();
print start_html(-title=>"Unix Command",-bgcolor=>"Green",-text=>"blue");
$cmd=param("command");
print b("Hello $cmd"),br();
print start_form();
print submit(-value=>"Back");
print end_form();
print end_html();
}
else
{
print header();
print start_html(-title=>"Enter user name",-bgcolor=>"yellow",-text=>"blue");
print start_form(),textfield(-name=>"command",-value=>" "),submit(-name=>"submit",-
value=>"Submit"),reset();
print end_form();
print end_html();
}
2. b) Program to keep track of the number of visitors, visited the web page and
display the counter with proper headings.
#!/usr/bin/perl
print header();
print start_html(-title=>"WebPage Counter",-bgcolor=>"Pink",-text=>"blue");
open(FILE,'<count.txt');
$count=<FILE>;
close(FILE);
$count++;
open(FILE,'>count.txt');
print FILE "$count";
print b("This page has been viewed $count times");
Page 3 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
close(FILE);
print end_html();
3. Program to display a greeting based on the access time of the Web server. Also to
verify whether the webmaster is currently logged in.
#!/usr/bin/perl
if (param)
{
($s,$m,$h)=localtime(time);
br();
br();
$name=param("username");
br();
if($h>11&$h<18)
{
print "Good Afternoon";
}
if($h<12)
{ print "Good Morning";
}
if($h>18)
{ print "Good night";
}
print start_form(),br,submit(-value=>"Back"),end_form();
else
{
print "<valign=right>",start_form(),b("Name "),textfield(-
name=>"username"),br,br,"<valign=right>",",b("Webmaster"),textfield(-
name=>"webm"),br,br," ","<valign=right>", submit(-value=>"Submit"),"
",reset(),end_form;
}
Page 4 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
4. Program display a digital clock which display the current time of the server.
#!/usr/bin/perl
#!/usr/bin/perl –w
use DBI;
use CGI ':standard';
print header();
print start_html(-title=>"Program 5",-bgcolor=>"yellow",-text=>"Black");
$dbh=DBI->connect("DBI:mysql:test","root","");
#or die "Couldnt connect:"DBI->errstr();
while(($a,$b)=$sth->fetchrow())
{
print b("$a $b"),br;
}
print b("success"),br;
$sth->finish();
$dbh->disconnect();
6. Program to insert new name and age information entered by the user into the
database.
#! /usr/bin/perl
while ( ($name,$age)=$qh->fetchrow())
{
print "<tr><td>$name</td><td>$age</td></tr>";
}
Page 5 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
print "</table>";
$qh->finish();
$dbh->disconnect();
print"</HTML>";
7. Program to query the database and to display the result on a web page.
//7.html
<html>
<head><title>Program 7</title></head>
<body>
<form action=”7.php” method=”post”>
Enter the query <input type=”text” name=”query”>
<input type=submit>
</form>
</body>
</html>
-----------------------------------------------------
//7.php
<html>
<head><title>Database Query</title></head>
<body>
<?
$link=mysql_connect("localhost","root","");
mysql_select_db("test");
$q=$_POST["query"];
$var=mysql_query("$q");
echo"<table border size =1>";
echo"<tr><th>Name</th><th>Age</th></tr>";
while ($arr=mysql_fetch_row($var))
{
echo"<tr><td>$arr[0]</td><td>$arr[1]</td></tr>";
}
echo"</table>";
mysql_free_result($var);
mysql_close($link);
?>
</body>
</html>
8. Program to accept book information viz. Accession number, title, authors, edition
and publication from a web page and to store those in a database.
//8.html
<html>
<head><title>Program 8</title></head>
<body>
<h3>Book Details</h3>
<form action=”8.php” method=”post”>
Account Number <input type=”text” name=”accno”><br>
Title <input type=”text” name=”title”><br>
Page 6 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
-----------------------------------------------------
//8.php
<html>
<head><title>Result Table</title></head>
<body>
<h3>Resulting table after the insert operation </h3>
<hr>
<?
$link=mysql_connect("localhost","root","");
mysql_select_db("test");
$acc=$_POST["accno"];
$title=$_POST["title"];
$author=$_POST["author"];
$edn=$_POST["edn"];
$pub=$_POST["pub"];
$t=mysql_query("insert into book values('$acc','$title','$author','$edn','$pub')");
$var=mysql_query("SELECT * FROM book");
echo"<table border size=1>";
echo"<tr><th>Accession number</th><th>Title</th><th>Author</th><th>
Edition</th><th>Publication</th></tr>";
while ($arr=mysql_fetch_row($var))
{
echo"<tr><td>$arr[0]</td><td>$arr[1]</td><td>$arr[2]</td>
<td>$arr[3]</td><td>$arr[4]</td></tr>”;
}
echo”</table>”;
mysql_free_result($var);
mysql_close($link);
?>
<hr>
<form action=”p8.html”>
<input type=”submit” value=”Back”>
</form>
</body>
</html>
9. Program to search a book for a title given by the user on the web page and
display the search results with proper headings.
//9.html
<html>
<head><title>Program 7</title></head>
Page 7 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
<body>
<form action=”9.php” method=”post”>
Enter the title of the book <input type=”text” name=”title”>
<input type=submit>
</form>
</body>
</html>
-----------------------------------------------------
//9.php
<html>
<head><title>Search Result </title></head>
<body>
<h3>Search Result </h3>
<hr>
<?
$link=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“test”);
$t=$_GET[“title”];
$var=mysql_query(“ SELECT * FROM book WHERE title LIKE ‘$t’ “);
echo”<table border size=1>”;
echo”<tr><th>Accession number</th><th>Title</th><th>Author</th><th>Edition</th><th>
Publication</th></tr>”;
while ($arr=mysql_fetch_row($var))
{
echo”<tr><td>$arr[0]</td><td>$arr[1]</td><td>$arr[2]</td>
<td>$arr[3]</td><td>$arr[4]</td></tr>”;
}
echo”</table>”;
mysql_free_result($var);
mysql_close($link);
?>
<hr>
<form action=”p9.html”>
<input type=”submit” value=”Back”>
</form>
</body>
</html>
import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Page 8 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head><title>CGI Programming- Servlet Program 10 A </title></head>");
out.println("<body>");
out.println("<h1>Program 10A: Greeting Program</h1>");
out.println("<h3>please Enter the User name :</h3>");
out.print("<form action=\"");
out.println("prg10a\" method=POST>");
//out.println("<form method=post action=/servlets-examples/prg10a>");
out.println("UserName:      <input type=text name=username
value=\"\">");
out.println("<input type=submit value=Submit>");
out.println("<input type=Reset value=Clear>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
}
10. b) Program to change the background color of the page based on the color selected by the user.
import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Page 9 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
throws ServletException,IOException
{
PrintWriter out=res.getWriter();
String color=req.getParameter("color");
if(color==null)
{
color="white";
}
out.println("<html>");
out.println("<head><title>Program 10 B </title></head>");
out.println("<body bgcolor=\""+color+"\">");
out.println("<h1>Program 10A: Background Color Chooser Program</h1>");
out.println("<h3>please Choose the color in list :</h3>");
out.println("<form method=post action=/servlets- examples/prg10b>");
out.println("<select name=color>");
out.println("<option selected>Red</option>");
out.println("<option >green</option>");
out.println("<option >blue</option>");
out.println("<option >orange</option>");
out.println("<option >pink</option>");
out.println("<option >yellow</option>");
out.println("<option >magenta</option>");
out.println("<option >gold</option>");
out.println("<option >silver</option>");
out.println("<option >gray</option>");
out.println("<option >lavender</option>");
out.println("<option >light pink</option>");
out.println("<option >pista</option>");
out.println("<option >peech</option>");
out.println("<option >snuff</option>");
out.println("<option >peacock blue</option>");
out.println("<option >cement</option>");
out.println("</select>");
out.println("<input type=submit value=Submit>");
out.println("<input type=Reset value=Clear>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
Page 10 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
11. Program to display a greeting based on the access time of the server.
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
int ampm=calendar.get(Calendar.AM_PM);
String str="PM";
if(ampm==0)
{
str="AM";
switch(hour)
{
case 0:
case 1:
case 2:
case 3:
case 4: out.println("It is MidNight, Have a great sleep");break;
case 5: break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:out.println("Good Morning");
}
Page 11 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
}
else
{
switch(hour)
{
case 0:
case 1:
case 2:
case 3:
case 4:out.println("Good Afternoon");break;
case 5:
case 6:
case 7:
case 8:out.println("Good Evening");break;
case 9:out.println("Have a nice dinner");break;
case 10:
case 11:out.println("Good Night; sleep well!");
}
}
import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Page 12 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
out.println("<body>");
out.println("<h1>Program to Create and Display the cookie</h1>");
Page 13 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
13. Program to create a session and display session information viz. session ID, creation
time and last accessed.
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Page 14 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
out.println("</body>");
out.println("</html>");
}
}
}
14. Program to request server information viz. Request Method, URL, Protocol and
Remote address.
import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class prg14 extends HttpServlet
{
Page 15 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
15. Program to accept User name and address and display them in a web page by
passing parameters.
import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class prg15 extends HttpServlet
{
Page 16 of 17
Java and CGI programming laboratory manual Yogeesha H C, Asstt. Prof, KVGCE, Sullia
}
}
Page 17 of 17