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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:42:43+00:00 2026-05-29T20:42:43+00:00

I’m working on a setup that takes an http request (GET for testing purposes)

  • 0

I’m working on a setup that takes an http request (GET for testing purposes) to java servlet. How it works is, the ser let takes a request from the browser, parses it and sends it via TCP socket to the ‘main’ server, which processes the request and sends a response back. The servlet then pulls the HttpServletResponse that was previously stored in an ConcurrentHashMap, opens up the PrintWriter, and sends a response back. Everything’s going smoothly, except that the HttpServletResponse doesn’t always send back the information written to the PrintWriter. The browser receives an “OK” response every time, but often the response doesn’t contain any of the information I try to write.

Below I have the code for the initial doGet that hands off the HttpServletResponse instance,then the method that writes to the Response buffer. Included after that are the responses as the browser receives them. After that, some observations on how to reliably get the expected result, in case that helps pin down the problem.

Note that the only variable seems to be whether or not the Response gets written; I’ve poured over output logs, and can’t find any other differences between the times where the Response is written as expected, and the times it isn’t. The HTTPServletResponseListener I wrote receives the response every time.

[using Glassfish 3.1.1]

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String js = request.getParameter("json");
// Omitting try-catch for space
Message msg = this.parser.parseToMessage(js);
this.sc.send(msg, new HTTPServletResponseListener(response));
}

And the HTTPServletResponseListener method that is called on response (other than the constructor which only assigns the local HttpServletResponse to a local field, this is the only method)

public void handleResponse(ResponseMessage response) {
  DataParser parser = new JSONParser();
  String temp = parser.parseToString(response);
  httpResponse.setContentType("application/json");
  httpResponse.addHeader("Hmm","yup");
  try {
     PrintWriter out = httpResponse.getWriter();
     out.println(temp);
     }  catch (IOException ex) {
        Logger.getLogger(HTTPServletReponseListener.class.getName()).log(Level.SEVERE,null,ex);         
     }
}

Responses and the browser receives them:

When it works as intended:

When the response is empty:

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Sun         Microsystems Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1.1
Content-Length: 0
Date: Thu, 16 Feb 2012 15:26:35 GMT

When the response is as intended:

HTTP/1.1 200 OK
Hmm: yup
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Sun  Microsystems Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1.1
Content-Type: application/json;charset=ISO-8859-1
Content-Length: 126
Date: Thu, 16 Feb 2012 15:27:30 GMT

Observations:

i can get a response 95% of the time by following these steps…. otherwise, its around 50% success rate.

hit refresh on browser… the first couple requests after deploying tend to work more often
wait at least 5 seconds before hitting refresh again (sending another test request)
this tends to work reliably. If it fails, you have to wait 15 seconds, and then it tends to work again.

if the HttpServletResponse is written BEFORE waiting for the response, it works every time.

Thank you so much for taking the time to read this. I’ve read other Stackoverflow questions, but nothing seems to touch on this particular problem, unless I’m missing a connection.

  • 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-29T20:42:44+00:00Added an answer on May 29, 2026 at 8:42 pm

    This is the most interesting line:

    this.sc.send(msg, new HTTPServletResponseListener(response));
    

    I suspect the sc is this external TCP server you are calling and you are also passing a listener to be notified when the response arrives. Now the important assumption: am I right that the TCP server sends response asynchronously, notifying your listener in a different thread?

    If this is the case, it explains your behaviour. In pre-3.0 servlets you had to handle the whole request within doGet. Once your code leaves doGet(), servlet container assumes the whole request has been handled and discards the request.

    You have introduced a race condition: doGet() returns without writing anything to the output stream. It takes few milliseconds for the container to handle the response and send it back. If during this short period of time your external TCP server returns data and notifies listener, the data will go through. But if the server is a bit slower, you are sending response to a connection that has already been handled.

    Think about it this way: browser makes a call, doGet() is called which in turns calls backend server. The doGet() returns and servlet container assumes you are done. It sends back (empty) response and forgets about this request. Milliseconds or even seconds later response comes back from the back-end server. But the connection is gone, it has already been sent, sockets were closed, browser rendered the response. You are not telling your container: hey, wait, I haven’t finished with that response!.

    Solutions

    From worst to best:

    1. Actively wait/poll for response in doGet().

    2. Make your external TCP server call blocking. Change the TCP server facade so that it returns ResponseMessage and move listener code to doGet(). Example:

       ResponseMessage responseMsg = this.sc.send(msg);
       DataParser parser = new JSONParser();
       String temp = parser.parseToString(responseMsg);
       httpResponse.setContentType("application/json");
       httpResponse.addHeader("Hmm","yup");
       PrintWriter out = httpResponse.getWriter();
       out.println(temp);
      
    3. Use Servlet 3.0 asynchronous support, it is a much better choice in your use case and the scope of changes will be very limited.

      In doGet():

      final AsyncContext asyncContext = request.startAsync(request, response);
      

      and in the HTTPServletResponseListener once you’re done:

      asyncContext.complete();
      

      The extra call to startAsync() tells the container: even though I have returned from doGet(), I haven’t finished with this request. Please hold. I wrote an article about Servlet 3.0 some time ago.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.