How to get whole HTML from a .vm file in Apache Velocity?

17,460

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.
Share:
17,460
ASingh
Author by

ASingh

Updated on June 05, 2022

Comments

  • ASingh
    ASingh about 2 years

    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.

    • ASingh
      ASingh almost 11 years
      Or if I can return the HTML from the browser back to my servlet in any way...?
    • Sergiu Dumitriu
      Sergiu Dumitriu almost 11 years
      The question isn't clear enough. Can you describe in more details each step of the process?
    • Web Devie
      Web Devie almost 11 years
      And what is the part you have problem with?
    • ASingh
      ASingh almost 11 years
      @Sergiu: After my page has been rendered into the browser..I want to fetch the entire html as a string from inside a given div... Any suggestions on this one...Fetching will be based on a click event...This is fairly easy I guess...but my bad that I am nt getting to the correct solution yet.
  • ASingh
    ASingh almost 11 years
    Thanks Sergiu for your detailed response. I tried your solution and I was able to store my HTML. But I wanted to preserve my Javascript events as well. With your solution, the javascript events are not preserved...Can you help me with this?
  • Sergiu Dumitriu
    Sergiu Dumitriu almost 11 years
    What do you mean? An event is a synchronous notification, or signal, sent by the browser... They're not part of the HTML in any way.