0

For an illustration I would like my servlet to respond to http twice. When lengthy work shall be done, I would like to:

  1. Immediately: Show dialog saying that there will be a delay
  2. Later: Remove dialog and update the page with results from the lengthy work.

If possible I would like to not use any framework and in case that is inevitable it should preferably be a small one. I would like the solution to be deployable as a .war-file.

Is WebSockets the right way to go?

First attempt was a premade request that waited to be released. Once used it was useless for a third update. Second was push-notices. (Sorry that was silly.) Third was websockets but I cant find examples to copy that fit with .war-deployment. Switching from servletrequest to websocket was not possible due to the forced header-sending combined with the Placeholder output-stream.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335

1 Answers1

0

Use ordinary Servlet requests. Return an http-200 and the data for a short-running request. Return an http-202 with a job Id for long-running requests.

When the browser receives the 202 it calls another Servlet with a job Id to setup an event-source

When the log-running job is complete it uses the event-source to signal the browser and, if the results are small, pass them to the browser. If results are large then the receipt of the event in the browser triggers a a Servlet call to fetch the long-running job results.

You will need to start the long-running jobs in new Threads in the server before returning the 202.

John Williams
  • 4,252
  • 2
  • 9
  • 18
  • This is obviously the winning idea, true to the http nature. I have not implemented at this very moment but this makes perfect sense. Thanks so much! – Kåre Jonsson Apr 25 '23 at 20:31