Is there a way to transfer JSON values from JSP to JavaScript as an object using AJAX without resorting to global JavaScript variables, which can expose the JSON content in the page's source?
The desired scenario is as follows:
- The JSP URL is opened in a browser.
- Data is generated within a scriptlet and converted to JSON format
- The JSON object is then passed to JavaScript
Based on the scenario above, it seems that JavaScript needs to trigger the AJAX call to the JSP. However, this approach will result in the JSP code being executed twice:
- When the page loads - data preparation occurs
- On each subsequent AJAX call, the same code will run again
Unfortunately, there are several constraints: no jQuery, no other libraries, no servlets, and no additional JSPs. :(
EDIT:
There is another complication: I need to pass multiple JSON objects to JavaScript, and using response.getWriter().write();
won't suffice.
Merging all JSON objects into one string for transmission doesn't seem like the best solution.
The parsing of the response object in JavaScript http.responseText
could become unmanageable.