Search This Blog

Friday, December 12, 2008

AJAX-Introduction

Ajax Documentation:

Ajax definition:

  1. AJAX stands for Asynchronous Java script and XML.
  2. It’s a web development technique for creating interactive web-based application that can asynchronously interchange data between client and Server.

Definition 2:

AJAX is a technique rather than a technology: It describes how JavaScript can be used to pull data from the server using the XML HTTP Request object and then insert this data into the website using DOM. This is done asynchronously - that is, in the background, without having to refresh the whole page.

DOM (Document object Model):

  1. DOM is an internal representation of the website.
  2. DOM is accessible by the java script and provide the way to programmatically insert, remove, modify in a n website..

Example program with DOM:

1. Below example show how to use DOM tag in java script.

Div tag also a DOM (Document object model.):

Code: dom.html

 

Replace Text

 
  Hello, world!

The above example shows how to set the content of the

element with the id "foo" to "Hello, AJAX world!”

Output:

Initial:

Replace Text

Hello,world!

After Clicked Replace Text:

Output:

Replace Text

Hello, AJAX world!

Example 2:

1. Check the username is available in database? If the username available print “username available “message. If not available (ie. same user name already exist in db) print message “username not available”.

1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

2. pageEncoding="ISO-8859-1"%>

3.

4.

5.

6.

29.

30. Insert title here

31.

32.

33.

Please choose your username:

34.

35.

36.

37.

38.

39.

40.

Description:

Line 7: var http=false; > Initializing the http object reference variable that will hold the browser object .

Line 8 to 12: check browser type. If the browse type is Microsoft internet explorer we have to use the ActiveXObject .

http = new ActiveXObject("Microsoft.XMLHTTP");

Other wise we should use

http = new XMLHttpRequest();

Line 13 to 22: This JavaScript function is responsible for calling the another JSP file and validate the username is available in db.

Line 17: In this line we have to mention the action type either POST or GET and URL.

http.open("GET", "validate.jsp?name=" + user, true);

Line 18: http.onreadystatechange method we should check JSP (process complete or what) ready for response.

if(http.readyState == 4) {

document.getElementById('foo').innerHTML = http.responseText;

}

Different ready states are from 1 to 4 ,Ready state 4 is intimating all the process has completed is server .So we can display result for the corresponding DOM.

Line 21: document.getElementById('foo').innerHTML = http.responseText;

Setting up the response into the foo div tag.

Validate.jsp

It’s a normal jsp page:

<%@ page import="java.util.*" %>

<%

String user="";

user=request.getParameter("name");

System.out.println("user="+new Date()+user);


if (user.equals("test"))

{

out.println("name is not available");

}

else

{

out.println("name is available");

}

%>

What ever you will be printing in out.println all the content will get response in the client side ( http.responseText) .

No comments:

Post a Comment