Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  82] [ 4]  / answers: 1 / hits: 18472  / 11 Years ago, tue, july 30, 2013, 12:00:00

I would appreciate your help on my use case. I have a Servlet which renders some information using javascript in a Apache Velocity template (.vm) file.



Now, before I return this template to the browser, I want to store the entire HTML into my local file system for which I need to access the whole HTML from the .vm template. I am stuck at doing the last step.


More From » html

 Answers
6

Web applications are client⟷server applications, meaning that there is a clear separation between the client, which is your browser, and the web server. There is no direct connection between the server and the HTML that you see in your browser.



Try to visualize the process:




  1. The user tries to open a web page, so the browser sends a HTTP request to the server.

  2. The server processes the URL that was requested and identifies that it should go to the servlet that processes velocity templates, and identifies the .vm file that should be used to render the response.

  3. The .vm file is read by the servlet on the server and rendered into a string representation of the HTML.

  4. The HTML is sent to the client in the HTTP response. From now on, the server has no connection to that HTML.

  5. The browser reads the HTML from the response, parses it, and displays it.

  6. The JavaScript resources associated with that HTML are also fetched from the server, parsed and executed (in the client browser).



There is no way for the Velocity template (or any other code on the server) to access the HTML that is now in the browser, unless the browser explicitly sends it back to the server in another request.



What you can do is:




  1. Write another piece of JavaScript code that listens to the click event.

  2. The JS gets the serialized HTML from your target element, something like var html = document.getElementById('id_of_the_element').innerHTML;

  3. The JS sends this string to the server using an XMLHttpRequest, either using the raw XHR support from the browser, or a JS framework of your choice.

  4. On the server you write another servlet (or extend the functionality of an existing servlet) that receives this HTML and processes it as you want.


[#76640] Monday, July 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rossj

Total Points: 606
Total Questions: 100
Total Answers: 116

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
;