HTML CSS pdf
HTML CSS pdf
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
</
body>
</html>
HTML Lists
An Unordered List:
Item
Item
Item
Item
An Ordered List:
1. First item
2. Second item
3. Third item
4. Fourth item
The list items will be marked with bullets (small black circles) by default:
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Example - Disc
<ul style="list-style-type:disc">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Example - Circle
<ul style="list-style-type:circle">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Example - Square
<ul style="list-style-type:square">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Example - None
<ul style="list-style-type:none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Ordered HTML List
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Type Description
type="I" The list items will be numbered with uppercase roman numbers
type="i" The list items will be numbered with lowercase roman numbers
Numbers:
<ol type="1">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Uppercase Letters:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Lowercase Letters:
<ol type="a">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<ol type="I">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<ol type="i">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
The <dl> tag defines the description list, the <dt> tag defines the term (name), and the <dd>
tag describes each term:
Example
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Nested HTML Lists
List can be nested (lists inside lists):
Example
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
Note: List items can contain new list, and other HTML elements, like images and links, etc.
Horizontal Lists
HTML lists can be styled in many different ways with CSS.
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
li a:hover {
background-color: #111111;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
When you move the mouse over a link, the mouse arrow will turn into a little hand.
Note: A link does not have to be text. It can be an image or any other HTML element.
Example
<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text will send you to the specified address.
Note: Without a forward slash on subfolder addresses, you might generate two requests to the
server. Many servers will automatically add a forward slash to the address, and then create a
new request.
Local Links
The example above used an absolute URL (A full web address).
Example
Each table row is defined with the <tr> tag. A table header is defined with the <th> tag. By
default, table headings are bold and centered. A table data/cell is defined with the <td> tag.
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Note: The <td> elements are the data containers of the table.
They can contain all sorts of HTML elements; text, images, lists, other tables, etc.
Example
table, th, td {
border: 1px solid black;
}
Remember to define borders for both the table and the table cells.
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
If you do not specify a padding, the table cells will be displayed without padding.
Example
th, td {
padding: 15px;
}
Example
th {
text-align: left;
}
To set the border spacing for a table, use the CSS border-spacing property:
Example
table {
border-spacing: 5px;
}
Example
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>55577854</td>
<td>55577855</td>
</tr>
</table>
Example
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>55577854</td>
</tr>
<tr>
<td>55577855</td>
</tr>
</table>
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Note: The <caption> tag must be inserted immediately after the <table> tag.
Example
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color: #fff;
}
table#t01 th {
color: white;
background-color: black;
}
Summary
Use the HTML <table> element to define a table
Use the HTML <tr> element to define a table row
Use the HTML <td> element to define a table data
Use the HTML <th> element to define a table heading
Use the HTML <caption> element to define a table caption
Use the CSS border property to define a border
Use the CSS border-collapse property to collapse cell borders
Use the CSS padding property to add padding to cells
Use the CSS text-align property to align cell text
Use the CSS border-spacing property to set the spacing between cells
Use the colspan attribute to make a cell span many columns
Use the rowspan attribute to make a cell span many rows
Use the id attribute to uniquely define one tabl
Form elements are different types of input elements, like text fields, checkboxes, radio
buttons, submit buttons, and more.
The <input> element can be displayed in several ways, depending on the type attribute.
Type Description
<input type="text"> Defines a one-line text input field
<input type="radio"> Defines a radio button (for selecting one of many choices)
<input type="submit"> Defines a submit button (for submitting the form)
Text Input
<input type="text"> defines a one-line input field for text input:
Example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
Example
<form>
User name:<br>
<input type="text" name="username"><br>
User password:<br>
<input type="password" name="psw">
</form>
The form-handler is typically a server page with a script for processing input data.
Example
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
If you omit the submit button's value attribute, the button will get a default text:
Example
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit">
</for
m>
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John">
</form>
HTML frames are used to divide your browser window into multiple sections where
each section can load a separate HTML document. A collection of frames in the browser
window is known as a frameset. The window is divided into frames in a similar way the
tables are organized: into rows and columns.
Disadvantages of Frames
There are few drawbacks with using frames, so it's never recommended to use frames in your
webpages −
Some smaller devices cannot cope with frames often because their screen is not big
enough to be divided up.
Sometimes your page will be displayed differently on different computers due to
different screen resolution.
The browser's back button might not work as the user hopes.
There are still few browsers that do not support frame technology.
Creating Frames
To use frames on a page we use <frameset> tag instead of <body> tag. The <frameset> tag
defines, how to divide the window into frames. The rows attribute of <frameset> tag defines
horizontal frames and cols attribute defines vertical frames. Each frame is indicated by
<frame> tag and it defines which HTML document shall open into the frame.
Note − The <frame> tag deprecated in HTML5. Do not use this element.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<noframes>
<body>Your browser does not support frames.</body>
</noframes>
</frameset>
</html>
Example
Let's put the above example as follows, here we replaced rows attribute by cols and changed
their width. This will create all the three frames vertically −
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<noframes>
<body>Your browser does not support frames.</body>
</noframes>
</frameset>
</html>
Division:-
A section in a document that will be displayed in blue:
<div style="color:#0000FF">
<h3>This is a heading</h3>
<p>This is a paragraph.</p>
</div>
The <div> tag is used to group block-elements to format them with CSS.
Global Attributes
The <div> tag also supports the Global Attributes in HTML.
Event Attributes
The <div> tag also supports the Event Attributes in HTML.
What is CSS
CSS is an acronym stands for Cascading Style Sheets. It is a style sheet language
which is used to describe the look and formatting of a document written in markup
language. It provides an additional feature to HTML. It is generally used with HTML to
change the style of web pages and user interfaces. It can also be used with any kind
of XML documents including plain XML, SVG and XUL.
CSS is used along with HTML and JavaScript in most websites to create user
interfaces for web applications and user interfaces for many mobile applications.
o You can completely change the look of your website with only a few changes in
CSS code.
Before CSS, tags like font, color, background style, element alignments, border and
size had to be repeated on every web page. This was a very long process. For
example: If you are developing a large website where fonts and color information are
added on every single page, it will be become a long and expensive process. CSS was
created to solve this problem. It was a W3C recommendation.
CSS style definitions are saved in external CSS files so it is possible to change the
entire website by changing just one file.
CSS provides more detailed attributes than plain HTML to define the look and feel of
the website.
CSS Syntax
A CSS rule set contains a selector and a declaration block.
Selector: Selector indicates the HTML element you want to style. It could be any tag
like <h1>, <title> etc.
Declaration Block: The declaration block can contain one or more declarations
separated by a semicolon. For the above example, there are two declarations:
1. color: yellow;
2. font-size: 11 px;
Value: Values are assigned to CSS properties. In the above example, value "yellow"
is assigned to color property.
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the
part of CSS rule set. CSS selectors select HTML elements according to its id, class,
type, attribute etc.
2. CSS Id Selector
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <p>This style will be applied on every paragraph.</p>
13. <p id="para1">Me too!</p>
14. <p>And me!</p>
15. </body>
16. </html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
Test it Now
Output:
This style will be applied on every paragraph.
Me too!
And me!
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific
element. An id is always unique within the page so it is chosen to select a single,
unique element.
It is written with the hash character (#), followed by the id of the element.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. #para1 {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <p id="para1">Hello Javatpoint.com</p>
13. <p>This paragraph will not be affected.</p>
14. </body>
15. </html>
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
Test it Now
Output:
Hello Javatpoint.com
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is blue and center-aligned.</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html>
<!DOCTYPE html>
<html>
<head>
<style>
.center {
Test it Now
Output:
If you want to specify that only one specific HTML element should be affected then
you should use the element name with class selector.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is not affected</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html>
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. * {
6. color: green;
7. font-size: 20px;
8. }
9. </style>
10. </head>
11. <body>
12. <h2>This is heading</h2>
13. <p>This style will be applied on every paragraph.</p>
14. <p id="para1">Me too!</p>
15. <p>And me!</p>
16. </body>
17. </html>
<!DOCTYPE html>
<html>
<head>
<style>
*{
Test it Now
Output:
This is heading
Me too!
And me!
5) CSS Group Selector
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to separate each
selector in grouping.
1. h1 {
2. text-align: center;
3. color: blue;
4. }
5. h2 {
6. text-align: center;
7. color: blue;
8. }
9. p {
10. text-align: center;
11. color: blue;
12. }
h1 {
text-align: center;
color: blue;
}
h2 {
As you can see, you need to define CSS properties for all the elements. It can be
grouped in following ways:
1. h1,h2,p {
2. text-align: center;
3. color: blue;
4. }
h1,h2,p {
text-align: center;
color: blue;
}
Test it Now
Output:
Hello Javatpoint.com
Hello Javatpoint.com (In smaller font)
This is a paragraph.
1. Inline CSS
2. Internal CSS
3. External CSS
1) Inline CSS
Inline CSS is used to apply CSS on a single line or element.
For example:
2) Internal CSS
Internal CSS is used to apply CSS on a single document or page. It can affect all
the elements of the page. It is written inside the style tag within head section of
html.
For example:
1. <style>
2. p{color:blue}
3. </style>
<style>
p{color:blue}
</style>
3) External CSS
External CSS is used to apply CSS on multiple pages or all pages. Here, we write
all the CSS code in a css file. Its extension must be .css for example style.css.
For example:
1. p{color:blue}
p{color:blue}
You need to link this style.css file to your html pages like this:
Inline CSS
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document. This
method mitigates some advantages of style sheets so it is advised to use this
method sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant
tag.
Syntax:
Example:
1. <h2 style="color:red;margin-
left:40px;">Inline CSS is applied on this heading.</h2>
2. <p>This paragraph is not affected.</p>
<h2 style="color:red;margin-left
<p>This paragraph is not affect
Test it Now
Output:
o These styles are tough to be edited because they are not stored at a single
place.
Internal CSS
The internal style sheet is used to add a unique style for a single document. It is
defined in <head> section of the HTML page inside the <style> tag.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-color: linen;
7. }
8. h1 {
9. color: red;
10. margin-left: 80px;
11. }
12. </style>
13. </head>
14. <body>
15. <h1>The internal style sheet is applied on this heading.</h1>
16. <p>This paragraph will not be affected.</p>
17. </body>
18. </html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
External CSS
The external style sheet is generally used when you want to make changes on
multiple pages. It is ideal for this condition because it facilitates you to change
the look of the entire web site by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside
the head section.
Example:
1. <head>
2. <link rel="stylesheet" type="text/css" href="mystyle.css">
3. </head>
<head>
<link rel="stylesheet" type="text
</head>
The external style sheet may be written in any text editor but must be saved with
a .css extension. This file should not contain HTML elements.
File: mystyle.css
1. body {
2. background-color: lightblue;
3. }
4. h1 {
5. color: navy;
6. margin-left: 20px;
7. }
body {
background-color: lightblue;
}
h1 {
color: navy;
Note: You should not use a space between the property value and the unit. For
example: It should be margin-left:20px not margin-left:20 px.
CSS Font
CSS Font property is used to control the look of texts. By the use of CSS font
property you can change the text size, color, style and more. You have already
studied how to make text bold or underlined. Here, you will also know how to
resize your font using percentage.
1. CSS Font color: This property is used to change the color of the text.
(standalone attribute)
2. CSS Font family: This property is used to change the face of the font.
3. CSS Font size: This property is used to increase or decrease the size of
the font.
4. CSS Font style: This property is used to make the font bold, italic or
oblique.
5. CSS Font variant: This property creates a small-caps effect.
o By a color name
o By hexadecimal value
o By RGB
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. font-size: 100%;
7. }
8. h1 { color: red; }
9. h2 { color: #9000A1; }
10. p { color:rgb(0, 220, 98); }
11. }
12. </style>
13. </head>
14. <body>
15. <h1>This is heading 1</h1>
16. <h2>This is heading 2</h2>
17. <p>This is a paragraph.</p>
18. </body>
19. </html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
Test it Now
Output:
This is heading 1
This is heading 2
This is a paragraph.
o Font family: It specifies the font family name like Arial, New Times
Roman etc.
Serif: Serif fonts include small lines at the end of characters. Example of serif:
Times new roman, Georgia etc.
Sans-serif: A sans-serif font doesn't include the small lines at the end of
characters. Example of Sans-serif: Arial, Verdana etc.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. font-size: 100%;
7. }
8. h1 { font-family: sans-serif; }
9. h2 { font-family: serif; }
10. p { font-family: monospace; }
11. }
12. </style>
13. </head>
14. <body>
15. <h1>This heading is shown in sans-serif.</h1>
16. <h2>This heading is shown in serif.</h2>
17. <p>This paragraph is written in monospace.</p>
18. </body>
19. </html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
Test it Now
Output:
These are the possible values that can be used to set the font size:
Font Size Value Description
1. <html>
2. <head>
3. <title>Practice CSS font-size property</title>
4. </head>
5. <body>
6. <p style="font-size:xx-small;"> This font size is extremely small.</p>
7. <p style="font-size:x-small;"> This font size is extra small</p>
8. <p style="font-size:small;"> This font size is small</p>
9. <p style="font-size:medium;"> This font size is medium. </p>
10. <p style="font-size:large;"> This font size is large. </p>
11. <p style="font-size:x-large;"> This font size is extra large. </p>
12. <p style="font-size:xx-large;"> This font size is extremely large. </p>
13. <p style="font-size:smaller;"> This font size is smaller. </p>
14. <p style="font-size:larger;"> This font size is larger. </p>
15. <p style="font-size:200%;"> This font size is set on 200%. </p>
16. <p style="font-size:20px;"> This font size is 20 pixels. </p>
17. </body>
18. </html>
<html>
<head>
<title>Practice CSS font-size pro
</head>
<body>
Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. font-size: 100%;
7. }
8. h2 { font-style: italic; }
9. h3 { font-style: oblique; }
10. h4 { font-style: normal; }
11. }
12. </style>
13. </head>
14. <body>
15. <h2>This heading is shown in italic font.</h2>
16. <h3>This heading is shown in oblique font.</h3>
17. <h4>This heading is shown in normal font.</h4>
18. </body>
19. </html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p { font-variant: small-caps; }
6. h3 { font-variant: normal; }
7. </style>
8. </head>
9. <body>
10. <h3>This heading is shown in normal font.</h3>
11. <p>This paragraph is shown in small font.</p>
12. </body>
13. </html>
<!DOCTYPE html>
<html>
<head>
<style>
p { font-variant: small-caps; }
Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <p style="font-weight:bold;">This font is bold.</p>
5. <p style="font-weight:bolder;">This font is bolder.</p>
6. <p style="font-weight:lighter;">This font is lighter.</p>
7. <p style="font-weight:100;">This font is 100 weight.</p>
8. <p style="font-weight:200;">This font is 200 weight.</p>
9. <p style="font-weight:300;">This font is 300 weight.</p>
10. <p style="font-weight:400;">This font is 400 weight.</p>
11. <p style="font-weight:500;">This font is 500 weight.</p>
12. <p style="font-weight:600;">This font is 600 weight.</p>
13. <p style="font-weight:700;">This font is 700 weight.</p>
14. <p style="font-weight:800;">This font is 800 weight.</p>
15. <p style="font-weight:900;">This font is 900 weight.</p>
16. </body>
17. </html>
<!DOCTYPE html>
<html>
<body>
<p style="font-w eight:bold;">Th
<p style="font-w eight:bolder;">T
Test it Now
Output:
Java script
JavaScript is an object-based scripting language that is
lightweight and cross-platform.
o Client-side validation
JavaScript Example
1. <h2>Welcome to JavaScript</h2>
2. <script>
3. document.write("Hello JavaScript by JavaScript");
</script>
JavaScript Example
1. JavaScript Example
1. <script type="text/javascript">
2. document.write("JavaScript is a simple language for javatpoint learners");
3. </script>
<script type="text/javascript">
document.w rite("JavaScript is a
</script>
Test it Now
1. <script type="text/javascript">
2. alert("Hello Javatpoint");
3. </script>
<script type="text/javascript">
alert("Hello Javatpoint");
</script>
Test it Now
To call function, you need to work on event. Here we are using onclick event to
call msg() function.
1. <html>
2. <head>
3. <script type="text/javascript">
4. function msg(){
5. alert("Hello Javatpoint");
6. }
7. </script>
8. </head>
9. <body>
10. <p>Welcome to JavaScript</p>
11. <form>
12. <input type="button" value="click" onclick="msg()"/>
13. </form>
14. </body>
15. </html>
<html>
<head>
<script type="text/javascript">
function msg(){
alert("Hello Javatpoint");
Let’s create an external JavaScript file that prints Hello Javatpoint in a alert
dialog box.
message.js
1. function msg(){
2. alert("Hello Javatpoint");
3. }
function msg(){
alert("Hello Javatpoint");
}
Let’s include the JavaScript file into html page. It calls the JavaScript function on
button click.
index.html
1. <html>
2. <head>
3. <script type="text/javascript" src="message.js"></script>
4. </head>
5. <body>
6. <p>Welcome to JavaScript</p>
7. <form>
8. <input type="button" value="click" onclick="msg()"/>
9. </form>
10. </body>
11. </html>
<html>
<head>
<script type="text/javascript" sr
</head>
<body>
1. window.document
w indow .document
Is same as
1. document
document
According to W3C - "The W3C Document Object Model (DOM) is a platform and
language-neutral interface that allows programs and scripts to dynamically
access and update the content, structure, and style of a document."
Method Description
Here, document is the root element that represents the html document.
value is the property, that returns the value of the input text.
Let's see the simple example of document object that prints name with
welcome message.
1. <script type="text/javascript">
2. function printvalue(){
3. var name=document.form1.name.value;
4. alert("Welcome: "+name);
5. }
6. </script>
7.
8. <form name="form1">
9. Enter Name:<input type="text" name="name"/>
10. <input type="button" onclick="printvalue()" value="print name"/>
11. </form>
<script type="text/javascript">
function printvalue(){
var name=document.form1.nam
alert("Welcome: "+name);
}
Enter Name:
Javascript - document.getElementById()
method
1. getElementById() method
2. Example of getElementById()
1. <script type="text/javascript">
2. function getcube(){
3. var number=document.getElementById("number").value;
4. alert(number*number*number);
5. }
6. </script>
7. <form>
8. Enter No:<input type="text" id="number" name="number"/><br/>
9. <input type="button" value="cube" onclick="getcube()"/>
10. </form>
<script type="text/javascript">
function getcube(){
var number=document.getEleme
alert(number*number*number);
}
Enter No:
Javascript -
document.getElementsByName() method
1. getElementsByName() method
2. Example of getElementsByName()
1. document.getElementsByName("name")
document.getElementsByName(
Example of document.getElementsByName()
method
Male: Female:
Bottom of FoJavascript -
document.getElementsByTagName()
method
1. getElementsByTagName() method
2. Example of getElementsByTagName()
1. document.getElementsByTagName("name")
document.getElementsByTagNa
Example of document.getElementsByTagName()
method
1. <script type="text/javascript">
2. function countpara(){
3. var totalpara=document.getElementsByTagName("p");
4. alert("total p tags are: "+totalpara.length);
5.
6. }
7. </script>
8. <p>This is a pragraph</p>
9. <p>Here we are going to count total number of paragraphs by getElementByT
agName() method.</p>
10. <p>Let's see the simple example</p>
11. <button onclick="countpara()">count paragraph</button>
<script type="text/javascript">
function countpara(){
var totalpara=document.getElem
alert("total p tags are: "+totalpar
This is a pragraph
count paragraph
Another example of
document.getElementsByTagName()
method
In this example, we going to count total number of h2 and h3 tags used in the
document.
1. <script type="text/javascript">
2. function counth2(){
3. var totalh2=document.getElementsByTagName("h2");
4. alert("total h2 tags are: "+totalh2.length);
5. }
6. function counth3(){
7. var totalh3=document.getElementsByTagName("h3");
8. alert("total h3 tags are: "+totalh3.length);
9. }
10. </script>
11. <h2>This is h2 tag</h2>
12. <h2>This is h2 tag</h2>
13. <h3>This is h3 tag</h3>
14. <h3>This is h3 tag</h3>
15. <h3>This is h3 tag</h3>
16. <button onclick="counth2()">count h2</button>
17. <button onclick="counth3()">count h3</button>
<script type="text/javascript">
function counth2(){
var totalh2=document.getElemen
alert("total h2 tags are: "+totalh2
}
This is h2 tag
This is h2 tag
This is h3 tag
This is h3 tag
This is h3 tag
count h2 count h3
Note: Output of the given examples may differ on this page because it will count the
total number of para , total number of h2 and total number of h3 tags used in this
document
Javascript - innerHTML
1. javascript innerHTML
The innerHTML property can be used to write the dynamic html on the html
document.
It is used mostly in the web pages to generate the dynamic html such as
registration form, comment form, links etc.
In this example, we are dynamically writing the html form inside the div name
having the id mylocation. We are identifing this position by calling the
document.getElementById() method.
Test it Now
Javascript - innerText
1. javascript innerText
The innerText property can be used to write the dynamic text on the html
document. Here, text will not be interpreted as html text but a normal text.
It is used mostly in the web pages to generate the dynamic content such as
writing the validation message, password strength etc.
Test it Now
Strength:no strength
It is important to validate the form submitted by the user because it can have
inappropriate values. So validation is must.
The JavaScript provides you the facility the validate the form on the client side
so processing will be fast than server-side validation. So, most of the web
developers prefer JavaScript form validation.
Here, we are validating the form on form submit. The user will not be
forwarded to the next page until given values are correct.
1. <script>
2. function validateform(){
3. var name=document.myform.name.value;
4. var password=document.myform.password.value;
5.
6. if (name==null || name==""){
7. alert("Name can't be blank");
8. return false;
9. }else if(password.length<6){
10. alert("Password must be at least 6 characters long.");
11. return false;
12. }
13. }
14. </script>
15. <body>
16. <form name="myform" method="post" action="abc.jsp" onsubmit="return v
alidateform()" >
17. Name: <input type="text" name="name"><br/>
18. Password: <input type="password" name="password"><br/>
19. <input type="submit" value="register">
20. </form>
<script>
function validateform(){
var name=document.myform.na
var passw ord=document.myfor
Test it Now
JavaScript Retype Password Validation
1. <script type="text/javascript">
2. function matchpass(){
3. var firstpassword=document.f1.password.value;
4. var secondpassword=document.f1.password2.value;
5.
6. if(firstpassword==secondpassword){
7. return true;
8. }
9. else{
10. alert("password must be same!");
11. return false;
12. }
13. }
14. </script>
15.
16. <form name="f1" action="register.jsp" onsubmit="return matchpass()">
17. Password:<input type="password" name="password" /><br/>
18. Re-enter Password:<input type="password" name="password2"/><br/>
19. <input type="submit">
20. </form>
<script type="text/javascript">
function matchpass(){
var firstpassw ord=document.f1
var secondpassw ord=documen
Test it Now
1. <script>
2. function validate(){
3. var num=document.myform.num.value;
4. if (isNaN(num)){
5. document.getElementById("numloc").innerHTML="Enter Numeric value only"
;
6. return false;
7. }else{
8. return true;
9. }
10. }
11. </script>
12. <form name="myform" onsubmit="return validate()" >
13. Number: <input type="text" name="num"><span id="numloc"></span><
br/>
14. <input type="submit" value="submit">
15. </form>
<script>
function validate(){
var num=document.myform.num
if (isNaN(num)){
document.getElementById("num
Test it Now
1. <script>
2. function validate(){
3. var name=document.f1.name.value;
4. var password=document.f1.password.value;
5. var status=false;
6.
7. if(name.length<1){
8. document.getElementById("nameloc").innerHTML=
9. " <img src='unchecked.gif'/> Please enter your name";
10. status=false;
11. }else{
12. document.getElementById("nameloc").innerHTML=" <img src='checked.gif'/>
";
13. status=true;
14. }
15. if(password.length<6){
16. document.getElementById("passwordloc").innerHTML=
17. " <img src='unchecked.gif'/> Password must be at least 6 char long";
18. status=false;
19. }else{
20. document.getElementById("passwordloc").innerHTML=" <img src='checked.gi
f'/>";
21. }
22. return status;
23. }
24. </script>
25.
26. <form name="f1" action="#" onsubmit="return validate()">
27. <table>
28. <tr><td>Enter Name:</td><td><input type="text" name="name"/>
29. <span id="nameloc"></span></td></tr>
30. <tr><td>Enter Password:</td><td><input type="password" name="pass
word"/>
31. <span id="passwordloc"></span></td></tr>
32. <tr><td colspan="2"><input type="submit" value="register"/></td></tr
>
33. </table>
34. </form>
<script>
function validate(){
var name=document.f1.name.va
var passw ord=document.f1.pas
var status=false;
Test it Now
Output:
Enter Name:
Enter Password:
register
There are many criteria that need to be follow to validate the email id such as:
1. <script>
2. function validateemail()
3. {
4. var x=document.myform.email.value;
5. var atposition=x.indexOf("@");
6. var dotposition=x.lastIndexOf(".");
7. if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
8. alert("Please enter a valid e-
mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);
9. return false;
10. }
11. }
12. </script>
13. <body>
14. <form name="myform" method="post" action="#" onsubmit="return validat
eemail();">
15. Email: <input type="text" name="email"><br/>
16.
17. <input type="submit" value="register">
18. </form>
<script>
function validateemail()
{
var x=document.myform.email.v
var atposition=x.indexOf("@");
Events Description
All the operations that you perform on a data such as searching, sorting, insertion,
manipulation, deletion etc. can be performed by Java Collections.
Java Collection simply means a single unit of objects. Java Collection framework provides
many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList,
PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
Collection framework represents a unified architecture for storing and manipulating group of
objects. It has:
Let us see the hierarchy of collection framework.The java.util package contains all the
classes and interfaces for Collection framework.
There are many methods declared in the Collection interface. They are as follows:
2 public boolean addAll(Collection is used to insert the specified collection elements in the
c) invoking collection.
6 public int size() return the total number of elements in the collection.
7 public void clear() removes the total no of element from the collection.
public boolean
9 is used to search the specified collection in this collection.
containsAll(Collection c)
Iterator interface
Iterator interface provides the facility of iterating the elements in forward direction only.
There are only three methods in the Iterator interface. They are:
public boolean
1 It returns true if iterator has more elements.
hasNext()
It returns the element and moves the cursor pointer to the next
2 public Object next()
element.
3 public void remove() It removes the last elements returned by the iterator. It is rarely used.
1. ArrayList class
2. LinkedList class
3. List interface
4. HashSet class
5. LinkedHashSet class
6. TreeSet class
7. PriorityQueue class
8. Map interface
9. HashMap class
10. LinkedHashMap class
11. TreeMap class
12. Hashtable class
13. Sorting
14. Comparable interface
15. Comparator interface
16. Properties class in Java
As shown in above diagram, Java ArrayList class extends AbstractList class which
implements List interface. The List interface extends Collection and Iterable interfaces in
hierarchical order.
ArrayList(Collection It is used to build an array list that is initialized with the elements of the
c) collection c.
ArrayList(int
It is used to build an array list that has the specified initial capacity.
capacity)
void add(int index, Object It is used to insert the specified element at the specified position index
element) in a list.
It is used to return the index in this list of the last occurrence of the
int lastIndexOf(Object o)
specified element, or -1 if the list does not contain this element.
Object[] toArray(Object[] It is used to return an array containing all of the elements in this list in
a) the correct order.
boolean add(Object o) It is used to append the specified element to the end of a list.
boolean addAll(int index, It is used to insert all of the elements in the specified collection into this
Collection c) list, starting at the specified position.
It is used to return the index in this list of the first occurrence of the
int indexOf(Object o)
specified element, or -1 if the List does not contain this element.
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.
Java new generic collection allows you to have only one type of object in collection. Now it
is type safe so typecasting is not required at run time.
In generic collection, we specify the type in angular braces. Now ArrayList is forced to have
only specified type of objects in it. If you try to add another type of object, it gives compile
time error.
For more information of java generics, click here Java Generics Tutorial.
Java ArrayList Example
import java.util.*;
class TestCollection1{
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:-
Ravi
Vijay
Ravi
Ajay
1. By Iterator interface.
2. By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to
traverse ArrayList elements using for-each loop.
import java.util.*;
class TestCollection2{
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
for(String obj:al)
System.out.println(obj);
OUTPUT:-
Ravi
Vijay
Ravi
Ajay
Let's see an example where we are storing Student class object in array list.
class Student{
int rollno;
String name;
int age;
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
import java.util.*;
//creating arraylist
al.add(s2);
al.add(s3);
//Getting Iterator
Iterator itr=al.iterator();
//traversing elements of ArrayList object
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
` }
Output:-
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
import java.util.*;
class TestCollection4{
al.add("Ravi");
al.add("Vijay");
al.add("Ajay");
al2.add("Sonoo");
al2.add("Hanumat");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:-
Ravi
Vijay
Ajay
Sonoo
Hanumat
import java.util.*;
class TestCollection5{
al.add("Ravi");
al.add("Vijay");
al.add("Ajay");
ArrayList<String> al2=new ArrayList<String>();
al2.add("Ravi");
al2.add("Hanumat");
al.removeAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:-
iterating the elements after removing the elements of al2...
Vijay
Ajay
import java.util.*;
class TestCollection6{
al.add("Ravi");
al.add("Vijay");
al.add("Ajay");
al2.add("Ravi");
al2.add("Hanumat");
al.retainAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:-
Let's see an ArrayList example where we are adding books to list and printing all the books.
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
//Creating Books
list.dd(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
Output:
Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list
data structure. It inherits the AbstractList class and implements List and Deque interfaces.
As shown in above diagram, Java LinkedList class extends AbstractSequentialList class and
implements List and Deque interfaces.
In case of doubly linked list, we can add or remove elements from both side.
LinkedList class declaration
void add(int index, Object It is used to insert the specified element at the specified position index
element) in a list.
void addFirst(Object o) It is used to insert the given element at the beginning of a list.
void addLast(Object o) It is used to append the given element to the end of a list.
boolean add(Object o) It is used to append the specified element to the end of a list.
boolean contains(Object o) It is used to return true if the list contains a specified element.
boolean remove(Object o) It is used to remove the first occurence of the specified element in a list.
int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the
specified element, or -1 if the list does not contain any element.
import java.util.*;
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:
Ravi
Vijay
Ravi
Ajay
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
//Creating Books
list.add(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
Output:
ArrayList and LinkedList both implements List interface and maintains insertion order. Both
are non synchronized classes.
But there are many differences between ArrayList and LinkedList classes that are given
below.
ArrayList LinkedList
1) ArrayList internally uses dynamic array to store LinkedList internally uses doubly linked list to
the elements. store the elements.
2) Manipulation with ArrayList is slow because it Manipulation with LinkedList is faster than
internally uses array. If any element is removed from ArrayList because it uses doubly linked list so
the array, all the bits are shifted in memory. no bit shifting is required in memory.
4) ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
import java.util.*;
class TestArrayLinked{
public static void main(String args[]){
System.out.println("arraylist: "+al);
System.out.println("linkedlist: "+al2);
}
Output:
arraylist: [Ravi,Vijay,Ravi,Ajay]
linkedlist: [James,Serena,Swati,Junaid]
List can contain duplicate elements whereas Set contains unique elements only.
The HashSet class extends AbstractSet class which implements Set interface. The Set
interface inherits Collection and Iterable interfaces in hierarchical order.
HashSet(Collection
It is used to initialize the hash set by using the elements of the collection c.
c)
It is used to initialize the capacity of the hash set to the given integer value
HashSet(int
capacity. The capacity grows automatically as elements are added to the
capacity)
HashSet.
void clear() It is used to remove all of the elements from this set.
boolean
It is used to return true if this set contains the specified element.
contains(Object o)
boolean add(Object o) It is used to adds the specified element to this set if it is not already present.
boolean isEmpty() It is used to return true if this set contains no elements.
boolean remove(Object
It is used to remove the specified element from this set if it is present.
o)
Iterator iterator() It is used to return an iterator over the elements in this set.
import java.util.*;
class TestCollection9{
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
Output:-
Ajay
Vijay
Ravi
Java HashSet Example: Book
Let's see a HashSet example where we are adding books to set and printing all the books.
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
//Creating Books
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing HashSet
for(Book b:set){
Output:
The LinkedHashSet class extends HashSet class which implements Set interface. The Set
interface inherits Collection and Iterable interfaces in hierarchical order.
LinkedHashSet(int capacity, It is used to initialize both the capacity and the fill ratio (also called
float fillRatio) load capacity) of the hash set from its argument.
import java.util.*;
class TestCollection10{
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:-
Ravi
Vijay
Ajay
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
//Creating Books
hs.add(b1);
hs.add(b2);
hs.add(b3);
for(Book b:hs){
Output:
Java TreeSet class implements the Set interface that uses a tree for storage. It inherits
AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are
stored in ascending order.
As shown in above diagram, Java TreeSet class implements NavigableSet interface. The
NavigableSet interface extends SortedSet, Set, Collection and Iterable interfaces in
hierarchical order.
TreeSet class declaration
It is used to build a new tree set that contains the elements of the
TreeSet(Collection c)
collection c.
TreeSet(Comparator It is used to construct an empty tree set that will be sorted according to
comp) given comparator.
boolean addAll(Collection It is used to add all of the elements in the specified collection to this
c) set.
boolean contains(Object o) It is used to return true if this set contains the specified element.
boolean remove(Object o) It is used to remove the specified element from this set if it is present.
void clear() It is used to remove all of the elements from this set.
Object last() It is used to return the last (highest) element currently in this sorted
set.
import java.util.*;
class TestCollection11{
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
//Traversing elements
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Test it Now
Output:
Ajay
Ravi
Vijay
Let's see a TreeSet example where we are adding books to set and printing all the books. The
elements in TreeSet must be of Comparable type. String and Wrapper classes are Comparable
by default. To add user-defined objects in TreeSet, you need to implement Comparable
interface.
import java.util.*;
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
if(id>b.id){
return 1;
}else if(id<b.id){
return -1;
}else{
return 0;
//Creating Books
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing TreeSet
for(Book b:set){
Output:
Java Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first
element is removed first and last element is removed at last.
boolean It is used to insert the specified element into this queue and return true upon
add(object) success.
boolean
It is used to insert the specified element into this queue.
offer(object)
Object remove() It is used to retrieves and removes the head of this queue.
It is used to retrieves and removes the head of this queue, or returns null if this
Object poll()
queue is empty.
Object element() It is used to retrieves, but does not remove, the head of this queue.
It is used to retrieves, but does not remove, the head of this queue, or returns
Object peek()
null if this queue is empty.
PriorityQueue class
The PriorityQueue class provides the facility of using queue. But it does not orders the
elements in FIFO manner. It inherits AbstractQueue class.
import java.util.*;
class TestCollection12{
queue.add("Amit");
queue.add("Vijay");
queue.add("Karan");
queue.add("Jai");
queue.add("Rahul");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
queue.remove();
queue.poll();
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
Output:-
head:Amit
head:Amit
iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
after removing two elements:
Karan
Rahul
Vijay
Let's see a PriorityQueue example where we are adding books to queue and printing all the
books. The elements in PriorityQueue must be of Comparable type. String and Wrapper
classes are Comparable by default. To add user-defined objects in PriorityQueue, you need to
implement Comparable interface.
import java.util.*;
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
if(id>b.id){
return 1;
}else if(id<b.id){
return -1;
}else{
return 0;
//Creating Books
queue.add(b1);
queue.add(b2);
queue.add(b3);
for(Book b:queue){
queue.remove();
for(Book b:queue){
Output:
Java Deque Interface is a linear collection that supports element insertion and removal at both
ends. Deque is an acronym for "double ended queue".
boolean It is used to insert the specified element into this deque and return true upon
add(object) success.
boolean
It is used to insert the specified element into this deque.
offer(object)
Object remove() It is used to retrieves and removes the head of this deque.
It is used to retrieves and removes the head of this deque, or returns null if this
Object poll()
deque is empty.
Object element() It is used to retrieves, but does not remove, the head of this deque.
It is used to retrieves, but does not remove, the head of this deque, or returns
Object peek()
null if this deque is empty.
ArrayDeque class
The ArrayDeque class provides the facility of using deque and resizable-array. It inherits
AbstractCollection class and implements the Deque interface.
ArrayDeque Hierarchy
The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the
page.
import java.util.*;
deque.add("Ravi");
deque.add("Vijay");
deque.add("Ajay");
//Traversing elements
System.out.println(str);
Output:
Ravi
Vijay
Ajay
import java.util.*;
deque.offer("arvind");
deque.offer("vimal");
deque.add("mukul");
deque.offerFirst("jai");
for(String s:deque){
System.out.println(s);
//deque.poll();
deque.pollLast();
for(String s:deque){
System.out.println(s);
Output:
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
//Creating Books
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing ArrayDeque
for(Book b:set){
Output:
ArrayList and Vector both implements List interface and maintains insertion order.
But there are many differences between ArrayList and Vector classes that are given below.
ArrayList Vector
import java.util.*;
class TestArrayList21{
al.add("Michael");
al.add("James");
al.add("Andy");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output:
Sonoo
Michael
James
Andy
import java.util.*;
class TestVector1{
v.add("umesh");//method of Collection
v.addElement("irfan");//method of Vector
v.addElement("kumar");
Enumeration e=v.elements();
while(e.hasMoreElements()){
System.out.println(e.nextElement());
Output:
umesh
irfan
kumar
AJAX
AJAX is an acronym for Asynchronous JavaScript and XML. It is a group of inter-related
technologies like JavaScript, DOM, XML, HTML, CSS etc.
AJAX allows you to send and receive data asynchronously without reloading the web page. So it
is fast.
AJAX allows you to send only important information to the server not the entire page. So only
valuable data from the client side is routed to the server side. It makes your application
interactive and faster.
Where it is used?
There are too many web applications running on the web that are using ajax technology like
gmail, facebook,twitter, google map, youtube etc.
Before understanding AJAX, let’s understand classic web application model and ajax web
application model first.
A synchronous request blocks the client until operation completes i.e. browser is not
unresponsive. In such case, javascript engine of the browser is blocked.
As you can see in the above image, full page is refreshed at request time and user is blocked
until request completes.
An asynchronous request doesn’t block the client i.e. browser is responsive. At that time, user
can perform another operations also. In such case, javascript engine of the browser is not
blocked.
As you can see in the above image, full page is not refreshed at request time and user gets
response from the ajax engine.
Let's try to understand asynchronous communication by the image given below.
AJAX Technologies
As describe earlier, ajax is not a technology but group of inter-related technologies. AJAX
technologies includes:
These technologies are used for displaying content and style. It is mainly used for
presentation.
DOM
For carrying data to and from server. JSON (Javascript Object Notation) is like XML but
short and faster than XML.
XMLHttpRequest
For asynchronous communication between client and server. For more visit next page.
JavaScript
XMLHttpRequest
Property Description
Method Description
void open(method, URL, async) same as above but specifies asynchronous or not.
void open(method, URL, async, username, same as above but specifies username and
password) password.
AJAX communicates with the server using XMLHttpRequest object. Let's try to understand
the flow of ajax or how ajax works by the image displayed below.
As you can see in the above example, XMLHttpRequest object plays a important role.
1. User sends a request from the UI and a javascript call goes to XMLHttpRequest
object.
2. HTTP Request is sent to the server by XMLHttpRequest object.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
6. HTML and CSS data is displayed on the browser.
1. SOAP
2. WSDL
3. UDDI
SOAP
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing
web services.
SOAP is a W3C recommendation for communication between two applications.
Language and Platform independent: SOAP web services can be written in any
programming language and executed in any platform.
Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that
must be followed while developing the SOAP applications. So it is slow and consumes more
bandwidth and resource.
WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to discover the
service.
WSDL
WSDL is a xml document containing information about web services such as method name,
method parameter and how to access it.
Features of WSDL
Definition : It is the root element of all WSDL documents. It defines the name of the
web service, declares multiple namespaces used throughout the remainder of the
document, and contains all the service elements described here.
Data types : The data types to be used in the messages are in the form of XML
schemas.
Message : It is an abstract definition of the data, in the form of a message presented
either as an entire document or as arguments to be mapped to a method invocation.
Operation : It is the abstract definition of the operation for a message, such as
naming a method, message queue, or business process, that will accept and process
the message.
Port type : It is an abstract set of operations mapped to one or more end-points,
defining the collection of operations for a binding; the collection of operations, as it is
abstract, can be mapped to multiple transports through various bindings.
Binding : It is the concrete protocol and data formats for the operations and messages
defined for a particular port type.
Port : It is a combination of a binding and a network address, providing the target
address of the service communication.
Service : It is a collection of related end-points encompassing the service definitions
in the file; the services map the binding to the port and include any extensibility
definitions.
In addition to these major elements, the WSDL specification also defines the following utility
elements:
UDDI
A registry of all web service's metadata, including a pointer to the WSDL description
of a service.
A set of WSDL port type definitions for manipulating and searching that registry.
A business or a company can register three types of information into a UDDI registry. This
information is contained in three elements of UDDI.
White Pages,
Yellow Pages, and
Green Pages.
White Pages
Yellow Pages
Yellow pages contain more details about the company. They include descriptions of
the kind of electronic capabilities the company can offer to anyone who wants to do
business with it.
Yellow pages uses commonly accepted industrial categorization schemes, industry
codes, product codes, business identification codes and the like to make it easier for
companies to search through the listings and find exactly what they want.
Green Pages
Green pages contains technical information about a web service. A green page allows
someone to bind to a Web service after it's been found. It includes:
NOTE : UDDI is not restricted to describing web services based on SOAP. Rather, UDDI
can be used to describe any service, from a single webpage or email address all the way up to
SOAP, CORBA, and Java RMI services.
UDDI Data Model is an XML Schema for describing businesses and web services. The data
model is described in detail in the "UDDI Data Model" chapter.
These are operator sites that provide implementations of the UDDI specification and
synchronize all data on a scheduled basis.
The UDDI Business Registry (UBR), also known as the Public Cloud, is a conceptually
single system built from multiple nodes having their data synchronized through replication.
The current cloud services provide a logically centralized, but physically distributed,
directory. It means the data submitted to one root node will automatically be replicated across
all the other root nodes. Currently, data replication occurs every 24 hours.
UDDI cloud services are currently provided by Microsoft and IBM. Ariba had originally
planned to offer an operator as well, but has since backed away from the commitment.
Additional operators from other companies, including Hewlett-Packard, are planned for the
near future.
It is also possible to set up private UDDI registries. For example, a large company may set up
its own private UDDI registry for registering all internal web services. As these registries are
not automatically synchronized with the root UDDI nodes, they are not considered as a part
of the UDDI cloud.
The UDDI data model defines a generic structure for storing information about a business
and the web services it publishes. The UDDI data model is completely extensible, including
several repeating sequence structures of information.
However, WSDL is used to describe the interface of a web service. WSDL is fairly
straightforward to use with UDDI.
The Hertz reservation system web service provides a concrete example of how UDDI and
WSDL works together. Here is the <tModel> for this web service: