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 6929253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:17:37+00:00 2026-05-27T11:17:37+00:00

An intranet site has a search form which uses AJAX to call a servlet

  • 0

An intranet site has a search form which uses AJAX to call a servlet on a different domain for search suggestions.

This works in Internet Explorer with the intranet domain being a “trusted site” and with cross-domain requests enabled for trusted sites, but doesn’t work in Firefox.

I have tried to work around the problem by creating a servlet on the intranet server, so there’s a JS call to my servlet on the same domain, then my servlet calls the suggestions servlet on the other domain. The cross-domain call is server-side, so it should work regardless of browser settings.

The AJAX call and my servlet’s call to the other servlet both use a HTTP POST request with arguments in the URL and empty request-content.

The reason I’m sticking with POST requests is that the JS code is all in files on the search server, which I can’t modify, and that code uses POST requests.

I’ve tried calling the customer’s existing suggestions servlet with a GET request, and it produces a 404 error.

The problem is that the result is inconsistent.

I’ve used System.out.println calls to show the full URL and size of the result on the server log.

The output first seemed to change depending on the calling browser and/or website, but now seems to change even between sessions of the same browser.

E.g. entering “g” in the search box, I got this output from the first few tries on the Development environment using Firefox:

Search suggestion URL: http://searchdev.companyname.com.au/suggest?q=g&max=10&site=All&client=ie&access=p&format=rich
Search suggestion result length: 64

Initial tries with Firefox on the Test environment (different intranet server but same search server) produced a result length of 0 for the same search URL.
Initial tries with Internet Explorer produced a result length of 0 in both environments.

Then I tried searching for different letters, and found that “t” produced a result in IE when “g” hadn’t.

After closing the browsers and leaving it for a while, I tried again and got different results.

E.g. Using Firefox and trying “g” in the Development environment now produces no result when it was previously producing one.

The inconsistency makes me think something is wrong with my servlet code, which is shown below. What could be causing the problem?

I think the search suggestions are being provided by a Google Search Appliance, and the JS files on the search server all seem to have come from Google.

The actual AJAX call is this line in one file:

XH_XmlHttpPOST(xmlhttp, url, '', handler);

The XH_XmlHttpPOST function is as follows in another file:

function XH_XmlHttpPOST(xmlHttp, url, data, handler) {
  xmlHttp.open("POST", url, true);
  xmlHttp.onreadystatechange = handler;
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-Length",
      /** @type {string} */ (data.length));
  XH_XmlHttpSend(xmlHttp, data);
}

Here is my servlet code:

package com.companyname.theme;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class suggest extends HttpServlet {
    Properties props=null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String result = "";
        String args = req.getQueryString();
        String baseURL = props.getProperty("searchFormBaseURL");
        String urlStr = baseURL + "/suggest?" + args;
        System.out.println("Search suggestion URL: " + urlStr);

        try {
            int avail, rCount;
            int totalCount = 0;
            byte[] ba = null;
            byte[] bCopy;
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write("".getBytes());
            os.close();
            InputStream is = conn.getInputStream();
            while ((avail = is.available()) > 0) {
                if (ba == null) ba = new byte[avail];
                else if (totalCount + avail > ba.length) {
                    // Resize ba if there's more data available.
                    bCopy = new byte[totalCount + avail];
                    System.arraycopy(ba, 0, bCopy, 0, totalCount);
                    ba = bCopy;
                    bCopy = null;
                }
                rCount = is.read(ba, totalCount, avail);
                if (rCount < 0) break;
                totalCount += rCount;
            }
            is.close();
            conn.disconnect();
            result = (ba == null ? "" : new String(ba));
            System.out.println("Search suggestion result length: " + Integer.toString(result.length()));
        } catch(MalformedURLException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = resp.getWriter();
        pw.print(result);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        InputStream stream = this.getClass().getResourceAsStream("/WEB-INF/lib/endeavour.properties");
        props = new Properties();
        try {
            props.load(stream);
            stream.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    }
  • 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-27T11:17:38+00:00Added an answer on May 27, 2026 at 11:17 am

    Solution: don’t rely on InputStream.available().

    The JavaDoc for that method says it always returns 0.
    HttpURLConnection.getInputStream() actually returns a HttpInputStream, in which available() seems to work but apparently sometimes returns 0 when there is more data.

    I changed my read loop to not use available() at all, and now it consistently returns the expected results.
    The working servlet is below.

    package com.integral.ie.theme;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Properties;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class suggest extends HttpServlet implements
    javax.servlet.Servlet {
        Properties props=null;
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //super.doPost(req, resp);
            final int maxRead=200;
    
            String result="";
            String args=req.getQueryString();
            String baseURL=props.getProperty("searchFormBaseURL");
            String urlStr=baseURL+"/suggest?"+args;
            //System.out.println("Search suggestion URL: "+urlStr);
            try {
                int rCount=0;
                int totalCount=0;
                int baLen=maxRead;
                byte[] ba=null;
                byte[] bCopy;
                URL url=new URL(urlStr);
                HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                conn.setRequestMethod("POST");
                // Setting these properties may be unnecessary - just did it
                // because the GSA javascript does it.
                conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length","0");
    
                InputStream is=conn.getInputStream();
                ba=new byte[baLen];
                while (rCount>=0) {
                    try {
                        rCount=is.read(ba,totalCount,baLen-totalCount);
                        if (rCount>0) {
                            totalCount+=rCount;
                            if (totalCount>=baLen) {
                                baLen+=maxRead;
                                bCopy=new byte[baLen];
                                System.arraycopy(ba,0,bCopy,0,totalCount);
                                ba=bCopy;
                                bCopy=null;
                            }
                        }
                    } catch(IOException e) {
                        // IOException while reading - allow the method to return
                        // anything we've read so far.
                    }
                }
    
                is.close();
                conn.disconnect();
                result=(totalCount==0?"":new String(ba,0,totalCount));
    
                //System.out.println("Search suggestion result length: "
                //+Integer.toString(result.length()));
    
            } catch(MalformedURLException e) {
                e.printStackTrace();
            } catch(IOException e) {
                e.printStackTrace();
            }
            PrintWriter pw=resp.getWriter();
            pw.print(result);
        }
    
        @Override
        public void init() throws ServletException {
            super.init();
            InputStream stream=this.getClass().getResourceAsStream("/WEB-INF/lib/endeavour.properties");
            props=new Properties();
            try {
                props.load(stream);
                stream.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've set up an intranet site that uses AD to determine premissions. Works great
My site is an Intranet and has hundreds of hits by day. The issue
I'm trying to create a simple report on an intranet site. Based on this
I have an ASP.NET 3.5 intranet website which has a default page with a
So I've logged onto a client's site this morning (it's an intranet that monitors
My ASP.NET MVC intranet app has a data repository that uses current user's Windows
We have an intranet page which has been around for several years and it
Our intranet site has an unusual set of requirements. It functions like a multi-page
I have an intranet site that loads in IE7 compatibility mode, unless the user
I'm developing an intranet site project ( ASP.NET MVC project). How do I associate

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.