using XMLHttpRequest to query a sevlet

T

Tim B

I've posted this to a Java group and a Javascript group because it involves
both languages.

I want to update a select list dynamically when the value of another select
changes. The XMLHttpRequest would be send to a servlet. I have done this
previously using the responseText, parsing out a pre-encoded string that was
returned from the servlet.

I would like to try it instead using the responseXML. An example of how to
get the information I need out of the responseXML would be very helpful.
Also, what would I need to send from the servlet? Would it have to be XML,
or could I write an array of objects to the ServletOutputStream that would
be marshalled into XML and be available in the responseXML with (preferably
built in) javascript functionality?

Examples or links would be great, but I don't have time to wade through an
Ajax tutorial.

Tim Blanchard
 
M

Martin Honnen

Tim said:
I would like to try it instead using the responseXML. An example of how to
get the information I need out of the responseXML would be very helpful.
Also, what would I need to send from the servlet? Would it have to be XML,
or could I write an array of objects to the ServletOutputStream that would
be marshalled into XML and be available in the responseXML with (preferably
built in) javascript functionality?

If you want to use responseXML then your servlet needs to send a
well-formed XML document with a proper HTTP Content-Type response header
e.g.
Content-Type: application/xml
Then that response body with the XML document is parsed by the browser
into a W3C DOM document made available as the responseXML property of
the XMLHttpRequest instance.
As for getting the information out of responseXML, as it is a W3C DOM
document you can use the W3C DOM APIs <URL:http://www.w3.org/DOM/DOMTR>
e.g. properties like documentElement, firstChild, lastChild,
nextSibling, previousSibling, childNodes, nodeType, methods like
getElementsByTagName.
Mozilla and Opera provide a W3C DOM Level 2 Core namespace aware
implementation and a W3C DOM Level 3 XPath implementation.
IE (6 and 7) use MSXML 3 for parsing which is W3C DOM Level 1 XML with
proprietary extensions like createNode for namespace aware node creation
and selectSingleNode/selectNodes with XPath nodes access for instance.
Mozilla and Opera also provide this API
<URL:http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations>
for scripting XSLT transformation, to transform the XML into HTML nodes
to be inserted into your HTML document.
MSXML can also do XSLT but can't create HTML nodes as the result of the
transformation, you would need to create a string snippet with a HTML
fragment and parse that with e.g. innerHTML or insertAdjacentHTML.

As for writing an array of objects, for that you can't use XML but you
can use JSON as the responseText instead: <URL:http://www.json.org/>,
that way you are dealing directly with JavaScript objects composed of
string and/or number and/or boolean and/or array and/or object values.

Using JSON might be easier as using XML as the above description makes
it clear that the DOM support the various browsers provide differ widely
while JSON is a subset of JavaScript/ECMAScript fully implemented by the
various script engines the browsers use.
 
T

Tim B

Martin Honnen said:
If you want to use responseXML then your servlet needs to send a
well-formed XML document with a proper HTTP Content-Type response header
e.g.
Content-Type: application/xml
Then that response body with the XML document is parsed by the browser
into a W3C DOM document made available as the responseXML property of
the XMLHttpRequest instance.
As for getting the information out of responseXML, as it is a W3C DOM
document you can use the W3C DOM APIs <URL:http://www.w3.org/DOM/DOMTR>
e.g. properties like documentElement, firstChild, lastChild,
nextSibling, previousSibling, childNodes, nodeType, methods like
getElementsByTagName.
Mozilla and Opera provide a W3C DOM Level 2 Core namespace aware
implementation and a W3C DOM Level 3 XPath implementation.
IE (6 and 7) use MSXML 3 for parsing which is W3C DOM Level 1 XML with
proprietary extensions like createNode for namespace aware node creation
and selectSingleNode/selectNodes with XPath nodes access for instance.
Mozilla and Opera also provide this API
<URL:http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_inter
face_to_XSL_Transformations>
for scripting XSLT transformation, to transform the XML into HTML nodes
to be inserted into your HTML document.
MSXML can also do XSLT but can't create HTML nodes as the result of the
transformation, you would need to create a string snippet with a HTML
fragment and parse that with e.g. innerHTML or insertAdjacentHTML.

As for writing an array of objects, for that you can't use XML but you
can use JSON as the responseText instead: <URL:http://www.json.org/>,
that way you are dealing directly with JavaScript objects composed of
string and/or number and/or boolean and/or array and/or object values.

Using JSON might be easier as using XML as the above description makes
it clear that the DOM support the various browsers provide differ widely
while JSON is a subset of JavaScript/ECMAScript fully implemented by the
various script engines the browsers use.


Thanks for the informative reply, Martin. I'll look into JSON first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top