0

I wrote an asynchronous servlet to feed changes in a cached object to all clients who have sent a request to the servlet.

With the request a client can get a subsection of the cache by including different parameters.

I am simply storing the requests in an ArrayList and iterating through them whenever a change occurs to send the responses back to the client.

Everything is working well, except now I have a requirement to handle a situation where a client will send a second request with possibly different parameters.

I would like to replace the old request I have stored with this new one when that happens. But to do that I need to know if the request came from the same user. Is there a way to test if the request came from the same user with Servlets 3.0?

egerardus
  • 11,316
  • 12
  • 80
  • 123

1 Answers1

1

The old request must be served. You can send an error response, with Connection:closed header.

To associate requests from the same client, the only way is through a cookie. You can compare the session id (which is a cookie) from the two requests. If any incoming request does not have a session, you should establish a session (by request.getSession()), then do a redirect, so that the client will retry the request with the jsessionid cookie.

if request.getSession(false)==null
    request.getSession(true);
    response.sendRedirect( request.uri +"?"+ request.query )

Redirect is necessary in your case since normally it'll take a while to return a response, but we want to set the cookie ASAP.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61