Expand my Community achievements bar.

SOLVED

Page not rendering servlet POST request

Avatar

Level 2

Adobe Experience Manager, Version 2023.8.13099.20230810T160553Z | (I followed this method to check the version) 

Hi there!  I've been running in circles for weeks with this problem and I don't know what to try anymore, so I'm hoping to get some answers here. PS: I'm very new to this 

What I want to accomplish: create a servlet that lets me update my model/component via an external payload (postman). I have a model with the fields "name" and "age" and they should start empty, and should update with the name and age I send via postman.  Lastly, I want to create a scheduler that updates it every 30 seconds, clearing the content that was sent via servlet, but I'm not there yet, first I need to make the content actually display on the page with the servlet.   This is all for learning purposes.   

First, see how my AEM page is displaying right now: https://i.imgur.com/GwzC6dY.png

It's showing a null value for both fields. If I double click and manually change the name and age, it shows something like this:  

"Name: john  
Age: 20  
John  
20  
"  
However, if I update the name + age via postman, it doesn't update on the page.  

Here are my codes:   
ModelTeste.java code (model): https://pastecode.io/s/rrewbjj1  
TesteServlet.java (servlet): https://pastecode.io/s/b2cfg9kp 
.content.xml (component. It's under _cq_dialog folder): https://pastecode.io/s/qq6rjab2  
modelteste.html (htl file): https://pastecode.io/s/d7o8iix9    
And I didn't attempt to create the scheduler yet, since the first part is not working yet. 
 
This is how postman looks like when I send the post request:  https://i.imgur.com/DJgw2Cd.png

PS: I already tried with "form-data", "x-www-form-urlencoded", and even with the "Params" section, trying to send the request via the URL "http://localhost:4502/bin/TesteServlet?name=John&age=25".   

Do you have any idea on how to proceed? Thank you so much in advance

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @edo_wjp 

From what could see from the code you have shared, the servlet must have one additional parameter and that is, either,

1. the path to the page that contains this component,

2. or the direct path to the component where you need the properties added.

 

The postman request will pass this additional parameter to the servlet. The servlet must cast the String from the additional parameter into a Resource object and set the properties on it.

The component must show the properties thereafter.

 

thanks,

Preetpal

View solution in original post

5 Replies

Avatar

Community Advisor

@edo_wjp 

 

1. Sling Models are meant to display content from Pages. For a page to render Sightly provides visual elements and Sling Models provide data (values saved in Page).

2. The Servlet should fetch the resource (Path to the component node in Page), append values and then commit them.

3. Now you would need to refresh the page to see new values.


Aanchal Sikka

Avatar

Level 2

(it seems my last comment was not successfully sent, so I'm sending it again)   

Hi there, thank you for your reply!   

From what I understand: 
1) My servlet should fetch the resource (node) in the AEM repository corresponding to my component;
2) I should update the values of the resource (name and age) with the data sent via Postman;
3) Commit the changes to the AEM repository.;

But my code is already supposed to do this. Am I missing something?   
 

  1. "Sling Models are meant to display content from Pages:"

    In my Code: My ModelTeste class is a Sling Model designed to adapt the current resource to a Java object, providing data that can be used in the HTML or other presentation logic.
  2. "The Servlet should fetch the resource (Path to the component node in Page), append values, and then commit them:" 

    In my Code: In my servlet's doPost method, I extract data from the request, adapt the current resource to a Sling Model (ModelTeste), update its properties, and then call updateProperties(). This method commits the changes to the repository.



Here is my ModelTeste.java code (model):  

 

https://pastecode.io/s/nrsk3c76

 

 

 

 

Here is my TesteServlet.java code (servlet): 

 

https://pastecode.io/s/2wdrmktc

 

 

 

 

This is how postman looks like when I send the post request:  https://i.imgur.com/DJgw2Cd.png (it shows it's been sent successfully)   

PS: I always refresh the page after sending the post request via postman, but it doesn't reflect on the page  
PS2: the code from my other files are in my initial message

Avatar

Administrator

@edo_wjp Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Correct answer by
Community Advisor

Hello @edo_wjp 

From what could see from the code you have shared, the servlet must have one additional parameter and that is, either,

1. the path to the page that contains this component,

2. or the direct path to the component where you need the properties added.

 

The postman request will pass this additional parameter to the servlet. The servlet must cast the String from the additional parameter into a Resource object and set the properties on it.

The component must show the properties thereafter.

 

thanks,

Preetpal

Avatar

Level 2

Thank you so much! This insight was crucial to solve the problem.    

For future reference, here are the changes I've made:    

1) Added componentPath Parameter:

Added a new parameter, componentPath, to the doPost method.  
2) Used componentPath to Get Resource:

Instead of using a hardcoded path (knownPath), I now use the componentPath parameter.
3) Followed your second suggestion to pass the direct path to the AEM component as a parameter, as I didn't manage do get it to work with the first suggestion.