Note that there are some explanatory texts on larger screens.

plurals
  1. POapplication/xml versus text/xml content types
    primarykey
    data
    text
    <p>I am trying to get an XML response from a servlet. The servlet returns a content type of "application/xml". Using XmlHttpRequest, I can get responseText, but not responseXml. I was wondering if this had something to do with the content type or the request type (I'm doing a GET)...?</p> <p>Many thanks!</p> <p>I've pared down all my files. I <em>think</em> I'm setting that all correctly. Here's what I have:</p> <p>------- HTML ------</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script src="js/jboard_simple.js" type="text/javascript"&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="myDiv"&gt; &lt;h2&gt;No results yet....&lt;/h2&gt; &lt;/div&gt; &lt;form name="searchForm" id="searchForm_id"&gt; &lt;input type="text" name="searchString" id="searchString_id" /&gt; &lt;button type="button" onclick="loadXMLDoc()"&gt;Perform Search&lt;/button&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>------- JavaScript -----------</p> <pre><code>function loadXMLDoc() { document.getElementById("myDiv").innerHTML = "searching..."; var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) { // Do something here... alert(xmlhttp.responseText); alert(xmlhttp.responseXml); processSearchServletResponse(xmlhttp.responseText); } } // Find teh search string var searchString_el = window.document.getElementById('searchString_id'); var searchString = searchString_el.value; alert('searchString: ' + searchString); var searchUrl = "/SimpleServlet?searchString=" + searchString; xmlhttp.open("GET", searchUrl, true); xmlhttp.send(); } function processSearchServletResponse(xmlTxt) { document.getElementById("myDiv").innerHTML = xmlTxt; } </code></pre> <p>------- Servlet --------</p> <pre><code>import java.io.BufferedOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.apache.log4j.LogManager; public class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static Logger logger = LogManager.getRootLogger(); public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { BufferedOutputStream bs = null; String simpleResponse = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;root&gt;hi&lt;/root&gt;"; try { res.setContentType("text/xml"); res.setCharacterEncoding("UTF-8"); bs = new BufferedOutputStream(res.getOutputStream()); bs.write(simpleResponse.getBytes()); } catch (Exception ex) { logger.error("JboardSearchServlet.service(): error = ", ex); } finally { bs.flush(); bs.close(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload