WT Lab Manual
WT Lab Manual
3 Write a PHP script to merge two array and sort the mass
numbers, in descending order.
4 Write a PHP script that reads data from one file and write into
another file.
5 Develop static pages (using Only HTML) of an online book
store. The pages should resemble:www.amazon.com. The
website should consist the following pages.
a) Home page
b) Registration and user Login
c) User Profile Page
d) Books catalog
e) Shopping Cart
f) Payment By credit card
g) Order Conformation
<?php
$number =2 ;
while($number<100)
{
$div_count=0;
for($i=1;$i<=$number;$i++)
{
if(($number%$i)==0)
{
$div_count++;
}
}
if($div_count<3)
{
echo$number.",";
}
$number=$number+1;
}
?>
OUTPUT:2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
2.PHP script to
<!DOCTYPEhtml>
<html>
<body>
<?php
echostrlen("Helloworld!");
?>
</body>
</html>
OUTPUT:
12
b. Count no of words in a string.
<!DOCTYPEhtml>
<html>
<body>
<?php
echostr_word_count("Helloworld!");
?>
</body>
</html>
OUTPUT:
c. Reverse string.
<!DOCTYPEhtml>
<html>
<body>
<?php
echostrrev("Helloworld!");
?>
</body>
</html>
OUTPUT:
!dlrowolleH
d. Search for a specific string.
<!DOCTYPEhtml>
<html>
<body>
<?php
echostrpos("Helloworld!","world");
?>
</body>
</html>
OUTPUT:
6
<!DOCTYPEhtml>
<html>
<body>
<?php
echostr_replace("world","Dolly","Helloworld!");
?>
OUTPUT:
HelloDolly!
</body>
</html>
3. Write a PHP script to merge two arrays and sort them as numbers, in descending order.
<?php
$a1=array(1,3,15,7,5);
$a2=array(4,3,20,1,6);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>
Array([0]=> 20[1]=> 15[2]=> 7[3]=>6[4]=> 5[5]=> 4[6]=> 3[7]=> 3[8]=> 1[9]=> 1)
4.Write a PHP script that reads data from one file and write into another file.
<?php
if(isset($_POST['save']))
$f=$_POST['file'];
$ext=$_POST['ext'];
$data=$_POST['data'];
$file=$f.$ext;
if(file_exists($file))
echo"<fontcolor='red'>filealreadyexists</font>";
else
$fo=fopen($file,"w");
fwrite($fo,$data);
echo"yourdataissaved";
}
?>
<formmethod="post">
enteryourfile<inputtype="text"name="file"/><br/> choose
Enteryourcontents<textarearows="10"cols="30"name="data">
<?phpecho@$contents;?>
</textarea><br/>
<inputtype="submit"value="Save"name="save"/>
</form>
<?php
if(isset($_POST['disp']))
{
$f=$_POST['file'];
$ext=$_POST['ext'];
$file=$f.$ext;
if(file_exists($file))
$fo= fopen($file,"r");
$contents=fread($fo,filesize($file));
else
echo"<font color='red'>filedoesn'texists</font>";
?>
<formmethod="post">
enteryourfile<inputtype="text"name="file"/><br/> choose
Enteryourcontents<textarearows="10"cols="30"name="data">
<?phpecho@$contents;?>
</textarea><br/>
<inputtype="submit"value="Disp"name="disp"/>
</form>
5. Develop static pages (using Only HTML) of an online book store. The pages should
resemble:www.amazon.com. The website should consist the following pages.
a) Home page
b) Registration and user Login
c) User Profile Page
d) Books catalog
e) Shopping Cart
f) Payment By credit card
Order Conformation
a) Home page
Main.html:
<html>
<head>
<title>
Amazon</title>
</head>
<bodybgcolor="cyan"><center>
<strong><h1>WelcometoAMAZON</h1></strong>
<formmethod="post"action="login.html"target=_blank>
<h4>forbooks</h4><inputtype="submit"value="click here">
</form>
</center>
</body>
</html>
Login.html:
<html>
<head>
<title>
login</title>
</head>
<bodybgcolor="cyan"><center>
<strong><h1>AMAZON</h1></strong></center>
<right>
<tablealign="right">
<tr>
<td><h4>username</td>
<td><input type="text"></td>
<td></td>
</tr>
<tr>
<td><h4>password</td>
<td><inputtype="password"></td>
<td></td>
</tr>
<tr>
<td>
<formmethod="post"action="catalog.html">
<inputtype="submit"value="submit">
</form>
</td>
<td>
<formmethod="post"action="reg.html">
<inputtype="submit"value="register">
<inputtype="reset" value="reset"></form></td>
</tr>
</table>
</body>
</html>
Registration
page
reg.html:
<html>
<head>
<title>
loginpage</title>
</head>
<bodybgcolor="cyan">
<center><strong><h1>AMAZON</h1></strong></center>
<formmethod="post"action="catalog.html">
<right>
<tablealign="left">
<tr>
<td><h4>username</td>
<td><input type="text"></td>
<tr>
<tr>
<td><h4>password</td>
<td><inputtype="password"></td>
</tr>
<tr>
<td><h4>confirmpassword</td>
<td><inputtype="password"></td>
</tr>
<tr>
<td><h4>male
<option>
<inputtype="radio"name="sex" id="male"></td>
<td><h4>female
<inputtype="radio"name="sex"id="female"></td>
</option>
</tr>
<tr>
<td>Address</td>
<td><textareaname="address"rows=5 cols=19>
</textarea>
</td>
<tr>
<td>
<inputtype="submit"value="submit"></td>
<td>
<inputtype="reset" value="reset"></td>
</tr>
</form>
</body>
</html>
c)User profile
userprofile.html
<html>
<head>
<title>
userprofile</title>
</head>
<bodybgcolor="cyan"><center>
<strong><h1>WelcometoAMAZONOnlineBookStore</h1></strong></center> Edit
your profile here...
<formmethod="post"action="catalog.html">
<right>
<tablealign="left">
<tr>
<td><h4>Edit username</td>
<td><input type="text"></td>
<tr>
<tr>
<td><h4>Edit password</td>
<td><inputtype="password"></td>
</tr>
<tr>
<option>
<td><h4>male
<inputtype="radio"name="sex" id="male"></td>
<td><h4>female
<inputtype="radio"name="sex"id="female"></td>
</option>
</tr>
<tr>
<td>EditAddress</td>
<td><textareaname="address"rows=5 cols=19>
</textarea>
</td>
<tr>
<td>
<inputtype="submit"value="submit"></td>
</tr>
</form>
</body>
</html>
d)Books catalog
Catalog.html:
<html>
<head>
<title>
bookscatalog</title>
</head>
<bodybgcolor="cyan">
<center><h1>AMAZON</h1></center>
<formmethod="post"action="shopping.html">
<left>
<table>
<tr>
<td><b><h3>frontendbooks</td>
<td></td></tr>
<tr>
<td></td>
<td><h4>C&Ds</td>
</tr>
<tr>
<td></td>
<td><h4>Ads</td>
</tr>
<tr>
<td></td>
<td><h4>JAVA
</td></tr>
<tr>
<td><b><h3>backendbooks</td>
<td></td>
</tr>
<tr>
<td></td>
<td><h4>Oracle</td>
</tr>
<tr>
<td></td>
<td><h4>MsSQL Server
</td></tr>
<tr>
<td></td>
<td><h4>MySql</td>
</tr>
</table>
</h4>
<center>
<b>forbuyoneofthesebooks
<br>
</b><inputtype="submit"value="click here">
</center>
</form>
</body>
</html>
e)Shopping cart
Shopping.html:
<html>
<head><title>shoppingcart</title>
</head>
<bodybgcolor="cyan">
<center><h1>
ShoppingCart</h1></center>
<br><br><br><br><br>
<tablealign="center">
<tr>
<td>Text Books</td>
<td>
<select>
<optgrouplabel="selectthe book">
<optionvalue="C&Ds">C&Ds
<optionvalue="Ads">Ads
<optionvalue="Java">Java
<optionvalue="Oracle">Oracle
<optionvalue="MsSQLServer">MsSQL Server
<optionvalue="MySql">MySql
</optgroup>
</select>
</td></tr>
<tr>
<td>Quantity</td
>
<td>
<inputtype="text"id="q">
</td></tr>
<tr>
<td></td>
<td>
<formmethod=postaction="payment.html">
<inputtype="submit"value=ok/>
</form>
</td></tr>
</table>
<center>
<pre>Costofonebook is"500"+shipping "100"</pre>
</center>
<body>
</html>
Payment.html:
<html>
<head><title>payment</title></head>
<bodybgcolor="cyan">
<center><h1>Payment ByCreditCard</h1></center>
<formmethod=postaction="ordrconform.html">
<br><br><br><br><br>
<tablealign="center">
<tr>
<td>
<h4>TotalAmount</h4></td>
<td><inputtype="text">
</td>
</tr>
<tr>
<td><h4>CreditCardNumber</td>
<td><inputtype="text"></td>
</tr>
<tr>
<td>
</td>
<td><inputtype="submit"value=OK>
</td>
</tr>
</table>
</form></body>
</html>
g)Order Conformation
Ordrconform:
<html>
<head><title>orderconformation</title><M/head>
<bodybgcolor="cyan">
<center>
<h1><b>BOOKSHOPPING</h1>
<pre><strong>
<b>YourorderIs Conformed
</strong></pre>
<h2><b>THANKYOU</h2>
</center>
</body></html>
output:
6.Validate the Registration, user login, user profile and payment by credit card pages using
JavaScript.
Homepage:
Main.html:
<html>
<framesetrows="25%,*">
<framesrc="top.html"name="top"scrolling="no"frameborder="0">
<framesetcols="25%,75%">
<framesrc="left.html"name="left"scrolling="no"frameborder="0">
<framesrc="right.html" name="right"scrolling="auto"frameborder ="0">
</frameset>
</frameset>
</html>
Top.html:
<html>
<bodybgcolor="pink">
<br><br>
<marquee><h1
align=”center”><b><u>ONLINEBOOK
STORAGE</u></b></h1></marquee>
</body>
</html>
Right.html:
<html>
<body>
<br><br><br><br><br>
<h2align="center">
<b><p>welcometoonlinebookstorage.Press
login if you are having id otherwise press
registration.
</p></b></h2>
</body></html>
Left.html:
<html>
<bodybgcolor="pink">
<h3>
<ul>
<li><ahref="login.html"target="right"><font
color="black">
LOGIN</font></a></li><br><br>
<li><ahref="profile.html"target="right"><font
color="black"> USER
PROFILE</font></a></li><br><br>
<li><ahref="catalog.html"target="right"><font
color="black"> BOOKS
CATALOG</font></a></li><br><br>
<li><ahref="scart.html"target="right"><font
color="black">
SHOPPINGCART</font></a></li><br><br>
<li><ahref="payment.html"target="right"><font
color="black">
PAYMENT</font></a></li><br><br>
<br><br>
</ul>
</body>
</html>
Registration and user Login
Login.html:
<html>
<bodybgcolor="pink"><br><br><br>
<script
language="javascript">fu
nction validate()
{
var flag=1;
if(document.myform.id.value=="
"||
document.myform.pwd.value==
"")
{
alert("LoginIdandPasswordmustbefilled")
flag=0;
}
if(flag==1)
{
alert("VALIDINPUT");
window.open("catalog.html","right");
}
else
{
alert("INVALIDINPUT");
//document.myform.focus();
}
}
</script>
<formname="myform">
<divalign="center"><pre>
LOGINID:<inputtype="text"name="id"><br>
PASSWORD:<input
type="password"name="pwd"><br><br>
</pre>
<inputtype="button"value="ok"onClick="validate()">
<inputtype="reset"value="clear">
</div>
</form>
</body>
</html>
User profile page
Profile.html:
<html>
<bodybgcolor="pink"><br><br>
<script
type="text/javasc
ript"> function
validate()
{
var flag=1;
if(document.myform.name.value
==""||
document.myform.add
r.value==""||
document.myform.phn
o.value==""||
document.myform.id.v
alue==""||
document.myform.pw
d.value=="")
{
alert("Enterallthe details");
flag=0;
}
varstr=document.myform.phno.value;
varx=newRegExp("\\d","g");
if(!(str.match(x)))
{
if(!(str.
length
==10))
flag=0
;
}
varstr1=document.myform.id.value;
var x1=new RegExp("^[A-Z][a-
zA-Z]+$","g");
if(!(str1.match(x1)))
{
flag=0;
alert("InvalidUserID");
}
var
str1=document.myform.pwd.valu
e; var x1=new RegExp("^[A-
Z][a-zA-Z]+$","g");
if(!(str1.match(x1)))
{
flag=0;
alert("Invalidpassword");
}
if(f
lag
==
1)
{
alert("VALIDINPUT");
window.self.location.href="login.html";
}
else
{
alert("INVALIDINPUT");
document.myform.focus();
<br><br>
}
}
</script>
<formname="myform">
<divalign="center"><pre>
NAME:<inputtype="text"name="name"><br>ADDRESS:<input
type="type" name="addr"><br> CONTACT NUMBER:<input
type="text"name="phno"><br> LOGINID :<input
type="text"name="id"><br>
PASSWORD:<input type="password"name="pwd"></pre><br><br>
</div>
<divalign="center">
<inputtype="button"value="ok"onClick="validate()">
<inputtype="reset" value="clear">
</form></body></html>
Books catalog:
Scart.html:
<html>
<bodybgcolor="pink"><br><br><br>
<script
language="javascript
"> functionvalidate()
{
var flag=1;
if(document.myform.title.val
ue=="")
{
flag=0;
}
str=document.myform.title.value;
if(str=="c"||str=="C")
{
document.myform.t1.value="C";
document.myform.t2.value=444;
}
elseif(str=="jsp"||str=="JSP")
else{
document.myform.t1.value="JSP";document.myform.t2.value=555;
}
{
flag=0;
}
Shopping cart:
Catalog.html:
<html>
<bodybgcolor="pink"><br><br><br>
<script
language="javascript
"> functionvalidate()
{
var flag=1;
if(document.myform.id.value
==""||
document.myform.title.valu
e==""||
document.myform.no.valu
e==""||
document.myform.cost.val
ue=="")
{
flag=0;
}
str=document.myform.title.value;
varstr1=document.myform.cost.value;
if(!((str=="c"&&str1==444) ||(str=="jsp"&&str1==555)))
{
flag=0;
}
if(flag==1)
{
alert("VALIDINPUT");
else
}
{
alert("INVALIDINPUT");
document.myform.focus();
}
</script>
<formname="myform"action="scart.html"target="right">
<divalign="center"><pre>
LOGINID
:<inputtype="text"name="id"><br>TI
TLE :<input
type="text"name="title"><br>NO.OFBOOKS
:<input type="text"name="no"><br>
COSTOFBOOK :<inputtype="text"name="cost"><br>
</pre><br><br>
</div>
<br><br>
<divalign="center">
<inputtype="submit"value="ok"onClick="validate()">
<inputtype="reset" value="clear">
</form>
</body>
</html>
Main.html
Login.html:
Catalog.html:
Scart.html:
Payment.html:
Order.html
7. Create and save an XML document on the server, which contains 10 users information.
Write a program, which takes User Id as an input and returns the user details by taking the
user information from the XML document.
<employees>
<employeeid="111">
<firstName>Chandrika</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employeeid="222">
<firstName>Srinivas</firstName>
<lastName>Reddy</lastName>
<location>Russia</location>
</employee>
<employeeid="333">
<firstName>Anupama</firstName>
<lastName>P</lastName>
<location>USA</location>
</employee>
<employeeid="444">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employeeid="555">
<firstName>Vishnu</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employeeid="666">
<firstName>Veeru</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
<employeeid="777">
<firstName>Pavan</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
<employeeid="888">
<firstName>Narayana</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employeeid="999">
<firstName>David</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
<employeeid="1000">
<firstName>Sunder</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
</employees>
Read XML.java:
import org.w3c.dom.*;
importjavax.xml.parsers.*;
import java.io.*;
importjava.util.Scanner;
publicclassReadXML{
publicstaticvoidmain(Stringa[])throwsException{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
//BuildDocument
Documentdocument=builder.parse(new
File("C:\\Users\\Narayana\\Desktop\\employees.xml"));
//Normalize the XML Structure;It's just too
important!!document.getDocumentElement().normalize();
//Herecomestheroot node
Elementroot= document.getDocumentElement();
//Getallemployees
NodeListnList=document.getElementsByTagName("employee");
System.out.println("enter employee id:");
Scanners=newScanner(System.in);
String id=s.next();
for(inttemp =0;temp <nList.getLength();temp++)
{
Nodenode=nList.item(temp);
if(node.getNodeType() == Node.ELEMENT_NODE)
{
ElementeElement=(Element)node;
if(eElement.getAttribute("id").equals(id)){
System.out.println("FirstName:"+
eElement.getElementsByTagName("firstName").item(0).getTextContent());
System.out.println("Last Name:" +
eElement.getElementsByTagName("lastName").item(0).getTextContent());
System.out.println("Location:" +
eElement.getElementsByTagName("location").item(0).getTextContent());
}
}
}
}
}
OUTPUT:-
EnterEmployeeId:222
8.Install TOMCAT web server. Convert the static web pages of assignments 2 into
dynamic web pages using servlets and cookies. Hint: Users information (user id, password,
credit card number) would be stored in web.xml. Each user should have a separate
Shopping Cart.
PROCEDURE:
Firstinstallthetomcatintothe system.
Thenmakeasubdirectly(eg.,tr) inthe\tomcat\webapps.
Under tr create WEB-INF directoryand also place the html files in this tr
directoryonly.Next underWEB-INFcreatetwosubclasseslib,classesand
web.xml
Next placealltheclassfilesundertheclassesand jarfiles(servlet-api.jar,classes12.jaretc…) under lib
subdirectories.
Afterthisstarttomcat bygivingthefollowingcommandatthe
instll_dir>tomcat>bin Catalina.bat run
AttheI.E(webbrowser)givetheurlashttp;//localhost:8080//tr/htmlfileorservlet url pattern
Portno 8080 is assigned for the tomcat.
Web.xml
<?xmlversion="1.0"encoding="iso-8859-1"?>
<!DOCTYPEweb-app
PUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication
2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Servlet2.4Examples</display-name>
<description>
Servlet2.4Examples.
</description>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>reg</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
</servlet>
<servlet>
<servlet-name>profile</servlet-name>
<servlet-class>profile</servlet-class>
</servlet>
<servlet>
<servlet-name>catalog</servlet-name>
<servlet-class>catalog</servlet-class>
<servlet-mapping>
<servlet-name>order</servlet-name>
<url-p</servlet>
<servlet>
<servlet-name>order</servlet-name>
<servlet-class>order</servlet-class>
</servlet>attern>order<
/url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>catalog</servlet-name>
<url-pattern>catalog</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>profile</servlet-name>
<url-pattern>profile</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>reg</url-pattern>
</servlet-mapping>
</web-app>
Main.html
<!DOCTYPE htmlPUBLIC"-//W3C//DTDXHTML1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<bodybgcolor="pink">
<br/><br/><br/><br/><br/>
<h1align="center"><U>ONLINEBOOKSTORAGE</U></h1><br/><br/><br/>
<h2align="center"><pre>
<b>Welcome to online book
storage.PressLOGINifyouare
having id otherwise press
REGISTRATION
</b></pre></h2>
<br/><br/><pre>
<divalign="center"><ahref="/tr/login.html">LOGIN</a><a
href="/tr/reg.html"> REGISTRATION</a></div></pre>
</body>
</html>
Login.html
<html>
<bodybgcolor="pink"><br/><br/><br/>
<formname="myform"method="post"action="/tr/login">
<divalign="center"><pre>
LOGINID:<inputtype="text"name="id"/><br />
PASSWORD:<inputtype="password"name="pwd"/></pre><br/><br/>
</div>
<br/><br/>
<divalign="center">
<inputtype="submit"value="ok"onclick="validate()"
/> <inputtype="reset"
value="clear" />
</div>
</form>
</body>
</html>
Reg.html
<!DOCTYPE htmlPUBLIC"-//W3C//DTDXHTML1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<bodybgcolor="pink"><br/><br/>
<formname="myform"method="post"action="/tr/reg">
<divalign="center"><pre>
NAME :<inputtype="text"name="name" /><br/>
ADDRESS :<inputtype="text"name="addr"
/><br /> CONTACTNUMBER :<input type="text"
name="phno" /><br /> LOGINID :<inputtype="text"name="id"
/><br/>
PASSWORD :<inputtype="password"name="pwd"/></pre><br/><br/>
</div>
<br/><br/>
<divalign="center">
<inputtype="submit"value="ok"onclick="validate()"
/> <inputtype="reset"
value="clear" />
</div>
</form>
</body>
</html>
Profile.html
<!DOCTYPE htmlPUBLIC"-//W3C//DTDXHTML1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<bodybgcolor="pink"><br/><br/><br/>
<formname="myform"method="post"action="/tr/profile">
<divalign="center"><pre>
LOGINID:<inputtype="text"name="id"/><br />
</pre><br/><br/>
</div>
<br/><br/>
<divalign="center">
<inputtype="submit"value="ok"onclick="validate()"
/> <inputtype="reset"
value="clear" />
</div></form></body></html>
Catalog.html
<!DOCTYPE htmlPUBLIC"-//W3C//DTDXHTML1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<bodybgcolor="pink"><br/><br/><br/>
<formmethod="post"action="/tr/catalog">
<divalign="center"><pre>
BOOKTITLE:<inputtype="text"name="title"/><br />
</pre><br/><br/>
</div>
<br/><br/>
<divalign="center">
<inputtype="submit"value="ok"name="button1"/>
<inputtype="reset" value="clear"name="button2"/>
</div>
</form>
</body></html>
Order.html
<!DOCTYPEhtmlPUBLIC"-
//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/
DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<bodybgcolor="pink"><br/><br/>
<formmethod="post"action="/tr/reg">
<divalign="center"><pre>
NAME :<inputtype="text"name="name"
/><br /> PASSWORD :<input type="password"
name="pwd" /> TITLE :<inputtype="text"name="title"
/><br/>NO.OFBOOKS :<inputtype="text"name="no"
/><br/>DATE :<inputtype="text"name="date"
/><br/>
CREDITCARDNUMBER:<input type="password"name="cno"/><br/></pre><br/><br/>
</div>
<br/><br/>
<divalign="center">
<inputtype="submit"value="ok"
name="button1"/> <inputtype="reset"
value="clear" name="button2"/>
</div>
</form>
</body>
</html>
Login.java
importjava.sql.*;
import java.io.*;
importjava.util.*;
import
javax.servlet.*;
importjavax.servlet.http.*;
publicclass loginextendsHttpServlet
{
publicvoidservice(HttpServletRequestreq,HttpServletResponse
resp) throws ServletException,IOException
{ PrintWriter
pw=resp.getWriter();
pw.println("<html><bodybgcolor=\"pink\");
Stringid=req.getParamenter("id");
String
pwd=req.getParameter("pwd");try
{ Driver
d=neworacle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger
")
;
Statement stmt=con.createStatement();
String sqlstmt="select id,password from
login";
ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next(
))
{
if(id.equal(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
}
if(flag==0)
{
}
else
{
pw.println("SORRYINVALIDIDTRYAGAINID<br><br>");
pw.println("<ahref=\"/tr/login.html\">pressLOGINtoRETRY</a>");
pw.println("VALIDLOGINID<br><br>");
pw.println("<h3><ul>");
pw.println("<li><ahref=\"profile.html\"><fontcolor=\"black\">USERPROFILE</font>
</a></li><br><br>");
pw.println("<li><ahref=\"catalog.html\"><fontcolor=\"black\">BOO
KS CATALOG</font></a></li><br><br>");
pw.println("<li><ahref=\"order.html\"><fontcolor=\"black\">OR
DER CONFIRMATION</font> </a></li><br><br>");
}
pw.println("</body></html>");
}
catch(Exceptione)
{ resp.sendError(500,e.toString());
}
}
Reg.html
import java.sql.*;
import java.io.*;import
java.util.*; import
javax.servlet.*;
importjavax.servlet.http.*;
publicclass loginextendsHttpServlet
{
publicvoidservice(HttpServletRequestreq,HttpServletResponse
resp) throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body
bgcolor=\"pink\"); String
name=req.getParamenter("name");
String addr=req.getParameter("addr");
Stringphno=req.getParameter("phno");
Stringid=req.getParamenter("id");
String
pwd=req.getParameter("pwd");int
no=Integer.parseInt(phno);
try
{
Driverd=neworacle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger
")
;
Statement stmt=con.createStatement();
String sqlstmt="select id,password from
login";
ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next(
))
{
if(id.equal(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
} }
if(flag==1)
{
pw.println("SORRYINVALIDIDALREADYEXITSTRYAGAINWITHNEW
ID<br><br>");
pw.println("<ahref=\"/tr/reg.html\">pressREGISTERtoRETRY</a>");
}
else
{ Statement
stmt1=con.createStatement();
stmt1.executeUpdate("insertintologin
values("+names","+addr+","+no+","+id+","+pwd+")");
pw.println("YOUR DETAILS
AREENTERED<br><br>");
pw.println("<ahref=\"/tr/login.html\">pressLOGINtologin</a>");
}
pw.println("</body></html>");
}
catch(Exceptione)
{ resp.sendError(500,e.toString());
}}}
Catlog.java
importjava.sql.*;
import java.io.*;
importjava.util.*;
import
javax.servlet.*;
importjavax.servlet.http.*;
publicclass loginextendsHttpServlet
{
publicvoidservice(HttpServletRequestreq,HttpServletResponse
resp) throws ServletException,IOException
{
PrintWriterpw=resp.getWriter();
pw.println("<html><body
bgcolor=\"pink\"); String
title=req.getParameter("title");try
{
Driverd=neworacle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger
")
;
Statement stmt=con.createStatement();
String sqlstmt="select id,password from
login";
ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next(
))
{
pw.println(",divalign=\"center\">");
pw.println("TITLE
:"+rs.getString(1)+"<br>")
; pw.println("AUTHOR :"+rs.getString(2)+"<br>");
pw.println("VERSION :"+rs.getString(3)+"<br>");
pw.println("PUBLISHER
:"+rs.getString(4)+"<br>
");pw.println("COST
:"+rs.getString(5)+"<br>
");pw.println("</div");
flag=1;
}
if(flag==0)
{
pw.println("SORRYINVALIDTITLETRYAGAIN<br><br>");
pw.println("<ahref=\"/tr/catalog.html\">pressHEREtoRETRY</a>");
}
pw.println("</body></html>");
}
catch(Exceptione)
{
resp.sendError(500,e.toString());
}
}
}
Profile.java
import java.sql.*;
import java.io.*;
importjava.util.*;
import
javax.servlet.*;
importjavax.servlet.http.*;
publicclass loginextendsHttpServlet
{
publicvoidservice(HttpServletRequestreq,HttpServletResponse
resp) throws ServletException,IOException
{
PrintWriterpw=resp.getWriter();
pw.println("<html><body
bgcolor=\"pink\"); String
id=req.getParamenter("id");
try
{
Driverd=neworacle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:ora
cle:thin:
@localhost:1521:orcl","scott","tiger");
Statement stmt=con.createStatement();
Stringsqlstmt="select*fromloginwhere
id="+id+"";
ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
pw.println("<br><br><br>");
while(rs.next())
{
pw.println("<divalign=\"center\">");
pw.println("NAME
:"+rs.getString(1)+"<br>"
);
pw.println("ADDRESS:"+rs.getString(2)+"<br
>");
pw.println("PHONENO
:"+rs.getString(3)+"<br>"
);pw.println("</div>");
flag=1;
}
if(flag==0)
{
pw.println("SORRYINVALIDIDTRYAGAINID<br><br>");
pw.println("<ahref=\"/tr/profile.html\">pressHEREto RETRY</a>");
}
pw.println("</body></html>");
}
catch(Exceptione)
{
resp.sendError(500,e.toString());
}
}
}
Order.java 33
import java.sql.*;
import java.io.*;
importjava.util.*;
import
javax.servlet.*;
importjavax.servlet.http.*;
publicclass loginextendsHttpServlet
{
publicvoidservice(HttpServletRequestreq,HttpServletResponse
resp) throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body
bgcolor=\"pink\");
Stringid=req.getParamenter("id");
String
pwd=req.getParameter("pwd");
String
title=req.getParameter("title");
String
count1=req.getParameter("no");
String
date=req.getParameter("date");
String
cno=req.getParameter("cno");
intcount=Integer.parseInt(count1);
try
{
Driverd=neworacle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger
")
;
Statement stmt=con.createStatement();
Stringsqlstmt="selectid,passwordfrom
login";
ResultSetrs=stmt.executeQuery(sqlstmt);
int
flag=0,amount,x;
while(rs.next())
{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
}
if(flag==0)
{
pw.println("SORRYINVALIDIDTRYAGAINID<br><br>");
pw.println("<ahref=\\"/tr/order.html\\">pressHEREtoRETRY</a>");
}
else
{
Statement stmt2=con.createStatement();
Strings="selectcostfrombookwheretitle="+title+"";ResultSet
rs1=stmt2.executeQuery(s);
intflag1=0;while(rs1.next())
{
flag1=1;
x=Integer.parseInt(rs1.getString(
1));
amount=count*x;
pw.println("AMOUNT:"+amount+"<br><br><br><br>");
Statement stmt1=con.createStatement();
stmt1.executeUpdate("insertintodetailsvalues('"+id+",'"+title
+"'+amount+'","'+cno+'")"'); pw.println("YOUR ORDER has
taken<br>");
}
if(flag1==0)
{
pw.println("SORRYINVALIDIDTRYAGAINID<br><br>");
pw.println("<ahref=\\"/tr/order.html\\">pressHEREtoRETRY</a>");
}
}
pw.println("</body></html>");
con.close();
}
catch(Exceptione)
{
resp.sendError(500,e.toString());
}
}
35
36
9. Redo the previous task using JSP by converting the static web pages of assignments 2
into dynamic web pages. Create a database with user information and books information.
The books catalogue should be dynamically loaded from the data base. Follow the MVC
architecture while doing the website.
PROCEDURE:
1) Createyourowndirectoryundertomcat/webapps(e.g.tr1)
2) Copythe htmlfilesintr1
3) Copythejspfilesalsointotr1
4) Starttomcatgivethefollowing
command Catalina.batrun
Atinstall‐dir/bin
5) at I.Egiveurlashttp://localhost:8081/tr1/main.html
Main.html:
<html>
<bodybgcolor=”pink”>
<br><br><br><br><br><br>
<h1align=”center”>>U>ONLINEBOOKSTORAGE</u></h1><br><br><br>
<h2align=”center”><PRE>
<b>Welcometoonlinebook
storage. Press LOGIN if
you are having id
OtherwisepressREGISTRATION
</b></PRE></h2>
<br><br><pre>
<div align=”center”><a
href=”/tr/login.html”>LOGIN</a>href=”/tr/log
in.html”>REGISTRATION</a></div></pre>
</body></html>
Login.html:
<html>
<bodybgcolor=”pink”><br><br><br>
<formname="myform"method="post"action=/tr1/login.jsp">
<divalign="center"><pre>
LOGIN ID :<input type="passwors"
name="pwd"></pre><br><br>PASSWORD:<input
type="password" name="pwd"></pre><br><br>
</div>
<br><br>
<divalign="center">
<inputtype="submit"value="ok"onClick="validate()">
<inputtype="reset"value="clear">
</form>
</body></html>
Reg.html:
<html>
<bodybgcolor="pink"><br><br>
<formname="myform"method="post"action="/tr1/reg.jsp">
<div align="center"><pre>
NAME:<inputtype="text"
name="name"><br> ADDRESS :<input
type="text"name="addr"><br>CONTACT
NUMBER:<inputtype="text"
name="phno"><br> LOGINID : <input
type="text"name="id"><br>
PASSWORD:<inputtype="password"name="pwd"></pre><br><br>
</div>
<br><br>
<divalign="center">
<inputtype="submit"value="ok"
onClick="validate()">()"> <input
type="reset" value="clear">
</form>
</body>
</html>
Profile.html:
<html>
<bodybgcolor="pink"><br><br>
<formname="myform"method="post"action="/tr1/profile.jsp">
<divalign="center"><pre>
LOGINID :<inputtype="text"name="id"><br>
</pre><br><br>
</div>
<br><br>
<divalign="center">
<inputtype="submit"value="ok"
onClick="validate()">()"> <input
type="reset" value="clear">
</form>
</body>
</html>
Catalog.html:
<html>
<bodybgcolor="pink"><br><br><br>
<formmethod="post"action="/tr1/catalog.jsp">
<divalign="center"><pre>
BOOKTITLE:<inputtype="text" name="title"><br>
</pre><br><br>
</div>
<br><br>
<divalign="center">
<inputtype="submit"value="ok"
name=”button1”> <inputtype="reset"value="clear"
name=”button2
”>
</form>
</body>
</html>
Order.html:
<html>
<bodybgcolor="pink"><br><br><br>
<formmethod="post"action="/tr1/order.jsp">
<div align="center"><pre>
LOGINID:<inputtype="text"
name="id"><br>PASSWORD :<input
type="password"name="pwd"><br>TITLE
:<input
type="text"name="title"><br>
NO. OF BOOKS :<input type="text"
name="no"><br> DATE :<input
type="text"name="date"><br>
CREDITCARDNUMBER:<inputtype="password"name="cno"><br></pre><br><br>
</div>
<br><br>
<divalign="center">
<input type="submit" value="ok"name=”button1”> <input
type="reset" value="clear"name=”button2”>
</form>
</body>
</html>
Login.jsp: 41
%@page import=”java.sql.*”%
%@page import=”java.io.*”%
<%
out.println(“<html><body
bgcolor=\”pink\”>”);String
id=request.getParameter(“id”);
String
pwd=request.getParameter(“pwd”);
Driver d=new
oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
Statement stmt=con.createStatement();
Stringsqlstmt=”selectid,passwordfromloginwhereid=”+id+”and
password=”+pwd+””; ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.nex
t())
{
flag=1;
}
if(flag==0)
{
out.println(“SORRYINVALIDIDTRYAGAINID<br><br>”);
out.println(“<ahref=\”/tr1/login.html\”>pressLOGINtoRETRY</a>”);
}
else
{
out.println(“VALIDLOGINID<br><br>”);
out.println(“<h3><ul>”);
out.println(“<li><ahref=\”profile.html\”><fontcolor=\”black\”>USER
PROFILE</font></a></li><br><br>”);
out.println(“<li><ahref=\”catalog.html\”><fontcolor=\”black\”>BOOKS
CATALOG</font></a></li><br><br>”);
out.println(“<li><ahref=\”order.html\”><fontcolor=\”black\”>ORDER
CONFIRMATION</font></a></li><br><br>”)
; out.println(“</ul>”);
}
out.println(“<body></html>”);
%>
42
Reg.jsp:
%@page import=”java.sql.*”%
%@page import=”java.io.*”%
<%
out.println(“<html><body
bgcolor=\”pink\”>”); String
name=request.getParameter(“name”);
String
addr=request.getParameter(“addr”);
String
phno=request.getParameter(“phno”);
Stringid=request.getParameter(“id”);
String
pwd=request.getParameter(“pwd”);
int no=Integer.parseInt(phno);
Driver d=new
oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection con=
DriverManager.getConnection
(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”); Statement
stmt=con.createStatement();
String sqlstmt=”select id from
login”;
ResultSetrs=stmt.executeQuery(sql
stmt); int flag=0;
while(rs.next())
{
if(id.equals(rs.getString(1)))
{
flag=1;
}
}
if(flag==1)
{
out.println(“SORRYLOGINIDALREADYEXISTSTRYAGAINWITHNEWID<br><br>”);
out.println(“<ahref=\”/tr1/reg.html\”>pressREGISTERto RETRY</a>”);
}
else
{
Statement stmt1=con.createStatement ();
stmt1.executeUpdate(“insertintologinvalues
(“+name+”,”+addr+”,”+no+”,”+id+”,”+pwd+”)”);out.println(“YOUDETAILS ARE
ENTERED <br><br>”);
out.println(“<ahref=\”/tr1/login.html\”>pressLOGINtologin</a>”);
}
out.println(“</body></html>”);
%>
Profile.jsp: 43
<%@pageimport=”java.sql.*”%>
<%@pageimport=”java.io.*”%>
<%
out.println (“<html><body
bgcolor=\”pink\”>”); String
id=request.getParameter(“id”);
Driver d=new
oracle.jdbc.driver.OracleDriver();
DriverManager.regiserDriver(d);
Connectioncon=
DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”s
cott”,”tiger”); Statement stmt=con.createStatement ();
String sqlstmt=”select * from login where
id=”+id+””;ResultSetrs=stmt.executeQuery
(sqlstmt);
int
flag=0;
while(rs.next())
{
out.println(“<divalign=\”center\”>”);
out.println(“NAME
:”+rs.getString(1)+”<br>
”); out.println (“ADDRESS
:”+rs.getString(2)+”<br>”); out.println
(“PHONENO:”+rs.getString(3)+”<br>”);
out.println (“</div>”);
flag=1;
}
if(flag==0)
{
out.println(“SORRYINVALIDIDTRYAGAINID<br><br>”);
out.println(“<ahref=\”/tr1/profile.html\”>pressHEREtoRETRY</a>”);
}
out.println(“</body></html>”);
%>
Catalog.jsp:
<%@pageimport=”java.sql.*”%>
<%@pageimport=”java.io.*”%>
<%
out.println (“<html><body
bgcolor=\”pink\”>”); String
title=request.getParameter(“title”);
Driver d=new
oracle.jdbc.driver.OracleDriver ();
DriverManager.regiserDriver (d);
Connection con=
DriverManager.getConnection
(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”); Statement
stmt=con.createStatement ();
String sqlstmt=”select * from book where
title=”+title+””;ResultSetrs=stmt.executeQuery
(sqlstmt);
int
flag=0;
while(rs.nex
t())
{
out.println(“<divalign=\”center\”>”);
out.println(“TITLE
:”+rs.getString(1)+”<br
>”);out.println(“AUTHOR
:”+rs.getString(2)+”<br>”); out.println
(“VERSION:”+rs.getString(3)+”<br>”);
out.println (“PUBLISHER :”
+rs.getString(4)+”<br>”);out.println
(“COST:”+rs.getString(5)+”<br>”);
out.println (“</div>”);
flag=1;
}
if(flag==0)
{
out.println(“SORRYINVALIDIDTRYAGAINID<br><br>”);
out.println(“<ahref=\”/tr1/catalog.html\”>pressHEREtoRETRY</a>”);
}
out.println(“</body></html>”);
%>
Order.jsp:
<%@pageimport=”java.sql.*”%>
<%@pageimport=”java.io.*”%>
<%
out.println (“<html><body
bgcolor=\”pink\”>”); String
id=request.getParameter (“id”);
String pwd=request.getParameter
(“pwd”); String
title=request.getParameter (“title”);
String count1=request.getParameter
(“no”); String
date=request.getParameter (“date”);
String cno=request.getParameter
(“cno”); int
count=Integer.parseInt(count1);
Driver d=new
oracle.jdbc.driver.OracleDriver ();
DriverManager.regiserDriver (d);
Connection con=
DriverManager.getConnection
(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”); Statement
stmt=con.createStatement ();
Stringsqlstmt=”selectid,password
from login”;
ResultSetrs=stmt.executeQuery
(sqlstmt);
int
flag=0,amount,x;
while(rs.next())
{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
}
if(flag==0)
{
out.println(“SORRYINVALIDIDTRYAGAINID<br><br>”);
out.println(“<ahref=\”/tr1/order.html\”>pressHEREto RETRY</a>”);
}
els
e{
Statementstmt2=con.createStatement();
String s=”select cost from book where
title=”+title+””; ResultSet
rs1=stmt2.executeQuery(s);
int flag1=0;
{ while(rs1.nex
t())
flag1=1;
x=Integer.parseInt(rs1.getStri
ng(1)); amount=count*x;
out.println(“AMOUNT
:”+amount+”<br><br><br><br>”); Statement
} stmt1=con.createStatement ();
stmt1.executeUpdate (“insert into details
(“+id+”,”+title+”,”+amount+”,”+date+”,”+cno+”)”);out.println(“YOU ORDER
HAS TAKEN<br>”);
if(flag1==0)
{
out.println(“SORRYINVALIDBOOKTRYAGAIN<br><br>”);
out.println(“<ahref=\”/tr1/order.html\”>pressHEREtoRETRY</a>”);
}
} out.println(“</body></html>”);%>