0

I am learning both AJAX and the Java Servlet API (well, Spring MVC, which is based upon Servlets) at the same time, and believe I am understanding most of the basics, except when it comes to understanding how HttpServletResponse is structured/organized/populated differently when the server/Servlet is responding to an HTTP GET/POST (as it would with a normal page request) as opposed to an AJAX-based XmlHttpRequest.

It seems to me that, in the absence of AJAX, every HttpServletResponse would just contain the full HTML (plus header/metadat/etc. info) for the page. With AJAX, asynchronous XmlHttpRequests can be used to update specific components inside a particular page. Thus if I understand HTTP and Servlets correctly, a request for http://www.example.com/some-page.html might result with an HttpServletResponse containing the following body:

<html>
    <header><title>Title of the page</title></header>
    <body>
        <!-- Some massive amount of HTML -->

        <a href="./foo.html">This is a link</a>

        <!-- Lots more HTML -->
    </body>
</html>

Whereas, with an AJAX request, somehow the HttpServletRequest might send back information so that the link (from the example above) now renders to this:

<a href="./bar.html">This is a new link that point to bar</a>

My question is: How do Java Servlets structure HttpServletRequests to handle both full page requests as well as AJAX requests that may only produce changes to parts of a page?

As a segue into a similar-yet-separate question is how clients (browsers) know to take the HTTP Responses (sent back by the Servlet) and either render a whole new page or just update a small part of a page.

Thanks in advance for any clarity on the matter.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

3 Answers3

1

I am understanding most of the basics, except when it comes to understanding how HttpServletResponse is structured/organized/populated differently when the server/Servlet is responding to an HTTP GET/POST (as it would with a normal page request) as opposed to an AJAX-based XmlHttpRequest.

I think your confusion is rooted in thinking about AJAX requests and non-AJAX requests as being fundamentally different. First of all, it is important to know what AJAX is. From wikipedia:

Despite the name, the use of XML is not needed (JSON is often used instead), and the requests do not need to be asynchronous.

I would also add that some other browser scripting language besides JavaScript could be used instead. So the name tells you absolutely nothing. "AJAX" basically just means that an HTTP request is being kicked off from a script in the browser as opposed to a request that originates from someone typing a URL in the address bar for example.

So the scripting code that generated the HTTP request may choose to handle any HTTP response that the server sends or it can ignore it. How the request was generated -- "AJAX" or non-"AJAX", need not have any bearing on the HTTP response that is returned.

Dave L.
  • 9,595
  • 7
  • 43
  • 69
0

Ajax requests are (essentially) no different on the server side: what is returned is determined by the URL, parameters, content type, etc. Any combination of those can be used to determine what data is returned.

Ajax requests from the client are (usually) created specifically by the web app developer. As such, the response handler will know how to deal with the data that is returned, whether it's HTML (as you show above), JSON, XML, etc.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

Well, I think that what you need is not how the servlet responses you, but how you handle its response in the callback of the ajax request, and the simplest way is to process it, via JSON, XML or something as simple as a String with the link your servlet gives you...

In case you have defined an id tag for your <a>, something like

 <html>
     <body>
          <a href="" id="myLink">click here!</a>
     </body>
 </html>

then on your callback function (the onreadystatechange function of the XMLHTTPRequest object) you should do something like this:

retreq.onreadystatechange=function ()
    if (retreq.readyState==4){
        if(retreq.status==200){
             if(document.getElementById(idelemento)!=null){
                  document.getElementById("myLink").href=retreq.responseText;
             }
        }
    }   
}
albervera
  • 45
  • 7