- JavaScript
- XML
- HTML
- CSS
The web standards used in
AJAX Uses HTTP Requests
In traditional JavaScript coding, if you want to get any information from a database or a file on the server, or send user information to a server, you will have to make an HTML form and GET or POST data to the server. The user will have to click the "Submit" button to send/get the information, wait for the server to respond, and then a new page will load with the results.
Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly.
With
With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background.
AJAX - Browser Support
The keystone of
The code above is compatible with all the popular browsers: Internet Explorer, Opera, Firefox, and Safari.
Sample
The entire source code can be downloaded my website.
div id="t2"
The above line shows the HTML for the “Get Date” button. On clicking this button the “getTime” method gets called as is evident from the “onClick” attribute. Below is the “getTime” method that actually makes the
// Function For The Second Part Of The Document i.e Get Time
function getTime()
{
xmlHttp=getXMLHttp();
if(xmlHttp==null)
{
alert('Your Browser Does Not Support AJAX!');
}
else
{
xmlHttp.onreadystatechange=functionTime;
xmlHttp.open("GET","/AJAX/GetDate",true);
xmlHttp.send(null);
}
}
function functionTime()
{
if(xmlHttp.readyState==4)
t2.innerHTML=xmlHttp.responseText;
}
The XMLHttpObject hits the GetDate Servlet and when the response is ready the functionTime gets executed, which displays the value on the page. Below is the functional part of the GetDate Servlet.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("
Date From Server Is: " + new java.util.Date() + "
");out.close();
}
As is evident the Servlet is just pushing the current date into the output Stream which gets displayed on the page.
No comments:
Post a Comment