I have a jsp file which contain javascript functions at top of file. These functions
1. send/receive data to a server
2. Interact with the jsp file to implement drag & drop
This has always seemed off putting to me, as its mixing two/three languages – jsp & javascript/jquery on one page. Is there a design pattern to better implement this functionality or just a best practice ?
JSP is server-side and javascript is client side.
Javascript does everything that you want to happen on the browser (client-side).
JSP is server-side so computation is done on the server. This can be things like database calls, xml parsing or other calculations
If you want to send and receive data from a server then you would use AJAX.
There isn’t a particular design pattern you should use with these 2 languages. A better understanding of client-server interaction is what you need.
In short – The browser sends a request to the server. The server performs some sort of computation and prepares a response to send back to the client. The client receives this response and does what ever it needs to do with it
I am not sure what you are using drag and drop for but I’ll give a really simple example for you to understand client-server interaction.
Send a request to JSP to get drag speed.
JSP accesses some properties that contain the drag speed data.
JSP sends this back to the client as a JSON response.
The Javascript parses the JSON response and sets the speed value.