WT Expts
WT Expts
1 HTML
2 VB Script
3 Java Script
1
Ex.No.01 WEB PAGE DESIGN USING HTML
AIM
To design web page using HTML tags.
HTML TAGS
1) <html> The text between <html> and </html> describes the web page
2) <body> The text between <body> and </body> is the visible page content
3) HTML Headings
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
4) HTML Paragraphs
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
3
5) HTML Links
Example
6) HTML Images
Example
HTML Elements
An HTML element is everything from the start tag to the end tag:
• The start tag is often called the opening tag. The end tag is often called the closing tag.
7) HTML Elements
Most HTML elements can be nested (can contain other HTML elements).
4
<html>
<body>
<p>This is my first paragraph.</p>
</body>
</html>
8) HTML Attributes
Attributes provide additional information about HTML elements.
HTML Attributes
Attribute Example
HTML links are defined with the <a> tag. The link address is specified in the href attribute:
Example
Double style quotes are the most common, but single style quotes are also allowed.
Below is a list of some attributes that are standard for most HTML elements:
9) HTML Lines
5
Example
<p>This is a paragraph</p>
<hr />
<p>This is a paragraph</p>
<hr />
<p>This is a paragraph</p>
Comments can be inserted into the HTML code to make it more readable and understandable.
Comments are ignored by the browser and are not displayed.
Example
Use the <br /> tag if you want a line break (a new line) without starting a new paragraph:
Example
Note: The <br /> element is an empty HTML element. It has no end tag.
In XHTML, XML, and future versions of HTML, HTML elements with no end tag (closing tag) are not
allowed.
HTML uses tags like <b> and <i> for formatting output, like bold or italic text.
Tag Description
<b> Defines bold text
<big> Defines big text
<em> Defines emphasized text
6
<i> Defines italic text
<small> Defines small text
<strong> Defines strong text
<sub> Defines subscripted text
<sup> Defines superscripted text
<ins> Defines inserted text
<del> Defines deleted text
Styles was introduced with HTML 4, as the new and preferred way to style HTML elements. With
HTML styles, styles can be added to HTML elements directly by using the style attribute, or indirectly
in separate style sheets (CSS files).
Tags Description
<center> Defines centered content
<font> and <basefont> Defines HTML fonts
<s> and <strike> Defines strikethrough text
<u> Defines underlined text
Attributes Description
align Defines the alignment of text
bgcolor Defines the background color
color Defines the text color
7
HTML Style Example - Background Color
Example
<html>
<body style="background-color:yellow">
<h2 style="background-color:red">This is a heading</h2>
<p style="background-color:green">This is a paragraph.</p>
</body>
</html>
The font-family, color, and font-size properties defines the font, color, and size of the text in an
element:
Example
<html>
<body>
<h1 style="font-family:verdana">A heading</h1>
<p style="font-family:arial;color:red;font-size:20px;">A paragraph.</p>
</body>
</html>
Example
<html>
<body>
<h1 style="text-align:center">This is a heading</h1>
8
<p>The heading above is aligned to the center of this page.</p>
</body>
</html>
Links are found in nearly all Web pages. Links allow users to click their way from page to page. HTML
Hyperlinks (Links)
A hyperlink (or link) is a word, group of words, or image that you can click on to jump to a new
document or a new section within the current document.
When you move the cursor over a link in a Web page, the arrow will turn into a little hand.
Example
The example below will open the linked document in a new browser window:
Example
9
15) HTML The <img> Tag and the Src Attribute
In HTML, images are defined with the <img> tag. The <img> tag is empty, which means that it
contains attributes only, and has no closing tag.
To display an image on a page, you need to use the src attribute. Src stands for "source". The value of
the src attribute is the URL of the image you want to display.
The browser displays the image where the <img> tag occurs in the document. If you put an image tag
between two paragraphs, the browser shows the first paragraph, then the image, and then the second
paragraph.
The required alt attribute specifies an alternate text for an image, if the image cannot be displayed.
The alt attribute provides alternative information for an image if a user for some reason cannot view it
(because of slow connection, an error in the src attribute, or if the user uses a screen reader).
A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td>
tag). td stands for "table data," and holds the content of a data cell. A <td> tag can contain text, links,
images, lists, forms, other tables, etc.
Table Example
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
10
How the HTML code above looks in a browser:
If you do not specify a border attribute, the table will be displayed without borders. Sometimes this can
be useful, but most of the time, we want the borders to show.
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
11
HTML Table Tags
Tag Description
<table> Defines a table
<th> Defines a table header
<tr> Defines a table row
<td> Defines a table cell
<caption> Defines a table caption
<colgroup> Defines a group of columns in a table, for formatting
<col> Defines attribute values for one or more columns in a table
<thead> Groups the header content in a table
<tbody> Groups the body content in a table
<tfoot> Groups the footer content in a table
HTML Lists
An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.
The list items are marked with bullets (typically small black circles).
<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>
• Coffee
• Milk
12
HTML Ordered Lists
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>
1. Coffee
2. Milk HTML Definition Lists
The <dl> tag is used in conjunction with <dt> (defines the item in the list) and <dd> (describes the item
in the list):
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Coffee
- black hot drink
Milk
- white cold drink
Tag Description
<ol> Defines an ordered list
<ul> Defines an unordered list
<li> Defines a list item
<dl> Defines a definition list
<dt> Defines an item in a definition list
<dd> Defines a description of an item in a definition
13
19) HTML Forms and Input
HTML Forms are used to select different kinds of user input.
HTML Forms
A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more.
A form can also contain select lists, textarea, fieldset, legend, and label elements.
<form>
.
input elements
.
</form>
An input element can vary in many ways, depending on the type attribute. An input element can be of
type text field, checkbox, password, radio button, submit button, and more.
Text Fields
<input type="text" /> defines a one-line input field that a user can enter text into:
<form>
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
</form>
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.
14
Password Field
<form>
Password: <input type="password" name="pwd" />
</form>
Password:
Note: The characters in a password field are masked (shown as asterisks or circles).
Radio Buttons
<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE one of a
limited number of choices:
<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>
Male
Female
Checkboxes
<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE options of
a limited number of choices.
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>
I have a bike
I have a car
15
Submit Button
A submit button is used to send form data to a server. The data is sent to the page specified in the form's
action attribute. The file defined in the action attribute usually does something with the received input:
Submit
Username:
If you type some characters in the text field above, and click the "Submit" button, the browser will send
your input to a page called "html_form_action.asp". The page will show you the received input.
Tag Description
<form> Defines an HTML form for user input
<input> Defines an input control
<textarea> Defines a multi-line text input control
<label> Defines a label for an input element
<fieldset> Defines a border around elements in a form
<legend> Defines a caption for a fieldset element
<select> Defines a select list (drop-down list)
<optgroup> Defines a group of related options in a select list
<option> Defines an option in a select list
<button> Defines a push button
With frames, you can display more than one HTML document in the same browser window. Each
HTML document is called a frame, and each frame is independent of the others.
16
The Frameset Tag
• The <frameset> tag defines how to divide the window into frames
• Each frameset defines a set of rows or columns
• The values of the rows/columns indicate the amount of screen area each row/column will
occupy
• The <frame> tag defines what HTML document to put into each frame
In the example below we have a frameset with two columns. The first column is set to 25% of the width
of the browser window. The second column is set to 75% of the width of the browser window. The
HTML document "frame_a.htm" is put into the first column, and the HTML document "frame_b.htm"
is put into the second column:
<frameset cols="25%,75%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
</frameset>
Frame Tags
Tag Description
<frameset> Defines a set of frames
<frame> Defines a sub window (a frame)
<noframes> Defines a noframe section for browsers that do not handle frames
<iframe> Defines an inline sub window (frame
Even if a lot of people are using it, you should try to avoid it, and use styles instead.
With HTML code like this, you can specify both the size and the type of the browser output :
Example
<p>
<font size="2" face="Verdana">
This is a paragraph.
</font>
</p>
17
<p>
<font size="3" face="Times">
This is another paragraph.
</font>
</p>
Font Attributes
When a browser reads a style sheet, it will format the document according to it. There are three ways of
inserting a style sheet:
An external style sheet is ideal when the style is applied to many pages. With an external style sheet,
you can change the look of an entire Web site by changing one file. Each page must link to the style
sheet using the <link> tag. The <link> tag goes inside the head section.
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
An internal style sheet should be used when a single document has a unique style. You define internal
styles in the head section with the <style> tag.
<head>
<style type="text/css">
18
body {background-color: red}
p {margin-left: 20px}
</style>
</head>
Inline Styles
An inline style should be used when a unique style is to be applied to a single occurrence of an element.
To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any
CSS property. The example shows how to change the color and the left margin of a paragraph:
Style Tags
Tag Description
<style> Defines a style definition
<link> Defines a resource reference
<div> Defines a section in a document
<span> Defines a section in a document
<font> Deprecated. Use styles instead
<basefont> Deprecated. Use styles instead
<center> Deprecated. Use styles instead
19
Attribute Value Description
onchange script Script to be run when the element changes
onsubmit script Script to be run when the form is submitted
onreset script Script to be run when the form is reset
onselect script Script to be run when the element is selected
onblur script Script to be run when the element loses focus
onfocus script Script to be run when the element gets focus
Keyboard Events
Not valid in base, bdo, br, frame, frameset, head, html, iframe, meta, param, script, style, and title
elements.
Mouse Events
Not valid in base, bdo, br, frame, frameset, head, html, iframe, meta, param, script, style, title elements.
Result
Thus the HTML concepts have studied.
20
Ex.No.02 WEB PROGRAMMING USING VBSCRIPT
Aim
To program using VB Script
When a VBScript is inserted into an HTML document, the Internet browser will read the HTML and
interpret the VBScript. The VBScript can be executed immediately, or at a later event.
The HTML <script> tag is used to insert a VBScript into an HTML page.
The example below shows how to use VBSript to write text on a web page:
<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>
The document.write command is a standard VBScript command for writing output to a page.
By entering the document.write command between the <script> and </script> tags, the browser will
recognize it as a VBScript command and execute the code line. In this case the browser will write Hello
World! to the page:
Browsers that do not support scripting, will display VBScript as page content.
To prevent them from doing this, the HTML comment tag should be used to "hide" the VBScript.
21
Just add an HTML comment tag <!-- before the first VBScript statement, and a --> (end of comment)
after the last VBScript statement, like this:
<html>
<body>
<script type="text/vbscript">
<!--
document.write("Hello World!")
-->
</script>
</body>
</html>
VBScripts in a page will be executed immediately while the page loads into the browser. This is not
always what we want. Sometimes we want to execute a script when a page loads, or at a later event,
such as when a user clicks a button. When this is the case we put the script inside a function or a sub
procedure, you will learn about procedures in a later chapter.
Scripts in <head>
Put your functions and sub procedures in the head section, this way they are all in one place, and they
do not interfere with page content.
<html>
<head>
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>
<body onload="myFunction()">
</body>
</html>
Scripts in <body>
If you don't want your script to be placed inside a function, and especially if your script should write
page content, it should be placed in the body section.
22
<html>
<head>
</head>
<body>
<script type="text/vbscript">
document.write("This message is written by VBScript")
</script>
</body>
</html>
You can place an unlimited number of scripts in your document, and you can have scripts in both the
body and the head section.
<html>
<head>
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>
<body>
<button onclick="myFunction()">Click me</button>
<script type="text/vbscript">
document.write("This message is written by VBScript")
</script>
</body>
</html>
If you want to run the same VBScript on several pages, without having to write the same script on every
page, you can write a VBScript in an external file.
To use the external script, point to the .vbs file in the "src" attribute of the <script> tag:
Example
23
<html>
<head>
<script type="text/vbscript" src="ex.vbs"></script>
</head>
<body>
</body>
</html>
VBScript Variables
A variable can have a short name, like x, or a more descriptive name, like carname.
In VBScript, all variables are of type variant, that can store different types of data.
You can declare VBScript variables with the Dim, Public or the Private statement. Like this:
Dim x
Dim carname
Assigning Values to Variables
carname="Volvo"
x=10
Lifetime of Variables
When you declare a variable within a procedure, the variable can only be accessed within that
procedure. When the procedure exits, the variable is destroyed. These variables are called local
variables. You can have local variables with the same name in different procedures, because each is
recognized only by the procedure in which it is declared.
24
If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime
of these variables starts when they are declared, and ends when the page is closed
VBScript Array Variables
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a
fixed-size array. You assign data to each of the elements of the array like this:
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
Similarly, the data can be retrieved from any element using the index of the particular array element
you want. Like this:
mother=names(0)
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the
numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows
and 7 columns:
Dim table(4,6)
<html>
<body>
<script type="text/vbscript">
Dim x(2,2)
x(0,0)="Volvo"
x(0,1)="BMW"
x(0,2)="Ford"
x(1,0)="Apple"
x(1,1)="Orange"
x(1,2)="Banana"
x(2,0)="Coke"
x(2,1)="Pepsi"
x(2,2)="Sprite"
for i=0 to 2
document.write("<p>")
for j=0 to 2
document.write(x(i,j) & "<br />")
25
next
document.write("</p>")
next
</script>
</body>
</html>
• Sub procedure
• Function procedure
A Sub procedure:
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
Sub mysub()
alert("Hello World")
End Sub
A Function procedure:
26
• returns a value by assigning a value to its name
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
function myfunction()
myfunction=Date()
end function
There are different ways to call a procedure. You can call it from within another procedure, on an event,
or call it within a script.
<body>
<button onclick="myfunction()">Click me</button>
</body>
carname=findname()
Here you call a Function called "findname", the Function returns a value that will be stored in the
variable "carname".
27
Example (IE Only)
Function myfunction(a,b)
myfunction=a+b
End Function
document.write(myfunction(5,9))
The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14.
When you call a procedure you can use the Call statement, like this:
Call MyProc(argument)
MyProc argument
Conditional Statements
Conditional statements are used to perform different actions for different decisions.
If...Then...Else
If you want to execute only one statement when a condition is true, you can write the code on one line:
There is no ..Else.. in this syntax. You just tell the code to perform one action if a condition is true (in
this case If i=10).
If you want to execute more than one statement when a condition is true, you must put each statement
on separate lines, and end the statement with the keyword "End If":
28
If i=10 Then
alert("Hello")
i = i+1
End If
There is no ..Else.. in the example above either. You just tell the code to perform multiple actions if the
condition is true.
If you want to execute a statement if a condition is true and execute another statement if the condition is
not true, you must add the "Else" keyword:
<html>
<body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html>
In the example above, the first block of code will be executed if the condition is true, and the other
block will be executed otherwise (if i is greater than 10).
If...Then...ElseIf
You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:
<html>
<body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
29
If i = 10 Then
document.write("Just started...!")
ElseIf i = 11 then
document.write("Hungry!")
ElseIf i = 12 then
document.write("Ah, lunch-time!")
ElseIf i = 16 then
document.write("Time to go home!")
Else
document.write("Unknown")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html>
Select Case
You can also use the "Select Case" statement if you want to select one of many blocks of code to
execute:
<html>
<body>
<script type="text/vbscript">
d=weekday(date)
Select Case d
Case 1
document.write("Sleepy Sunday")
Case 2
document.write("Monday again!")
Case 3
document.write("Just Tuesday!")
Case 4
document.write("Wednesday!")
Case 5
document.write("Thursday...")
Case 6
document.write("Finally Friday!")
Case else
document.write("Super Saturday!!!!")
30
End Select
</script>
</body>
</html>
This is how it works: First we have a single expression (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each Case in the structure. If there is a
match, the block of code associated with that Case is executed
Looping Statements
Looping statements are used to run the same block of code a specified number of times.
For...Next Loop
Use the For...Next statement to run a block of code a specified number of times.
The For statement specifies the counter variable (i), and its start and end values. The Next statement
increases the counter variable (i) by one.
Example
<html>
<body>
<script type="text/vbscript">
For i = 0 To 5
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
31
The Step Keyword
With the Step keyword, you can increase or decrease the counter variable by the value you specify.
In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats.
To decrease the counter variable, you must use a negative Step value. You must specify an end value
that is less than the start value.
In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats.
Exit a For...Next
You can exit a For...Next statement with the Exit For keyword.
For i=1 To 10
If i=5 Then Exit For
some code
Next
A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an
array.
Example
<html>
<body>
<script type="text/vbscript">
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
32
</script>
</body>
</html>
Do...Loop
If you don't know how many repetitions you want, use a Do...Loop statement.
The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes
true.
Do While i>10
some code
Loop
If i equals 9, the code inside the loop above will never be executed.
Do
some code
Loop While i>10
The code inside this loop will be executed at least one time, even if i is less than 10.
Do Until i=10
some code
Loop
If i equals 10, the code inside the loop will never be executed.
Do
some code
Loop Until i=10
The code inside this loop will be executed at least one time, even if i is equal to 10.
33
Exit a Do...Loop
Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop
The code inside this loop will be executed as long as i is different from 10, and as long as i is greater
than 10.
Result
Thus the VB Scripts concepts have been studied
34
Ex.No.03 WEB PROGRAMMING USING JAVASCRIPT
AIM
WHAT IS JAVASCRIPT?
• JavaScript gives HTML designers a programming tool - HTML authors are normally not
programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone
can put small "snippets" of code into their HTML pages
• JavaScript can put dynamic text into an HTML page - A JavaScript statement like this:
document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page
• JavaScript can react to events - A JavaScript can be set to execute when something happens,
like when a page has finished loading or when a user clicks on an HTML element
• JavaScript can read and write HTML elements - A JavaScript can read and change the
content of an HTML element
• JavaScript can be used to validate data - A JavaScript can be used to validate form data
before it is submitted to a server. This saves the server from extra processing
• JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect
the visitor's browser, and - depending on the browser - load another page specifically designed
for that browser
• JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve
information on the visitor's computer
35
Put a JavaScript into an HTML page
The example below shows how to use JavaScript to write text on a web page:
Example
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
The example below shows how to add HTML tags to the JavaScript:
Example
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
The document.write command is a standard JavaScript command for writing output to a page.
By entering the document.write command between the <script> and </script> tags, the browser will
recognize it as a JavaScript command and execute the code line. In this case the browser will write
Hello World! to the page:
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
JavaScripts in a page will be executed immediately while the page loads into the browser. This is not
always what we want. Sometimes we want to execute a script when a page loads, or at a later event,
such as when a user clicks a button. When this is the case we put the script inside a function, you will
learn about functions in a later chapter.
Scripts in <head>
36
Scripts to be executed when they are called, or when an event is triggered, are placed in functions.
Put your functions in the head section, this way they are all in one place, and they do not interfere with
page content.
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body>
</html>
Try it yourself »
Scripts in <body>
If you don't want your script to be placed inside a function, or if your script should write page content, it
should be placed in the body section.
Example
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
Try it yourself »
You can place an unlimited number of scripts in your document, so you can have scripts in both the
body and the head section.
37
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
Try it yourself »
Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when you write
JavaScript statements, create or call variables, objects and functions.
JavaScript Statements
A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser
what to do.
This JavaScript statement tells the browser to write "Hello Dolly" to the web page:
document.write("Hello Dolly");
It is normal to add a semicolon at the end of each executable statement. Most people think this is a good
programming practice, and most often you will see this in JavaScript examples on the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to
interpret the end of the line as the end of the statement. Because of this you will often see examples
without the semicolon at the end.
Note: Using semicolons makes it possible to write multiple statements on one line.
JavaScript Blocks
38
Blocks start with a left curly bracket {, and ends with a right curly bracket }.
This example will write a heading and two paragraphs to a web page:
Example
<script type="text/javascript">
{
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
}
</script>
The example above is not very useful. It just demonstrates the use of a block. Normally a block is used
to group statements together in a function or in a condition (where a group of statements should be
executed if a condition is met).
You will learn more about functions and conditions in later chapters.
JavaScript Comments
Comments can be added to explain the JavaScript, or to make the code more readable.
The following example uses single line comments to explain the code:
Example
<script type="text/javascript">
// Write a heading
document.write("<h1>This is a heading</h1>");
// Write two paragraphs:
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
Try it yourself »
39
JavaScript Variables
A variable can have a short name, like x, or a more descriptive name, like carname.
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character
var x;
var carname;
After the declaration shown above, the variables are empty (they have no values yet).
However, you can also assign values to the variables when you declare them:
var x=5;
var carname="Volvo";
After the execution of the statements above, the variable x will hold the value 5, and carname will hold
the value Volvo.
Note: When you assign a text value to a variable, use quotes around the value.
If you assign values to variables that have not yet been declared, the variables will automatically be
declared.
These statements:
x=5;
carname="Volvo";
var x=5;
40
var carname="Volvo";
If you redeclare a JavaScript variable, it will not lose its original value.
var x=5;
var x;
After the execution of the statements above, the variable x will still have the value of 5. The value of x
is not reset (or cleared) when you redeclare it.
JavaScript Arithmetic
y=x-5;
z=y+5;
You will learn more about the operators that can be used in the next chapter of this tutorial.
y=5;
z=2;
x=y+z;
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can
use conditional statements in your code to do this.
• if statement - use this statement to execute some code only if a specified condition is true
• if...else statement - use this statement to execute some code if the condition is true and another
code if the condition is false
41
• if...else if....else statement - use this statement to select one of many blocks of code to be
executed
• switch statement - use this statement to select one of many blocks of code to be executed
If Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true
}
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
Example
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the
specified condition is true.
If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if the condition
is not true.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
42
code to be executed if condition is not true
}
Example
<script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
Use the if....else if...else statement to select one of several blocks of code to be executed.
Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
Example
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
43
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Hello World!</b>");
}
</script>
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the structure. If there is a
match, the block of code associated with that case is executed. Use break to prevent the code from
running into the next case automatically.
Example
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
44
case 5:
document.write("Finally Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
</script>
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
alert("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>
</body>
</html>
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
45
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
confirm("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
</body>
</html>
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after
entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
prompt("sometext","defaultvalue");
46
Example
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
</body>
</html>
JavaScript Functions
To keep the browser from executing a script when the page loads, you can put your script into a
function.
A function contains code that will be executed by an event or by a call to the function.
You may call a function from anywhere within a page (or even from other pages if the function is
embedded in an external .js file).
Functions can be defined both in the <head> and in the <body> section of a document. However, to
assure that a function is read/loaded by the browser before it is called, it could be wise to put functions
in the <head> section.
Syntax
function functionname(var1,var2,...,varX)
{
some code
}
The parameters var1, var2, etc. are variables or values passed into the function. The { and the } defines
the start and end of the function.
47
Note: A function with no parameters must include the parentheses () after the function name.
Note: Do not forget about the importance of capitals in JavaScript! The word function must be written
in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with
the exact same capitals as in the function name.
Example
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
If the line: alert("Hello world!!") in the example above had not been put within a function, it would
have been executed as soon as the line was loaded. Now, the script is not executed before a user hits the
input button. The function displaymessage() will be executed if the input button is clicked.
The return statement is used to specify the value that is returned from the function.
So, functions that are going to return a value must use the return statement.
The example below returns the product of two numbers (a and b):
48
Example
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
If you declare a variable within a function, the variable can only be accessed within that function. When
you exit the function, the variable is destroyed. These variables are called local variables. You can have
local variables with the same name in different functions, because each is recognized only by the
function in which it is declared.
If you declare a variable outside a function, all the functions on your page can access it. The lifetime of
these variables starts when they are declared, and ends when the page is closed.
JavaScript Loops
Often when you write code, you want the same block of code to run over and over again in a row.
Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
The for loop is used when you know in advance how many times the script should run.
49
Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs.
Note: The increment parameter could also be negative, and the <= could be any comparing statement.
Example
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The while loop loops through a block of code while a specified condition is true.
Syntax
while (var<=endvalue)
{
code to be executed
}
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs:
Example
50
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
{
document.write("The number is " + i);
document.write("<br />");
i++;
}
</script>
</body>
</html>
The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE, and
then it will repeat the loop as long as the specified condition is true.
Syntax
do
{
code to be executed
}
while (var<=endvalue);
Example
The example below uses a do...while loop. The do...while loop will always be executed at least once,
even if the condition is false, because the statements are executed before the condition is tested:
Example
<html>
<body>
<script type="text/javascript">
var i=0;
do
{
document.write("The number is " + i);
document.write("<br />");
i++;
}
while (i<=5);
</script>
51
</body>
</html>
The break statement will break the loop and continue executing the code that follows after the loop (if
any).
Example
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The continue statement will break the current loop and continue with the next value.
Example
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
52
}
</script>
</body>
</html>
The for...in statement loops through the elements of an array or through the properties of an object.
Syntax
for (variable in object)
{
code to be executed
}
Note: The code in the body of the for...in loop is executed once for each element/property.
Note: The variable argument can be a named variable, an array element, or a property of an object.
Example
Example
<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>
Events
By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be
detected by JavaScript.
53
Every element on a web page has certain events which can trigger a JavaScript. For example, we can
use the onClick event of a button element to indicate that a function will run when a user clicks on the
button. We define the events in the HTML tags.
Examples of events:
• A mouse click
• A web page or an image loading
• Mousing over a hot spot on the web page
• Selecting an input field in an HTML form
• Submitting an HTML form
• A keystroke
Note: Events are normally used in combination with functions, and the function will not be executed
before the event occurs!
For a complete reference of the events recognized by JavaScript, go to our complete JavaScript
reference.
The onLoad and onUnload events are triggered when the user enters or leaves the page.
The onLoad event is often used to check the visitor's browser type and browser version, and load the
proper version of the web page based on the information.
Both the onLoad and onUnload events are also often used to deal with cookies that should be set when
a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his
first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page,
you could have another popup saying something like: "Welcome John Doe!".
The onFocus, onBlur and onChange events are often used in combination with validation of form fields.
Below is an example of how to use the onChange event. The checkEmail() function will be called
whenever the user changes the content of the field:
onSubmit
The onSubmit event is used to validate ALL form fields before submitting it.
Below is an example of how to use the onSubmit event. The checkForm() function will be called when
the user clicks the submit button in the form. If the field values are not accepted, the submit should be
cancelled. The function checkForm() returns either true or false. If it returns true the form will be
submitted, otherwise the submit will be cancelled:
54
<form method="post" action="xxx.htm" onsubmit="return checkForm()">
Below is an example of an onMouseOver event. An alert box appears when an onMouseOver event is
detected:
The try...catch statement allows you to test a block of code for errors. The try block contains the code to
be run, and the catch block contains the code to be executed if an error occurs.
Syntax
try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}
Note that try...catch is written in lowercase letters. Using uppercase letters will generate a JavaScript
error!
Examples
The example below is supposed to alert "Welcome guest!" when the button is clicked. However, there's
a typo in the message() function. alert() is misspelled as adddlert(). A JavaScript error occurs. The catch
block catches the error and executes a custom code to handle it. The code displays a custom error
message informing the user what happened:
Example
<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
55
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.description + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>
The next example uses a confirm box to display a custom message telling users they can click OK to
continue viewing the page or click Cancel to go to the homepage. If the confirm method returns false,
the user clicked Cancel, and the code redirects the user. If the confirm method returns true, the code
does nothing:
Example
<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Click OK to continue viewing this page,\n";
txt+="or Cancel to return to the home page.\n\n";
if(!confirm(txt))
{
document.location.href="http://www.w3schools.com/";
}
}
}
</script>
56
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>
The throw statement allows you to create an exception. If you use this statement together with the
try...catch statement, you can control program flow and generate accurate error messages.
Syntax
throw(exception)
Note that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
Example
The example below determines the value of a variable called x. If the value of x is higher than 10, lower
than 0, or not a number, we are going to throw an error. The error is then caught by the catch argument
and the proper error message is displayed:
Example
<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 0 and 10:","");
try
{
if(x>10)
{
throw "Err1";
}
else if(x<0)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
}
catch(er)
57
{
if(er=="Err1")
{
alert("Error! The value is too high");
}
if(er=="Err2")
{
alert("Error! The value is too low");
}
if(er=="Err3")
{
alert("Error! The value is not a number");
}
}
</script>
</body>
</html>
Result
58
Ex.No.04a XML with HTML
Aim
To Create a XML program for book store and use DTD
Procedure
i) Create a xml document to contain the data about the book catalogue in the following way
i<books>
<book>
<author>
<title>
<price>
<ISBN_No>
<Publisher>
<Country>
ii) Create a html document
iii) Open a html tag
iv) In html document Use the src attribute to refer the xml document
v) Create the table with the following fields
a) Book Title
b) Author
c) Price
d) ISBN_No
e) Publishers
f) Country
vi) Use the div and datafld field to fetch the data from the Xml document
vii) Close the table
viii) Close the html tag
Program
Html Program
<html>
<head>
<title> XML Data</title>
</head>
<body bgcolor="#ffccff">
<h1> <center> Book Catalogue table </h1>
59
<th colspan="5"> Book Catlalogue of Knowledge Garden </th>
</tr>
</Tfoot>
<tr>
<td> <div datafld="title"> </div> </td>
<td> <div datafld="author"> </div> </td>
<td> <div datafld="price"> </div> </td>
<td> <div datafld="ISBN_No"> </div> </td>
<td> <div datafld="Publishers"> </div> </td>
<td> <div datafld="Country"> </div> </td>
</tr>
</Table>
</body>
</html>
XML Program
<?xml version="1.0"?>
<books>
<book>
<title> Asp.net </title>
<author> Wilson </author>
<price> Rs.300 </price>
<ISBN_No> 12345 </ISBN_No>
<Publishers> TMH </Publishers>
<Country> USA </Country>
</book>
<book>
<title> Web Technology </title>
<author> R.Premananth </author>
<price> Rs.500 </price>
<ISBN_No> 12321 </ISBN_No>
<Publishers> RBP</Publishers>
<Country> India </Country>
</book>
<book>
<title> Data Structures </title>
<author> M.S.weiss </author>
<price> Rs.400 </price>
<ISBN_No> 12321 </ISBN_No>
<Publishers> RBP </Publishers>
<Country> USA </Country>
</book>
<book>
<title> C# Programming </title>
<author> E.Balagurusamy </author>
<price> Rs.400 </price>
<ISBN_No> 12320 </ISBN_No>
60
<Publishers> PHI </Publishers>
<Country> India </Country>
</book>
</books>
Output
Result
Thus the program for Book Catalogue Table has been created with HTML &XML
61
Ex.No.04b XML with XSLT & DTD
Aim
To Create a XML program for book catalogue using XSLT & DTD
Procedure
Program
XML file
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="bo.xsl"?>
<!DOCTYPE books SYSTEM "book.dtd">
<books>
<book>
<title> Asp.net </title>
<author> Wilson </author>
<price> Rs.300 </price>
<ISBN_No> 12345 </ISBN_No>
<Publishers> &TMH; </Publishers>
<Country> USA </Country>
</book>
<book>
<title> Web Technology </title>
<author> R.Premananth </author>
<price> Rs.500 </price>
<ISBN_No> 12321 </ISBN_No>
<Publishers> &RBP;</Publishers>
<Country> India </Country>
</book>
<book>
<title> Data Structures </title>
<author> M.S.weiss </author>
<price> Rs.400 </price>
<ISBN_No> 12321 </ISBN_No>
<Publishers> &RBP; </Publishers>
62
<Country> USA </Country>
</book>
<book>
<title> C# Programming </title>
<author> E.Balagurusamy </author>
<price> Rs.400 </price>
<ISBN_No> 12320 </ISBN_No>
<Publishers> &PHI; </Publishers>
<Country> India </Country>
</book>
</books>
XSLT File
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title> XML Data</title>
</head>
<body bgcolor="#ffccff">
<h1> <center> Book Catalogue table </center> </h1>
<center>
<Table border="1" bgcolor="#cccccc">
<THead>
<tr>
<th> Book Title </th>
<th> Author </th>
<th> Price </th>
<th> ISBN_No </th>
<th> Publishers </th>
<th> Country </th>
</tr>
</THead>
<Tfoot>
<tr>
<th colspan="5"> Book Catlalogue of Knowledge Garden </th>
</tr>
</Tfoot>
<xsl:for-each select="books/book">
<xsl:sort select="title" />
<tr>
<td> <xsl:value-of select="title" /> </td>
<td> <xsl:value-of select="author" /> </td>
<td> <xsl:value-of select="price" /> </td>
63
<td> <xsl:value-of select="ISBN_No" /></td>
<td> <xsl:value-of select="Publishers" /> </td>
<td> <xsl:value-of select="Country" /> </td>
</tr>
</xsl:for-each>
</Table>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
External DTD
<!--Elements-->
<!ELEMENT books (book+)>
<!--Element-->
<!ELEMENT book (title,author,price,ISBN_No,Publishers,Country)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT ISBN_No (#PCDATA)>
<!ELEMENT Publishers (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!--Entities-->
<!ENTITY PHI "Prentice-Hall of India">
<!ENTITY RBP "RB Publications">
<!ENTITY TMH "TATA MC-GraW Hill">
64
Output
Result
Thus the book catalogue is displayed using XML , XSLT & DTD
65
Ex.No.04c XML with CSS
Aim
To Create a simple XML program with CSS
Procedure
i) Create a file with .css extension
ii) The css file should include the procedure to display the data in a formatted
manner
iii) Create a XML file with simple mail transfer details
iv) Include the css file into the XML file by using the following way
<?xml-stylesheet type=”text/css” href=”mystyle.css”?>
v) Close the program
Program
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="mystyle.css"?>
<EMAIL><Inbox>
<Mail>
<To> [email protected]</To>
<From>[email protected]</From>
<Subject> Service Required </Subject>
<Body> Please give service for the supply of Instrument </Body>
</Mail>
<Mail>
<To> [email protected]</To>
<From>[email protected]</From>
<Subject> Thanks </Subject>
<Body> Thanks for the supply of Instrument </Body>
</Mail>
</Inbox>
</EMAIL>
CSS file(mystyle.css)
To
{
background-color:#ffccff;
width:100%;
}
From
{
background-color:#EEaaff;
width:100%;
}
Subject
66
{
background-color:#ffccff;
width:100%;
}
Body
{
background-color:#aaaaaa;
font-size:20pt;
color:#ccffcc;
margin-left:50pt;
}
Output
Result
Thus the XML program with css has been executed successfully
67
EX NO:5 ASP PROGRAM USING COMPONENTS
AIM
PROCEDURE
1. The ASP AdRotator component creates an AdRotator object that displays a different
image each time a user enters or refreshes a page.
2. A text file includes information about the images.
PROGRAM
adrotator.asp
<html>
<body>
<%
set adrotator=Server.CreateObject("MSWC.AdRotator")
response.write(adrotator.GetAdvertisement("sample.txt"))
%>
</body>
</html>
sample.txt
REDIRECT banners.asp
*
web.gif
Free Tutorials from W3Schools
50
mmc.gif
XML Editor from Altova
50
68
3. The BrowserType object then compares the information in the header with information
in a file on the server called "Browscap.ini".
4. If there is a match between the browser type and version number sent in the header and
the information in the "Browsercap.ini" file, you can use the BrowserType object to list
the properties of the matching browser.
5. If there is no match for the browser type and version number in the Browscap.ini file, it
will set every property to "UNKNOWN".
PROGRAM
sample.asp
<html>
<body>
<%
Set MyBrow=Server.CreateObject("MSWC.BrowserType")
%>
<table border="1" width="100%">
<tr>
<th>Client OS</th>
<th><%=MyBrow.platform%></th>
</tr><tr>
<td >Web Browser</td>
<td ><%=MyBrow.browser%></td>
</tr><tr>
<td>Browser version</td>
<td><%=MyBrow.version%></td>
</tr><tr>
<td>Frame support?</td>
<td><%=MyBrow.frames%></td>
</tr><tr>
<td>Table support?</td>
<td><%=MyBrow.tables%></td>
</tr><tr>
<td>Sound support?</td>
<td><%=MyBrow.backgroundsounds%></td>
</tr><tr>
<td>Cookies support?</td>
<td><%=MyBrow.cookies%></td>
</tr><tr>
<td>VBScript support?</td>
<td><%=MyBrow.vbscript%></td>
</tr><tr>
<td>JavaScript support?</td>
<td><%=MyBrow.javascript%></td>
</tr>
69
</table>
</body>
</html>
1. The ASP Content Linking component is used to create a quick and easy navigation
system
2. The Content Linking component returns a Nextlink object that is used to hold a list of
Web pages to be navigated.
Syntax
<%
Set nl=Server.CreateObject( "MSWC.NextLink" )
%>
PROGRAM
page1.asp
<html>
<body>
<h1>
This is page 1!
</h1>
<%
Set nl=Server.CreateObject("MSWC.NextLink")
If (nl.GetListIndex("links2.txt")>1) Then
%>
<a href="<%Response.Write(nl.GetPreviousURL("links2.txt"))%>">Previous Page</a>
<%End If%>
<a href="<%Response.Write(nl.GetNextURL("links2.txt"))%>">Next Page</a>
<p>The example uses the Content Linking Component
to navigate between the pages in a text file.</p>
<p>
<a href="links2.txt"><img border="0" src="warning.gif"></a>
</p>
</body>
</html>
page2.asp
<html>
<body>
<h1>This is page 2!</h1>
<%
Set nl=Server.CreateObject("MSWC.NextLink")
If (nl.GetListIndex("links2.txt")>1) Then
%>
70
<a href="<%Response.Write(nl.GetPreviousURL("links2.txt"))%>">Previous Page</a>
<%End If%>
<a href="<%Response.Write(nl.GetNextURL("links2.txt"))%>">Next Page</a>
<p>The example uses the Content Linking Component
to navigate between the pages in a text file.</p>
<p>
<a href="links2.txt"><img border="0" src="web.gif"></a>
</p>
</body>
</html>
page3.asp
<html>
<body>
<h1>This is page 3!</h1>
<%
Set nl=Server.CreateObject("MSWC.NextLink")
If (nl.GetListIndex("links2.txt")>1) Then
%>
<a href="<%Response.Write(nl.GetPreviousURL("links2.txt"))%>">Previous Page</a>
<%End If%>
<a href="<%Response.Write(nl.GetNextURL("links2.txt"))%>">Next Page</a>
<p>The example uses the Content Linking Component
to navigate between the pages in a text file.</p>
<p>
<a href="links2.txt"><img border="0" src="print.gif"></a>
</p>
</body>
</html>
links2.txt
page1.asp Page 1
page2.asp Page 2
page3.asp Page 3
1. The ASP Content Rotator component creates a ContentRotator object that displays a
different HTML content string each time a user enters or refreshes a page.
2. A text file, called the Content Schedule File, includes the information about the content
strings.
3. The content strings can contain HTML tags so you can display any type of content that
HTML can represent: text, images, colors, or hyperlinks.
Syntax
<% Set cr=Server.CreateObject( "MSWC.ContentRotator" ) %>
71
conrotator.asp
<html>
<body>
<%
set cr=server.createobject("MSWC.ContentRotator")
response.write(cr.ChooseContent("textads.txt"))
%>
</body>
</html>
textads.txt
%% #3
<h2>This is a great day!!</h2>
%% #3
<img src="winxp.gif">
%% #4
<a href="http://www.w3schools.com">Visit W3Schools.com</a>
OUTPUT
72
2. ASP Browser Capabilities Component
73
RESULT
Thus the ASP components are studied and a program is written and verified using the
components
74
EX NO:6 STUDENTS MARKLIST USING SERVLETS
Write a program in java to create servlets for displaying students mark list. Assume that
student information is available in a database which has been stored in a server.
AIM
PROCEDURE
PROGRAM
/*
* Exam.java
*
* Created on September 27, 2003, 4:39 PM
*/
package com;
75
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
Connection ConnRecordset;
PreparedStatement StatementRecordset;
ResultSet Recordset;
out.println("<td>");
out.print(Recordset.getString(2));
out.println("</td>");
int t;
for (int i=3;i<9;i++)
{ out.println("<td>");
t=Recordset.getInt(i);
total=total+t;
out.print(t);
out.println("</td>");
}
out.println("<td>");
out.print(total);
out.println("</td></tr>");
}
76
out.close();
Recordset.close();
StatementRecordset.close();
ConnRecordset.close();
}
catch(SQLException sqe)
{
out.print("Error"+sqe);
}
}
OUTPUT
77
RESULT:
Thus the Servlet program has been developed to display the students mark list.
78
EX NO:7 JSP PROGRAM FOR ORDER PROCESSING
AIM
PROCEDURE
STEP 1: Design a HTML form which includes product name,customer name, date of
purchase.
STEP 2:Get the required details from the user .
STEP 3: Using the details given, process the data using JSP.Retrieve the data from the database
using the given product number.
STEP 4: If the product is available, save the customer details and display the product details.
STEP 5: If there is no stock, display that the product is not available.
STEP 6: Execute and save the process.
PROGRAM
main.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<form name="form1" action="serverside.jsp" method="post">
79
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><h4>Mobile</h4></td>
<td><input type="text" name="mobile"></td>
</tr>
<tr>
<td><h4>Book Title</h4></td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td><h4>Author</h4></td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td><h4>Publisher</h4></td>
<td><input type="text" name="publisher"></td>
</tr>
<tr>
<td><h4>Edition</h4></td>
<td><input type="text" name="edition"></td>
</tr>
<tr>
<td><h4>No.of Books</h4></td>
<td><input type="text" name="quantity"></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</center>
</form>
<body>
</body>
</html>
serverside.jsp
80
String t1=" ";
String t2=" ";
String t3=" ";
String t4=" ";
String t5=" ";
String t6=" ";
String t7=" ";
String t8=" ";
String t9=" ";
int no;
%>
<%
try
{
t1=request.getParameter("name");
t2=request.getParameter("address");
t3=request.getParameter("email");
t4=request.getParameter("mobile");
t5=request.getParameter("title");
t6=request.getParameter("author");
t7=request.getParameter("publisher");
t8=request.getParameter("edition");
t9=request.getParameter("quantity");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, name, pass);
st=con.createStatement();
no=st.executeUpdate("insert into jsp_book
values('"+t1+"','"+t2+"','"+t3+"','"+t4+"','"+t5+"','"+t6+"','"+t7+"','"+t8+"','"+t9+"')");
con.close();
catch(SQLException e){
out.println("Error"+e);
81
}
%>
OUTPUT
RESULT
Thus the program to perform Order Processing using JSP has been created and executed
successfully.
82
Ex.No.8 C# Programming in .Net platform
Aim
To Create a namespace using C# in .net platform
Procedure
1. Start Microsoft Visual C# 2005 and create a new Console Application named
RealEstate1
2. To create a new class, in the Class View, right-click the name of the project,
position the mouse on Add and click Class...
3. Change the name to House and press Enter
4. Change the class as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RealEstate1
{
public class House
{
public string PropertyNumber;
public char PropertyType;
public uint Stories;
public uint Bedrooms;
public float Bathrooms;
public double Value;
}
}
5. Save the file
using System;
namespace Business
{
public class House
{
public string PropertyNumber;
public decimal Price;
}
}
83
{
static void Main()
{
Business.House property = new Business.House();
property.PropertyNumber = "D294FF";
property.Price = 425880;
Output
=//= Altair Realty =//=
Properties Inventory
Property #: D294FF
Market Value: 425880
Namespace Nesting
You can create one namespace inside of another namespace. Creating a namespace inside
of another is referred to as nesting the namespace. The namespace inside of another
namespace is nested. To create a namespace inside of another, simply type it as you
would create another namespace. Here is an example:
namespace Business
{
public class House
{
public string PropertyNumber;
public decimal Price;
}
namespace Dealer
{
}
}
In the example above, the Dealer namespace is nested inside of the Business namespace.
After creating the desired namespaces, nested or not, you can create the necessary
class(es) inside of the desired namespace. To access anything that is included in a nested
namespace, you use the period operator before calling a member of a namespace or
before calling the next nested namespace. Here is an example:
using System;
namespace Business
{
public class House
84
{
public string PropertyNumber;
public decimal Price;
}
namespace Dealer
{
public class Car
{
public decimal Price;
}
}
}
property.PropertyNumber = "D294FF";
property.Price = 425880;
Output
Result
Thus the program for creating a namespace using c# in .net platform has been executed
successfully.
85
References
Websites
1) http://www.functionx.com/csharp/Lesson05.htm
2) http://www.w3schools.com
3) http://www.w3c.org
Books
1) R.Bremananth-C.S. Senthil Raja, V.Sivakumar – Web Technology – Pradeepa
Publishers
2) Rashim Mogha, Preetham.V.V., “ Java Web Services Programming”, Wiley
Dreamtech, New Delhi, 2002.
3) Achyut S Godbole and Atul Kahate, “Web Technologies – TCP/IP
Architectures and Java Programming”, Second Edition, Tata Mc-Graw Hill
Education Pvt., Ltd., New Delhi, 2009
4). E Balagurusamy, “Programming in C#”, Second Edition, Tata Mc-Graw hill
Publishing Co. Ltd., New Delhi, 2008
5) Deitel ,“ XML How to Program”, first edition, Pearson Education, USA, 2002.
6) Jason Hunter, William Crawford, “Java Servlet Programming”, O’ Reilly
Publications, USA, 1998.
86