Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Get Values from Session-storage in AEM java

Avatar

Level 6

Hi,

 

We have to save some user related data in AEM session-storage.

We have one login API and it will fetch user data and store it to session.

In another API we need some of the values in session storage, this API will trigger immediately after login API.

Can we get the session data using

 

HTTP session =request.getSession();

String userdata = session.getAttribute("userData");

 

Is it a good Approach?

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @JeevanRaj ,

I think my question was confusing. It is not session-storage.

It is httpSession only and we are able to save to http session and able to get the same.

Issue is resolved.

But thank you so much for the info @JeevanRaj and @Ravi_Pampana 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi,

If your pages are not cached in dispatcher or any other cdn the request will go to backend and you can read values stored in session storage, but that will increase the load on the publish instances and we need to disabled caching in dispatcher. 

 

You can check Sling Dynamic Include so that only part of the page can be dynamic instead of full page hitting publisher everytime.

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/set-up-sling...

Avatar

Level 6

Hi @Ravi_Pampana ,

Thanks for the info.

Our first API will set some data in browser Session, that API is actually getting the details of user logged into windows.

Immediately after the first API, we are triggering second one, and it needs some data stored in the session by first login API.

 

Whenever we load pages both APIs will trigger. So can we use above code to get session data ? or else how can we achieve it. ?

 

 

Avatar

Community Advisor

Hi @akhilraj 

 

You cannot get the sessionStorage from SlingHttpServletRequest. You will have to pass the value saved in the sessionStorage to the API. For ex while you are making the ajax call from your JS, you can fetch the value from the sessionStorage and pass it as a query param.

 

JS snippet:

 

var someValue = window.sessionStorage.getItem("userName");
$.ajax({
url: "/bin/testServlet?abc=" + someValue,
type: "GET",
dataType: "json",
success: function(data, textStatus, jqXHR) {},
error: function (jqXHR, textStatus, errorThrown) {}
});

Then you can fetch this value on server side through the SlingHttpServletRequest object.

request.getParameter("abc")

Please let me know if this works for you.

 

Thanks

Jeevan

Avatar

Correct answer by
Level 6

Hi @JeevanRaj ,

I think my question was confusing. It is not session-storage.

It is httpSession only and we are able to save to http session and able to get the same.

Issue is resolved.

But thank you so much for the info @JeevanRaj and @Ravi_Pampana