This discussion is archived
8 Replies Latest reply: Jul 31, 2012 12:27 AM by gimbal2 RSS

Put on idle HttpServletResponse

952661 Newbie
Currently Being Moderated
Hi all,

I am newbie in using HttpServlet methods. What I want to do is to delay an HttpServletResponse. I set up some response.setHeader but then I want to wait a while after setting other values as the length of data to send and the content itself.

I tried to do that in 2 ways:

1. by using Thread.sleep() - in this case even with sleep of 100 ms the response seems to be lost.

2. by response.wait(TIME) - in this case it will never reach the block after wait, whatever TIME value I use.

Do you have any suggestions on what can be wrong ?

Thanks,
robert
  • 1. Re: Put on idle HttpServletResponse
    EJP Guru
    Currently Being Moderated
    What I want to do is to delay an HttpServletResponse.
    Why?
    I set up some response.setHeader but then I want to wait a while after setting other values as the length of data to send
    You don't have to set th Content-Length header at alll. The container does it for you. You could have caused a problem by doing that.
    and the content itself.
    Just write it when you're ready.
  • 2. Re: Put on idle HttpServletResponse
    952661 Newbie
    Currently Being Moderated
    1. Basically what I want is my client will initiate a doGet request but at that time, the answer is not ready yet. So it should wait and check(in a while loop ?) the connection till a not NULL answer arrives.

    2. You are right, I can just set the answer whenever it is ready. Though I am not sure how to suspend the client side till some content is on the connection.

    Thanks a lot EJP,
    Robert
  • 3. Re: Put on idle HttpServletResponse
    gimbal2 Guru
    Currently Being Moderated
    949658 wrote:
    1. Basically what I want is my client will initiate a doGet request but at that time, the answer is not ready yet. So it should wait and check(in a while loop ?) the connection till a not NULL answer arrives.
    You don't want the browser to be waiting for a response, the user experience is then that nothing is happening which will invite them to refresh the page, causing multiple requests to fire. Better is to treat this as a background job which can be started. The initial response to the client is then "okay, its been started" (or "something is already running!") and you provide an overview of running jobs which can be used to check the status. With some ajax magic you could even automate that without a need for the client to manually reload the job status page.
    2. You are right, I can just set the answer whenever it is ready. Though I am not sure how to suspend the client side till some content is on the connection.
    See 1).
  • 4. Re: Put on idle HttpServletResponse
    952661 Newbie
    Currently Being Moderated
    I understand and you are right.

    But in my case it does not matter so much about user. I am modifying a system which uses HttpServlet to communicate and everything happens behind. I am just wondering how can I suspend/delay the client connection side till something is written by the server.
  • 5. Re: Put on idle HttpServletResponse
    gimbal2 Guru
    Currently Being Moderated
    As EJP said, you don't need to. HTTP works that way, you send a request, you get a response. It might take a long time before the response is actually returned, the server is the one managing the connection, not you.
  • 6. Re: Put on idle HttpServletResponse
    EJP Guru
    Currently Being Moderated
    I am not sure how to suspend the client side till some content is on the connection.
    That's what it does. You don't have to add anything at the server end. just write the response when you're ready. The client will block until it arrives. No action required on your part whatsoever.
  • 7. Re: Put on idle HttpServletResponse
    952661 Newbie
    Currently Being Moderated
    Thank you all guys.

    It was really helpful to solve my problem.
  • 8. Re: Put on idle HttpServletResponse
    gimbal2 Guru
    Currently Being Moderated
    949658 wrote:
    Thank you all guys.

    It was really helpful to solve my problem.
    Its not so much that we were helpful its more that you were very unhelpful - to yourself. You could have just tried it and skipped this whole thread.

    Something to think about.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points