Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Java object data to be used in Sightly

Avatar

Level 5

In a post call API, I'm creating an HttpSession object and storing some values in it. Now, is there a way I can use the stored value in my AEM component?

 

The data stored is logged-in user's data, and the requirement is to use that data to populate certain fields in a component. How can I achieve this?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

9 Replies

Avatar

Level 4

Hello,

 

This can be achieved by getting the user data information from the HTTP Session object in your back-end server code, and then sending that to your AEM setup - likely via an HTTP request to a Servlet or Service in AEM. AEM can then store this information in a Sling Model, JCR node or OSGi service that your AEM component can access to populate its fields.

 

HttpSession session = request.getSession();
session.setAttribute("userData", userDataObject);

 

Then make an HTTP request to the servlet URL and send the user data in this request

// using Fetch API for a POST request
fetch('/bin/myServlet', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userDataObject)
});

 

Thanks,

Venkat

Avatar

Level 5

From where should this HTTP request to servlet URL be made?

Also, what if multiple attributes need to be shared to AEM?

Avatar

Level 4

Use Ajax, Sling Models+Pojo classes or servlets. 

If you have multiple attributes, just put them in a HashMap or custom Java Object and add that as an attribute to session. Then return this map or object as a JSON response from your servlet.

Avatar

Community Advisor

Hi @goyalkritika 
You may need to do it client side using javascript.

 

If you will use session Storage/user data in HTL then this will be cached and serve for all the consumer.

 

So just render the template from HTL and update the values using javascript based on user data.



Arun Patidar

Avatar

Level 4

Hi @goyalkritika ,


Your use case is not very clear from your question.
is the POST API hosted on AEM itself? or is it external to AEM? How is the page where you want to display the user information related to the post call?

Avatar

Employee Advisor

This approach of using a server-side session will cause problems, as the AEM default architecture forsees a clustering approach only on the author side, and on the publish side there is a farm (independent systems which do not know of each other). In all cases the server-side HTTP-sessions are not shared.

 

That means this works as long as the user is accessing exact 1 instance (of potentially multiple). It requires access to the backend, which limits your scalability (you cannot cache the page holding that component in dispatcher or the CDN).

 

I would not use this approach.

Avatar

Level 5

Hi @Jörg_Hoh 

According to you, what is the most efficient way of populating user data in an AEM component?

User will login to the application, user profile is received at AEM end, inside httpSession object some user data attributes will be saved. 

Now, the same user profile is available at client side as well. 

How to do this the best and efficient way possible?

Avatar

Employee Advisor

isn't it possible to fetch the userprofile from the system where this profile is stored, instead of importing it into AEM and process it on a server-side there?  In that case the client (browser) will can fetch the data and insert it into the page where required.

Avatar

Administrator

@goyalkritika  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