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.
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:
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.