WT Lab Manual

You might also like

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

INDEX

S.No

Name of the experiment

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. Home page, Registration and user
Login, User profile page, Books catalog, Shopping cart,
Payment By credit card, order confirmation.
Validate the registration, user login, user profile and
payment by credit card pages using JavaScript.

2
3

4
5

Date

Signature

Create and save an XML document at the server, which


contains 10 users information. Write a program which
takes User Id as input and returns the user details by
taking the user information from the XML document.
Bean Assignments
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.

1.Develop static pages (using only html) of an online, book store. The pages should
resemble www.amazon.com. The website should consist of following pages.
1) HOME PAGE:
The static home page must contain three frames.
Top frame: Logo and the college name and links to Home page, Login page, Registration page,Catalogue
page and Cart page (the description of these pages will be given below).
Left frame: At least four links for navigation, which will display the catalogue of respective links.For e.g.:
When you click the link CSE the catalogue for CSE Books should be displayed in the Right frame.
Right frame: The pages to the links in the left frame must be loaded here. Initially this page contains
Description of the web site.

Web Site Name


Logo
Home

Login

Registration

CSE
ECE
EEE
CIVIL

Catalogue

Cart

Description of the Web Site

2) LOGIN PAGE:

This page looks like below:


Logo
Home
CSE
ECE
EEE
CIVIL

Web Site Name


Login

Registration

Catalogue

Cart

Login :
Password:

Submit

Reset

3) CATOLOGUE PAGE:
The catalogue page should contain the details of all the books available in the web site in a table.
The details should contain the following:
1. Snap shot of Cover Page.
2. Author Name.
3. Publisher.
4. Price.
5. Add to cart button.
Logo
Home

Login

Web Site Name


Registration
Catalogue

Cart

CSE

Book : XML Bible


Author : Winston
Publication : Wiely

ECE

$ 40.5

EEE
Book : AI
Author : S.Russel
Publication : Princeton
hall

$ 63

4) CART PAGE:
The cart page contains the details about the books which are added to the cart.
The cart page should look like this:

Web Site Name


Logo
Home

CSE
ECE
EEE
CIVIL

Login

Registration

Book name

Price

Java 2
XML bible

$35.5
$40.5
Total amount -

Catalogue

Quantity
2
1

Cart

Amount
$70
$40.5

$130.5

Index.html
<html>
<head>
<title> Welcome to AMAZON Ebook's website</title>
</head>
<frameset rows="108,*" >
<frame name=top src="top.html" noresize frameborder=0 scrolling=no />
<frameset cols="80,*">

<frame name=left noresize scrolling=no frameborder=0


src="left.html">
<frame name=right noresize frameborder=0 src="right.html">
</frameset>
</frameset></html>
Top.html
<html>
<head>
<title> Welcome to AMAZON website</title>
<style type="text/css">
td{
cursor:pointer; color: #000066; font-weight:bolder; border:#665599 1 solid ;
}
marquee{background: #556677; color:#f5f5f5}
</style>
</head>
<body bgcolor=#D6E9EF>
<table width=100% align="center" cellpadding="0" cellspacing="2">
<tr>
<th width=20%><img src="images\amazon.jpeg" alt=logo /></th>
<th colspan=4> <h2><marquee scrollamount=2 scrolldelay=10 behavior=alternate>
WORLD BEST ONLINE EBOOKS WEBSITE
</marquee></h2></th></TR>
<tbody align=center bgcolor=#Ffdfdf>
<tr>
<a href="right.html" target="right"><td width=20%>Home</td></a>
<a href="login.html" target="right"><td width=20%>Login</td></a>
<a href="registration.html" target=right><td width=20%>Registration</td></a>
<a href="Catalogue.html" target=right><td width=20%>Catalogue</td></a>
<a href="cart.html" target=right><td width=20%>Cart</td></a>
</tr> </tbody>
</table> <hr/>
</body>
</html>
left.html
<html>
<body bgcolor=#D6E9EF>
<table align=center>
<tbody align=center>
<tr><td><a href="cse.html" target="right">CSE</a></td>
<tr><td><a href="ece.html" target="right">ECE</a></td>

<tr><td><a href="eee.html" target="right">EEE</a></td>


</tbody>
</table>
</body> </html>

Style.css
h3{color: #006699; font-family: Geneva, Arial, Helvetica, sans-serif;} p
{color: #003366;}
td{color: #003366;} th{color:#FFFFFF;} body{background-color:#F6F6F0;}

right.html
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<h3>Welcome to Amazon Website</h3>
<p>Shopping at <font size=5>AMAZON</font> can be both <font
size=5>fun</font> and <font size=5>savings</font>.</br>Shop with us in
this special <font size=5>discount</font> season and save upto <font
size=5>50%</font> on all your purchases.</br></p>
<br/><br/><br/><br/><br/><br/><br/><br/><h4><font color="red">
(C) 2008 All rights reserved by Amazon ebooks </body>
</html>

Index.html Output:

Login.html
<html>

<head>
<title> Welcome to AMAZON website</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body >
<center>
<h3> Enter Login Details</h3>
<br/>
<form name="f1" onsubmit="return form1_validation()">
<table align="center" >
<tr>
<td> User ID</td>
<td> <input type="text" name="username"></td>
</tr>
<tr>
<td> Password</td>
<td> <input type="password" name="password"></td>
</tr>
<tr><td></td>
<td><input type="submit" value="submit">
<input type="reset" value="reset"></td>
</tr>
<tr>
<td colspan=2>Forgot my User ID or Password</td>
</tr>
</table>
</html>

Output:

Registration.html
<html>
<head>
8

<td>

<title> Welcome to AMAZON website</title>


<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="validation.js">
</script>
</head>
<body>
<center>
<h3>Registration Form
</h3>
<br/>
<form name="f1" onsubmit="return form1_validation()">
<table cellpadding="1" align="center" >
<tr> <td> Name:*</td>
<td> <input type="text" name="username"></td></tr>
<tr> <td>Password:*</td>
<td> <input type="password" name="password"></td></tr>
<tr> <td>Email ID:*</td>
<td> <input type="text" name="email"></td></tr>
<tr> <td>Phone Number:*</td>
<td> <input type="text" name="phno"></td></tr>
<tr> <td valign="top">Gender:*</td>
<td> <input type="radio" name="radio" value="1">Male<br>
<input type="radio" name="radio" value="2">Female</td></tr>
<tr> <td>Date of Birth:*</td>
<td> dd: <select name=dd >
<option> </option>
<script type="text/javascript">
for(i=1;i<=31;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select>
mm: <select name=mm>
<option> </option>
<script type="text/javascript">
for(i=1;i<=12;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select>
yyyy: <select name=yyyy>
<option> </option>
<script type="text/javascript">
for(i=1880;i<=2008;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select>
</td></tr>
<tr> <td valign="top">Language Known:*</td>
<input type="checkbox" name="checkbox" value="English">English<b
<input type="checkbox" name="checkbox" value="Telugu">Telugu<br>
<input type="checkbox" name="checkbox" value="Hindi">Hindi<br>

<input type="checkbox" name="checkbox" value="Tamil">Tamil


</td></tr>
<tr> <td valign="top">Address:*</td>
<td><textarea name="address"></textarea></td>
<tr><td></td><td><input type="submit" value="submit" hspace="10">
<input type="reset" value="reset"></td></tr>
<tr> <td colspan=2 >*<font color="#FF0000">fields are mandatory</font>
</td>
</tr>
</table>
</center>
</body>
</html>

Output:

10

Catalogue.html

11

<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<h3>Ebooks Catalogue</h3>
<table width=90% align="center" bgcolor="#E1EEFF">
<tbody bgcolor="#999999">
<tr> <th><hr>Book <hr></th>
<th><hr>Details<hr></th>
<th><hr>Price<hr></th>
<th><hr>Options<hr></th> </tr>
</tbody>
<tbody align="center">
<tr> <td><img src="images/000327738-min-1.jpg" ></td>
<td> Title: Office 2007 Server<br/> Author: Raja<br/>
Publicher:Tata Mc.Grawhill</td>
<td>Rs. 1200/-</td>
<td> <input type="button" value="Add to cart"></td>
</tr>
<tr> <td><img src="images/000266974-min-1.jpg"></td>
<td>Book: Electronics Projects <br/>Author: Robert Tom
<br>
Public: Indian Ebook Mfg. <br><td align="center">Rs. 1000/</td>
<td align="center"> <input type="button" value="Add to cart"></td>
</tr>
<tr> <td align="center"><img src="images/000331427-min-1.jpg" ></td>
<td>Book: Solar Power Your Home <br/>Author: Albert<br>
Publicher: Andhra Mc.hill<br> <td align="center">Rs. 9000/-</td>
<td align="center"> <input type="button" value="Add to cart"></td>
</tr>
</tbody>
</table>
</center>
<p>For more ebooks catagory, visit side panel links</p>
</body>
</html>

12

Output:

13

Cart.html
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<h3>Cart</h3>
<table width=90% align="center" bgcolor="#E1EEFF">
<tbody bgcolor="#999999">
<tr> <th width=40%><hr>BookName<hr></th>
<th width=20%><hr>Price<hr></th>
<th width=20%><hr>Quantity<hr></th>
<th width=20%><hr>Amount<hr></th> </tr>
</tbody>
<tbody align=center>
<tr> <td>Office 2007 Server </td>
<td>Rs. 1200/-</td>
<td>2</td>
<td>Rs. 2400/-</td></tr>
<tr>
<td>Solar Power Your Home</td>
<td>Rs. 9000/-</td>
<td>1</td>
<td>Rs. 9000/-</td></tr>
<tr >
<td> </td>
<td ><hr><font color="#996600">Total
Amount:</font><hr></td>
<td><hr>3<hr></td>
<td><hr>Rs. 11400/-<hr></td> </tr>
<tr>
<td></td>
<td></td>
<td></td>
<td bgcolor="#FFCCCC"><a href="catalogue.html">Continue
Shopping...</a></td>
</tbody>
</table>
</body>
</html>

14

Output:

15

CSE.html
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<h3>Computer Science & Engineering Catalogue </h3>
<table width=90% align="center" bgcolor="#E1EEFF">
<tbody bgcolor="#999999">
<th width=25% >BOOK</th>
<th width="35%">DETAILS</th>
<th width="20%">PRICE</th>
<th width="20%">OPTION</th></tbody>
<tbody align="center">
<tr>
<td align="center"><img src="images/000327738-min-1.jpg" width="90"
height="120" ></td>
<td align="center"> Title: Office 2007 Server<br> Author: B.R.Prathap
<br/> Publicher:Tata Mc.Grawhill</td>
<td align="center">Rs. 1200/-</td>
<td align="center"> <input type="button" value="Add to cart"></td>
</tr>
<tr>
<td align="center"><img src="images/it1.jpg" width="90" height="120"></td>
<td align="center"> Title: Electronics Projects<br>Author: Prathap
r<br/> Publicher:Tata Mc.Grawhill</td>
<td align="center">Rs. 1000/-</td>
<td align="center"> <input type="button" value="Add to
cart"></td> </tr>
<tr>
<td align="center"><img src="images/java1.gif" width="90"
height="120" ></td>
<td align="center"> Title: Solar Power Your Home<br/>Author:
Jhanaki Ramaya Sir <br/>Publicher:Tata Mc.Grawhill</td>
<td align="center">Rs. 9000/-</td>
<td align="center"> <input type="button" value="Add to
cart"></td> </tr>
</tbody>
</table>
</center>
</body>
</hmtl>
Output:

16

ECE.html
<html>
17

<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor=#ffffa0>
<center>
<h3>Electronics Communication Engineering Catalogue </h3>
<table width=90% align="center" bgcolor="#E1EEFF">
<tbody bgcolor="#999999">
<tr>
<th width=25% >BOOK</th>
<th width="35%">DETAILS</th>
<th width="20%">PRICE</th>
<th width="20%">OPTION</th> </tr> </tbody>
<tr> <tbody align=center>
<td><img src="images/000266974-min-1.jpg" height=120
width=90></img></td>
<td>Book: Electronics Projects <br> Author: Robert
Tom <br>
Public: Indian Ebook Mfg. <br> </td>
<td align="center">Rs. 1000/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
<tr>
<td><img src="images\digital.jpg" height=120
width=90></img></td>
<td>Book: Digital Communication System <br>
Author: Howel Albert <br>
Public: Tata Mc.Grawhill<br>
</td>
<td align="center">Rs. 1000/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
<tr>
<td><img src="images\analog.jpg" height=120
width=90></img></td>
<td>Book: Digital and Analog
Communications<br>Author: Weinberg<br>
Public: Tata Mc.Grawhill<br>
</td>
<td align="center">Rs 1200/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
</tbody>
</table>
</center>
</body>
</html>
Output:

18

EEE.html
<html>

19

<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor=#ffffa0>
<center>
<h3>Eletrical & Electronics Engineering Catalogue</h3>
<table width=90% align="center" bgcolor="#E1EEFF">
<tbody bgcolor="#999999">
<tr> <th width=25% >BOOK</th>
<th width="35%">DETAILS</th>
<th width="20%">PRICE</th>
<th width="20%">OPTION</th></tr>
</tbody>
<tr> <tbody align=center>
<td><img src="images/eleceng.jpg" height=120
width=90></img></td>
<td>Book:IntelMicroProcessors<br>Author:Hibert
Schildt<br>
Publicher: Hyberabad Books Corp. <br>
<td align="center">Rs. 2100/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
<tr> <td><img src="images/elec.jpg" height=120
width=90></img></td>
<td>Book: Electrical &amp; Electronics <br> Author:
Rama Krishna. K <br>
Publicher:Tata Mc.Grawhill<br> </td>
<td align="center">Rs. 1250/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
<tr> <td><img src="images/000331427-min-1.jpg" height=120
width=90></img></td>
<td>Book: Solar Power Your Home <br>Author:
Albert<br>
Publicher: Andhra Mc.hill<br> </td>
<td align="center">Rs 9000/-</td>
<td><input type="button" name="Add to cart" value="Add
to cart"></td></tr>
</table>
</center>
</body>
</html>
Output:

20

2. Validate the registration, user login, user profile and payment by credit card
pages using JavaScript.

21

Validation.js
function form1_validation()
{
//Username Validation
if(f1.username.value=="")
{
alert("You must enter a User Name");
f1.username.focus();
return false;
}
if(f1.username.value.length<3)
{
alert("User Name must consist of atleast 3 character");
f1.username.focus();
return false;
}
var checkStr=f1.username.value;
for(i=0;i<checkStr.length;i++)
if(!(checkStr.charCodeAt(i)>=65&&checkStr.charCodeAt(i)<=91)
&& !(checkStr.charCodeAt(i)>=97 && checkStr.charCodeAt(i)<=122))
{
alert("Please enter valid User Name");
f1.username.focus();
return false;
}
//Password Validation
if(f1.password.value.length<6)
{
alert("Please enter Password not less than 6");
f1.password.focus();
return false;
}
if(!(f1.password.value==f1.password1.value))
{
alert("Please re-enter corrert Password");
f1.password1.focus();
return false;
}
//email validation
flag=true;
if(f1.email.value=="")
flag=false;
var Str=f1.email.value;
if(allValidChars(Str))
for(i=0;i<Str.length;i++)

22

if(Str.charAt(i)=="@")
{
if((Str.substr(i+1,9)=="yahoo.com")||(Str.substr(i+1,9)=="gmail.com"))
{
flag=true;
break;
}
}
else
flag=false;
if(!(flag))
{
alert("Please enter a valid Email ID");
f1.email.focus();
return false;
}
//phone number validation
flag=true;
if(f1.phno.value.length!=10)
flag=false;
var phno=f1.phno.value;
for(i=0;i<phno.length;i++)
if(!(phno.charCodeAt(i)>=48&&phno.charCodeAt(i)<=57))
flag=false;
if(!flag)
{
alert("Please enter valid Phone Number");
f1.phno.focus();
return false;
}
//gender validation
flag=false;
for(i=0;i<f1.radio.length;i++)
if(f1.radio[i].checked)
flag=true;
if(!(flag))
{
alert("Please choose a Gender");
return false;
}
//Date of birth validation
if((f1.dd.selectedIndex<=0)||(f1.mm.selectedIndex<=0)||(f1.yyyy.selectedIndex<=0))
{
alert("Please choose a Date of Birth");
f1.phno.focus();
return false;

23

}
//checkbox validation
flag=false;
for (i = 0; i < f1.checkbox.length; i++)
{
if (f1.checkbox[i].checked)
flag = true;
}
if(!(flag))
{
alert("Please select at least one of the \"Language\" options.");
return false;
}
//address validation
if(f1.address.value.length<25)
{
alert("Please enter a Correct Address");
return false;
}
}
function allValidChars(email)
{
var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
for (i=0; i < email.length; i++)
{
var letter = email.charAt(i).toLowerCase();
if (validchars.indexOf(letter) == -1)
return false;
}
return true;
}
Login.html
<html>
<head>
<title> Welcome to AMAZON website</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="validation.js"></script>
</head>
<body >
<center>
<h3> Enter Login Details</h3>
<br/>
<form name="f1" onsubmit="return form1_validation()">
<table align="center" >
<tr> <td> User ID</td>
<td> <input type="text" name="username"></td> </tr>

24

<tr>
<tr>
<tr>
</tr>
</table>
</html>

<td> Password</td>
<td> <input type="password" name="password"></td>
</tr>
<td></td>
<td><input type="submit" value="Submit"></td>
<input type="reset" value="Reset"></td> </tr>
<td colspan=2>Forgot my User ID or Password</td>

Output:

25

26

27

Registration.html
<html>
<head>
<title> Welcome to AMAZON website</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="validation.js"></script> </head>
<body>
<center><h3> Registration Form
</h3> <br/>
<form name="f1" onsubmit="return form1_validation()">
<table cellpadding="1" align="center" >
<tr> <td> Name:* </td>
<td> <input type="text" name="username"></td> </tr>
<tr> <td>Password:*
</td>
<td> <input type="password" name="password"></td> </tr>
<tr> <td>Email ID:*
</td>
<td> <input type="text" name="email"></td>
</tr>
<tr> <td>Phone Number:*</td>
<td> <input type="text" name="phno"></td>
</tr>
<tr> <td valign="top">Gender:*</td>
<td> <input type="radio" name="radio" value="1">Male<br>
<input type="radio" name="radio" value="2">Female</td>
</tr>
<tr> <td>Date of Birth:*</td>
<td> dd: <select name=dd > <option> </option>
<script type="text/javascript">
for(i=1;i<=31;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select>
mm: <select name=mm> <option> </option>
<script type="text/javascript">
for(i=1;i<=12;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select>
yyyy: <select name=yyyy> <option> </option>
<script type="text/javascript">
for(i=1880;i<=2008;i++)
document.write("<option value=i>"+i+"</option>");
</script> </select> </td>
</tr>
<tr> <td valign="top">Language Known:*</td>
<td> <input type="checkbox" name="checkbox" value="English">English<br>
<input type="checkbox" name="checkbox" value="Telugu">Telugu<br>
<input type="checkbox" name="checkbox" value="Hindi">Hindi<br>
<input type="checkbox" name="checkbox" value="Tamil">Tamil</td> </tr>
<tr> <td valign="top">Address:*</td>
<td><textarea name="address"></textarea></td>
<tr><td></td><td><input type="submit" value="submit" hspace="10">
28

<input type="reset" value="reset"></td>


</tr>
<tr> <td colspan=2 >*<font color="#FF0000">fields are
mandatory</font></td></tr>
</table> </center>
</body>
</html>
Output:

29

30

31

32

33

34

35

3.Create and save an XML document at the server, which contains 10 users information.
Write a program which takes User Id as input and returns the user details by taking the
user information from the XML document
student.dtd:
<!ELEMENT student(rollnum,name,parntname,branch,year,addr,email,phno)>
<!ELEMENT roll_no(#PCDATA)>
<!ELEMENT name(first_name,last_name)>
<!ELEMENT first_name(#PCDATA)>
<!ELEMENT last_name(#PCDATA)>
<!ELEMENT father_name(fname,mname,lname)>
<!ELEMENT first_name(#PCDATA)>
<!ELEMENT last_name(#PCDATA)>
<!ELEMENT branch(#PCDATA)>
<!ELEMENT year(#PCDATA)>
<!ELEMENT address(door_no,street,city)>
<!ELEMENT door_no(#PCDATA)>
<!ELEMENT street(#PCDATA)>
<!ELEMENT city(#PCDATA)>
<!ELEMENT email(#PCDATA)>
<!ELEMENT phno(#PCDATA)>
student.xsd:
<xs:schema>
<xs:element name="student">
<xs:complextype>
<xs:sequence>
<xs:element name="roll_no" type="xs:string"/>
<xs:element name="name">
<xs:complextype>
<xs:sequence>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
</xs:sequence>
</xs:complextype>
</xs:element>
<xs:element name="father-name">
<xs:complextype>
<xs:sequence>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
</xs:sequence>
</xs:complextype>
</xs:element>
<xs:element name="branch" type="xs:string"/>

36

<xs:element name="year" type="xs:integer"/>


<xs:element name="addr">
<xs:complextype>
<xs:sequence>
<xs:element name="door-no" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
</xs:complextype>
</xs:element>
<xs:element name="email" type="xs:string"/>
<xs:element name="phno" type="xs:integer"/>
</xs:sequence>
</xs:complextype>
</xs:element>
</xs:schema>
student.xml:
<?xml version="1.0" ?>
- <student xmlns:xsi="xmlschema-instance"
xsi:SchemaLocation="student.xsd">
<roll_no>08A01D2501</roll_no>
- <name>
<first_name>A</first_name>
<Last_name>Aruna</Last_name>
</name>
- <father_name>
<first_name>Venkateswara</first_name>
<Last_name>Rao</Last_name>
</father_name>
<branch>software engineering</branch>
<year>1ST Year</year>
- <address>
<door-no>32-15/1-44</door-no>
<street>B.R.Nagar-1</street>
<city>Hyderabad</city>
</address>
<email>iaaruna@yahoo.com</email>
<phno>9949643372</phno>
<roll_no>08A01D2501</roll_no>
- <name>
<first_name>Prathap</first_name>
<Last_name>Rudra</Last_name>
</name>
- <father_name>
<first_name>Venkateswara </first_name>
<Last_name>B</Last_name>

37

</father_name>
<branch> software engineering (mtech) </branch>
<year>1st Year</year>
<address>
<door-no>door no 603/n</door-no>
<street>Recoloney</street>
<city>Vijayawada</city>
</address>
<email>brprathap@gmail.com</email>
<phno>9866145423</phno>
<roll_no>08A01D2503</roll_no>
<name>
<first_name>Ramesh</first_name>
<Last_name>Kumar</Last_name>
</name>
<father_name>
<first_name>Venkateswara</first_name>
<Last_name>Rao</Last_name>
</father_name>
<branch> software engineering(mtech) </branch>
<year>1st Year</year>
<address>
<door-no>32-15/1-44</door-no>
<street>B.R.Nagar-1</street>
<city>Hyderabad</city>
</address>
<email>itrameshch@gmail.com</email>
<phno>9985150379</phno>
<roll_no>08A01D2504</roll_no>
<name>
<first_name>kishore</first_name>
<Last_name>Galla</Last_name>
</name>
<father_name>
<first_name>Venkateswara</first_name>
<Last_name>Rao</Last_name>
</father_name>
<branch> software engineering (mtech) </branch>
<year>1st Year</year>
<address>
<door-no>32-15/1-44</door-no>
<street>B.R.Nagar-1</street>
<city>hyderabad</city>
</address>
<email>galla_kishore@yahoo.com</email>
<phno>990837394</phno>
<roll_no>08A01D2505</roll_no>
<name>
<first_name>Madhu</first_name>

38

<Last_name>Garlapati</Last_name>
</name>
- <father_name>
<first_name>Venkateswara</first_name>
<Last_name>Rao</Last_name>
</father_name>
<branch> software engineering (mtech) </branch>
<year>1st Year</year>
- <address>
<door-no>32-15/1-44</door-no>
<street>B.R.Nagar-1</street>
<city>Hyderabad</city>
</address>
<email>madhubabu.garlapati@gmail.com</email>
<phno>9908005778</phno>
</student>

4. Bean Assignments

39

a. Create a JavaBeans which gives the exchange value of INR (Indian Rupees) into
equivalent American/Canadian/Australian Dollar value.
b. Create a simple Bean with a label which is the count of number of clicks. Then
create a Bean info class such that only the count properly is visible in the property
Window.
c. Create two Beans a) Keypad b) Display pad. After that integrate the two Beans
to make it work as a calculator.
d. Create two Beans Traffic Light(implemented as a label with only three
background colors-red, green, yellow) and Automobile(Implemented as a Text
Box which states its state/movement). The state of the Automobile should depend
on the following Light Transition table.
Developing a Simple Bean Using the BDK:
This section presents an example that shows how to develop a simple Bean and
connect it to other components via the BDK. Our new component is called the Colors
Bean. It appears as either a rectangle or ellipse that is filled with a color. A color is chosen
at random when the Bean begins execution. A public method can be invoked to change it.
Each time the mouse is clicked on the Bean, another random color is chosen. There is one
boolean read/write property that determines the shape. The BDK is used to lay out an
application with one instance of the Colors Bean and one instance of the OurButton
Bean. The button is labeled Change. Each time it is pressed, the color changes.
DEVELOPMENT
USING JAVA
Create a New Bean
Here are the steps that you must follow to create a new Bean:
1. Create a directory for the new Bean.
2. Create the Java source file(s).
3. Compile the source file(s).
4. Create a manifest file.
5. Generate a JAR file.
6. Start the BDK.
7. Test.
The following sections discuss each of these steps in detail.
Create a Directory for the New Bean You need to make a directory for the Bean.
To follow along with this example, create c:\bdk\demo\sunw\demo\colors. Then change
to that directory. The Colors and OurButton Beans Create the Source File for the New
Bean The source code for the Colors component is shown in the following listing. It is
located in the file Colors.java. The import statement at the beginning of the file places it
in the package named sunw.demo.colors. Recall from Chapter 9 that the directory
hierarchy corresponds to the package hierarchy. Therefore, this file must be located in a
subdirectory named sunw\demo\colors relative to the CLASSPATH environment
variable. The color of the component is determined by the private Color variable color,
and its shape is determined by the private boolean variable rectangular. The constructor
defines an anonymous inner class that extends MouseAdapter and overrides its
mousePressed( ) method. The change( ) method is invoked in response to mouse

40

presses. The component is initialized to a rectangular shape of 200 by 100 pixels. The
change( ) method is invoked to select a random color and repaint the component. The
getRectangular( ) and setRectangular( ) methods provide access to the one property of
this Bean. The change( ) method calls randomColor( ) to choose a color and then calls
repaint( ) to make the change visible. Notice that the paint( ) method uses the
rectangular and color variables to determine how to present the Bean.
// A simple Bean.
package sunw.demo.colors;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas {
transient private Color color;
private boolean rectangular;
public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
change();
}
});
rectangular = false;
setSize(200, 100);
change();
}
public boolean getRectangular() {
return rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change()
{
color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random());
int g = (int)(255*Math.random());
int b = (int)(255*Math.random());
return new Color(r, g, b);
}
public void paint(Graphics g) {
Dimension d = getSize();
int h = d.height;
int w = d.width;

41

g.setColor(color);
if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else {
g.fillOval(0, 0, w-1, h-1);
}
}
}
Compile the Source Code for the New Bean Compile the source code to create a class
file. Type the following:
javac Colors.java.
Create a Manifest File You must now create a manifest file. First, switch to the
c:\bdk\demo directory. This is the directory in which the manifest files for the BDK
demos are located. Put the source code for your manifest file in the file colors.mft.
It is shown here:
Name: sunw/demo/colors/Colors.class
Java-Bean: True
This file indicates that there is one .class file in the JAR file and that it is a Java
Bean Notice that the Colors.class file is in the package sunw.demo.colors and in the
subdirectory sunw\demo\colors relative to the current directory. Generate a JAR File
Beans are included in the ToolBox window of the BDK only if they are in JAR files in
the directory c:\bdk\jars. These files are generated with the jar utility.
Enter the following:
jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class
This command creates the file colors.jar and places it in the directory c:\bdk\jars.
(You may wish to put this in a batch file for future use.)
Start the BDK Change to the directory c:\bdk\beanbox and type run. This causes
the BDK to start. You should see three windows, titled ToolBox, BeanBox, and
Properties. The ToolBox window should include an entry labeled Colors for your new
Bean. Create an Instance of the Colors Bean After you complete the preceding steps,
create an instance of the Colors Bean in the BeanBox window. Test your new component
by pressing the mouse anywhere within its borders. Its color immediately changes. Use
the Properties window to change the rectangular property from false to true. Its shape
immediately changes.

42

Create and Configure an Instance of the OurButton Bean Create an instance of the
OurButton Bean in the BeanBox window. Then follow these steps:
1. Go to the Properties window and change the label of the Bean to Change. You
should see that the button appearance changes immediately when this property is
changed.
2. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed.
3. Move the cursor so that it is inside the Colors Bean display area, and click the left
mouse button. You should see the Event Target Dialog dialog box.
4. The dialog box allows you to choose a method that should be invoked when this button
is clicked. Select the entry labeled change and click the OK button.You should see a
message box appear very briefly, stating that the tool isGenerating and compiling
adaptor class.
5. Click on the button. You should see the color change. You might want to experiment
with the Colors Bean a bit before moving on.
Output:

43

Create two Beans Traffic Light(implemented as a label with only three background
colors-red, green, yellow) and Automobile(Implemented as a Text Box which states
its state/movement). The state of the Automobile should depend on the following
Light Transition table.
/*<applet code="Sig.class" height=300 width=200></applet>*/
import java.awt.*;
import java.lang.String;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.*;
public class Sig extends Applet implements ItemListener{
boolean c1,c2,c3;
String s1;
Checkbox r1,r2,r3;
CheckboxGroup cbg;
public void init(){
cbg=new CheckboxGroup();
Panel p=new Panel();
p.setLayout(new GridLayout());
add(r1=new Checkbox("red",cbg,false));
add(r2=new Checkbox("yellow",cbg,false));
add(r3=new Checkbox("green",cbg,false));
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawOval(10, 30, 20, 20);
//System.out.println(s1);
g.setColor(Color.yellow);
g.drawOval(10, 60, 20, 20);
g.setColor(Color.green);
g.drawOval(10, 90, 20, 20);
s1=cbg.getSelectedCheckbox().getLabel();
if(s1=="red")
{
g.setColor(Color.red);
g.fillOval(10, 30, 20, 20);
}
else if(s1=="yellow")
{
g.setColor(Color.yellow);
g.fillOval(10, 60, 20, 20);

44

}
else if(s1=="green")
{
g.setColor(Color.green);
g.fillOval(10, 90, 20, 20);
}
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}

45

5. SERVELETS & COOKIES


Reading Servlet Parameters:
The ServletRequest class includes methods that allow you to read the names and
values of parameters that are included in a client request. We will develop a servlet that
illustrates their use. The example contains two files: PostParameters.htm defines a Web
page, and PostParametersServlet.java defines a servlet. The HTML source code for
PostParameters.htm is shown in the following listing. It defines a table that contains
two labels and two text fields. One of the labels is Employee and the other is Phone. The
form also includes a submit button. Notice that the action parameter of the form tag
specifies a URL. The URL identifies the servlet to process the HTTP POST request.
<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlet/PostParametersServlet">
<table>
<tr>
<td><B>Employee</td>
<td><input type=textbox name="e" size="25" value=""></td>
</tr>
<tr>
<td><B>Phone</td>
<td><input type=textbox name="p" size="25" value=""></td>
</tr>
</table>
<input type=submit value="Submit">
</body>
</html>
The source code for PostParametersServlet.java is shown in the following listing. The
service( ) method is overridden to process client requests. The getParameterNames( )
method returns an enumeration of the parameter names. These are processed in a loop.
You can see that the parameter name and value are output to the client. The parameter
value is obtained via the getParameter( ) method.
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParametersServlet
extends GenericServlet {
public void service(ServletRequest request,

46

ServletResponse response)
throws ServletException, IOException {
// Get print writer
PrintWriter pw = response.getWriter();
// Get enumeration of parameter names
Enumeration e = request.getParameterNames();
// Display parameter names and values
while(e.hasMoreElements()) {
String pname = (String)e.nextElement();
pw.print(pname + " = ");
String pvalue = request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}
}
The example contains two files: ColorGet.htm defines a Web page, and
ColorGetServlet.java defines a servlet. The HTML source code for ColorGet.htm is
shown in the following listing. It defines a form that contains a select element and a
submit button. Notice that the action parameter of the form tag specifies a URL. The
URL identifies a servlet to process the HTTP GET request.
<body>
<center>
<form name="Form1"
action="http://localhost:8080/servlet/ColorGetServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<br><br>
<input type=submit value="Submit">
</form>
</body>
</html>
method is overridden to process any HTTP GET requests that are sent to this servlet. It
uses the getParameter( ) method of HttpServletRequest to obtain the selection that was
made by the user. A response is then formulated.

47

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorGetServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>The selected color is: ");
pw.println(color);
pw.close();
The HTML source code for ColorPost.htm is shown in the following listing. It is
identical to ColorGet.htm except that the method parameter for the form tag explicitly
specifies that the POST method should be used, and the action parameter for the form tag
specifies a different servlet.
<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlet/ColorPostServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<br><br>
<input type=submit value="Submit">
</form>
</body>
</html>

The source code for ColorPostServlet.java is shown in the following listing. The
doPost( ) method is overridden to process any HTTP POST requests that are sent to this
servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the
selection that was made by the user. A response is then formulated.

48

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorPostServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>The selected color is: ");
pw.println(color);
pw.close();
}
}

49

You might also like