How can I call a backend method of a java model from the htl frontend? | Community
Skip to main content
New Participant
October 10, 2023

How can I call a backend method of a java model from the htl frontend?

  • October 10, 2023
  • 3 replies
  • 1678 views

Hello, what happens is that I have a template that is for products and the information is brought from AiO, but what I want to do now is that in the template we want to implement a button that when clicked, create a pdf and download it.

 

For it in the java model of the template I have created a method that contains the structure and design of the pdf using the library IText7, since, the pdf will be different to the design of the template.

 

My question here is, how can I do so that in the frontend of the htl when I click on the download button I call the java backend to the method that creates the pdf and download it from the template model?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

EstebanBustamante
New Participant
October 11, 2023

I agree with @saravanan_dharmaraj  and @harwinder-singh , you need to use a servlet instead.

 

Do the following:

1. Create a servlet that has the functionality to create the PDF (this is Java) Example: https://medium.com/@toimrank/aem-sling-servlet-ca9e5926a384 

2. Once you click the button, you will use Javascript and through an AJAX call, you will invoke the servlet. Example: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-call-servlet-from-listener-event-js-function/m-p/222069 

 

Here is a full example of both parts working together: https://www.youtube.com/watch?v=iQu65qNN428 

Esteban Bustamante
New Participant
October 11, 2023

Nice, also I have another question, if I use a servlet, how would I send the information of my template model (product name, product image, description, etc) to the servlet?

Harwinder-singh
New Participant
October 10, 2023

@aaron_dempwolff , You can consider creating a servlet registered to the component resource type. This servlet should be able to serve the PDF by creating it using the mentioned PDF library. 

New Participant
October 11, 2023

Do you have any guide or tutorial on how I could create a servelt for my java method and how I can send it to call in the frontend when I click on the button?

 

Also I have another question, if I use a servelet, how would I send the information of my template model (product name, product image, description, etc) to the servelet?

Saravanan_Dharmaraj
New Participant
October 10, 2023

@aaron_dempwolff you cannot make a call to sling model by button click, thats more of client side interaction but you can make a call to servlet(JS Ajax call)and get the return of pdf url it generated and take the user to that pdf in same tab or new tab. Just an idea.

New Participant
October 11, 2023

But my method that creates the PDF is in java, it is not in javascript, can I still make the call?

 

And if so, do you have any guide or tutorial on how I could create a servelt for my java method and how I can send it to call in the frontend when I click on the button?

 

Also I have another question, if I use a servelet how would I send the information of my template model (product name, product image, description, etc) to the servelet?