Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6143817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:34:13+00:00 2026-05-23T18:34:13+00:00

I am trying to get an XML response from a servlet. The servlet returns

  • 0

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)…?

Many thanks!

I’ve pared down all my files. I think I’m setting that all correctly. Here’s what I have:

——- HTML ——

<html>
<head>
<title></title>
<script src="js/jboard_simple.js" type="text/javascript"> </script>
</head>
<body>

    <div id="myDiv">
        <h2>No results yet....</h2>
    </div>

    <form name="searchForm" id="searchForm_id">
        <input type="text" name="searchString" id="searchString_id" />
        <button type="button" onclick="loadXMLDoc()">Perform Search</button>
    </form>

</body>
</html>

——- JavaScript ———–

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 && 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;    
}

——- Servlet ——–

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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>hi</root>";


        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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:34:13+00:00Added an answer on May 23, 2026 at 6:34 pm

    Oh dear! I just figured out my whole problem (after someone else looked at it).

    I was using ‘responseXml’ rather than ‘responseXML’. Darn those upper case letters =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do a simple application using Ajax and servlet. But when
I'm trying to get a response from a ASP.NET webservice without using the get
I'm trying to parse the xml document returned from this link but I get
I am trying to get some data from a GoogleCheckout response. I am using
I am trying to get some XML data with LINQ, but running into a
I'm trying to get the contents of a XML document element, but the element
Hi I am trying to get my mind around XSDs, XML and namespaces but
I'm trying to store an xml serialized object in a cookie, but i get
I am trying to load a very basic XML document but everytime I get
I'm trying get values from a GridView using the following code: foreach (GridViewRow row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.