AJAX USING JSP

Good news for AJAX and jsp developers. Because you may find difficult to use AJAX in jsp page. your problem get solved now. Here I have given a script how to use AJAX in jsp.

AJAX is not a new programming language, but a technique for creating better, faster, and more interactive web applications.
With AJAX, your JavaScript can communicate directly with the server, using the JavaScript XMLHttpRequest object. With this object, your JavaScript can trade data with a web server, without reloading the page.

Using AJAX in php or asp it is easy. Using AJAX with jsp is little difficult. Using this below script you can display dynamic results effectively and without reloading.

function AJAXFunction() {

var xmlHttp;

try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();

} catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);

} catch (e) {

try {
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

} catch (e) {

alert(“Your browser does not support AJAX!”);
return false;
}
}
}

xmlHttp.onreadystatechange=function() {

if(xmlHttp.readyState==4) {

// Get the data from the server’s response.
} }

xmlHttp.open(“POST”,ajax.jsp,true);
xmlHttp.send(null);
}

With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object.

Example explained:
First create a variable xmlHttp to hold the XMLHttpRequest object. Then try to create the object with XMLHttp=new XMLHttpRequest(). This is for the Firefox, Opera, and Safari browsers. If that fails, try xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”) which is for Internet Explorer 6.0+, if this also fails, try xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”) which is for Internet Explorer 5.5+

If none of the three methods work, the user has a very outdated browser, and he or she will get an alert stating that the browser doesn’t support AJAX.

After a request to the server, we need a function that can receive the data that is returned by the server.

The onreadystatechange property stores your function that will process the response from a server. This is not a method, the function is stored in the property to be called automatically. The following code sets the onreadystatechange property and stores an empty function inside it:

To send off a request to the server, we use the open() method and the send() method.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server.

The Advantages of AJAX over Classical Web Based Application are:

Asynchronous – AJAX allows for the ability to make asynchronous calls to a web server. This
allows the client browser to avoid waiting for all data to arrive before allowing the user to act
once more.

Minimal data transfer – By not performing a full postback and sending all form data to the
server, the network utilization is minimized and quicker operations occur. In sites and locations
with restricted pipes for data transfer, this can greatly improve network performance.

Limited processing on the server – With the fact that only the necessary data is sent to the
server, the server is not required to process all form elements. By sending only the necessary
data, there is limited processing on the server. There is no need to process all form elements,
process the viewstate, send images back to the client, and no need to send a full page back to
the client.

The Disadvantages of AJAX over Classical Web Based Applications are:

1. AJAX is not well integrated with any browsers; so again, users may experience unexplained effects, such as failure of the back button, incapacity to bookmark pages.
2. Search engines may not see the content, which can have severe effects on a company’s page ranking.

2. Accessibility requirements may be contravened, so a site may have to provide a non-AJAX version for users of non-compliant browsers.

VN:F [1.1.4_465]
Rating: 3.0/5 (1 vote cast)

Comments on: "AJAX USING JSP" (2)

  1. Hi all,

    I am working for a software integrator company. My projects includes working on Java and Ruby on Rails and Ajax. I think Web Services is really cool. We also recently have to now work on REST and they are talking about mashups and Struts. Can anyone tell me if there are some good training or conferences so that me and my team members can get to speed with these technologies. Learning from books is not my cup of tea, even not when I was doing engineering ;)

    All the help that group members can provide in this regard is much appreciated.

    Thanks,
    Vaibhavi

    VA:F [1.1.4_465]
    Rating: 0.0/5 (0 votes cast)

  2. Hi Vaibhavi,

    There are several online resources available that you just google for. If any of your team like to read then quality books from wiley and oreilly cover such technologies in detail.

    I also highly recommend you could attend the upcoming Great Indian Developer Summit (http://www.developersummit.com) that is covering Java, Agile, REST, JAX-RS, mashups, .NET, Rich Web, JPA, SOA, rich user experiences, Spring, Groovy and more. They have most of the creators of these technologies as speakers. My team is attending this summit 22-25 apr at IISc campus where we are attending the web conference on April 23 and java on April 24. We have been able to get very good discounts. Maybe all those who are interested from your group can sign up together and get a good bargain from them. what say? I also attended last year’s conference and had a really cool time.

    In Hyderabad there is Sun Tech Days with some sun speakers.

    Thanks,
    Anaz

    VA:F [1.1.4_465]
    Rating: 0.0/5 (0 votes cast)

Leave a comment for: "AJAX USING JSP"

Tag Cloud