Is it possible to send ajax request to the server, store the HttpServletResponse Object there and send a response at any time to the clent?

  $.get({ url: '/startChat', async: true, success: function (msgToAdd, textStatus, jqXHR) { alert(msgToAdd) }, error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } }); 

Server:

 private HttpServletResponse firstResponse; @RequestMapping(value = "/startChat", method = RequestMethod.GET) public void startUserChat(HttpServletRequest request, HttpServletResponse response) { firstResponse = response; } 

Later use the saved firstResponse object to send a message to the success function ajax

I tried to do this, my-message comes to the success function, but with some strange pattern of 90%, it is, and the randomness pattern.

Checked the httpServletResponse object is the same in the case when msg is attached and when not.

    1 answer 1

    The HttpServletResponse object instance belongs to the container (web server), not to you. It can be associated with a variety of container resources that the application programmer does not know in most cases. Typically, a specific Response is associated with a specific Request and an attempt to replace Response may cause your software to become unspecified. Do not store or try to replace such resources.

    The content of the response should be passed to the client in HttpServletResponse.getOutputStream (), and the cached response should be stored in a format that can be transmitted as a stream (directly, or using serialization). In this case, you should always use Response , just received from the container.