Search code examples
javarestspring-bootspring-websocketauto-populate

How to push/send data from server (REST api response) to a jsp/html form


My requirement is as below

A user before scanning a label will open the web application(spring-boot) and click on a link which shows an empty html form. Now when he scans the label, the details of that scan should auto populate in the form that he had opened. Behind the scenes, the same application will also expose a REST api which receives the the details of the scan in JSON format. This json values should be pushed automatically to the form that is already open.

Are websockets a way to fulfill the above requirement? Or is there a better/easier way to do this?

Thanks.


Solution

  • Web sockets is definitely a viable option.

    A pure HTTP based solution would be polling:

    You can use short polling: The client asks for new data every few seconds.

    Or long polling: The server holds the http request until new data is ready for the client and responds with the data. Then the client immediately sends a new request and the server holds request again ...

    Here is a nice explanation about polling and how to implement long polling in Spring

    https://spring.io/blog/2012/05/14/spring-mvc-3-2-preview-adding-long-polling-to-an-existing-web-application