title: AJAX with Java: How is HttpServletResponse Affected?
body: <p>I am learning both AJAX and the Java Servlet API (well, Spring MVC, which is <em>based</em> upon Servlets) at the same time, and believe I am understanding most of the basics, except when it comes to understanding how <code>HttpServletResponse</code> is structured/organized/populated differently when the server/Servlet is responding to an <code>HTTP GET/POST</code> (as it would with a normal page request) as opposed to an AJAX-based <code>XmlHttpRequest</code>.</p>
  
  <p>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 <code>XmlHttpRequests</code> can be used to update specific <strong>components</strong> inside a particular page. Thus if I understand HTTP and Servlets correctly, a request for <code>http://www.example.com/some-page.html</code> might result with an HttpServletResponse containing the following body:</p>
  
  <pre><code>&lt;html&gt;
      &lt;header&gt;&lt;title&gt;Title of the page&lt;/title&gt;&lt;/header&gt;
      &lt;body&gt;
          &lt;!-- Some massive amount of HTML --&gt;
  
          &lt;a href="./foo.html"&gt;This is a link&lt;/a&gt;
  
          &lt;!-- Lots more HTML --&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Whereas, with an AJAX request, somehow the HttpServletRequest might send back information so that the link (from the example above) now renders to this:</p>
  
  <pre><code>&lt;a href="./bar.html"&gt;This is a new link that point to bar&lt;/a&gt;
  </code></pre>
  
  <p>My question is: <strong>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?</strong></p>
  
  <p>As a segue into a similar-yet-separate question is how clients (browsers) know to take the <code>HTTP Responses</code> (sent back by the Servlet) and either render a whole new page or just update a small part of a page.</p>
  
  <p>Thanks in advance for any clarity on the matter.</p>