AEM servlet redirect with parameters and display result using slightly

Avatar

Avatar

bunny87948290

Avatar

bunny87948290

bunny87948290

17-09-2020

Hello,

 

I am trying to redirect to .html page from servlet - 

 

String cased = "abcd4444";

response.sendRedirect(redirect_URL);

 

I need to pass the caseId parameter to redirect url from servlet and show it on the page. What is the best way to achieve it ? Is it possible via sling Model ?

View Entire Topic

Avatar

Avatar

raj_mandalapu

Avatar

raj_mandalapu

raj_mandalapu

19-09-2020

It is not possible to pass query string value using the sling model to another page, you can use the below code.

response.sendRedirect(redirect_URL + "?cased=" + cased);

in the redirected page you need to read the query string value for this, you can create a sling model class in the post construct method of sling model you can read the query string value and assign it to the property.

In the redirected page where you want to render cased, you need to create an object for the sling model and display cased value.

you can also use Jquery to read query string value and render on the page, it is up to your comfort level

 

Instead of passing via the query string, you can also follow another approach which is storing cased value in the browser cookie and read either in the sling model or in javascript and display on the redirected page.

Thanks you for the suggestion, I don't want to pass anything as query parameter. I think cookie option may work. For that, I need to set the cookie in servlet, then how would I read it in front end via sling model. I think Javascript may be possible. Can you please elaborate on this ? Also, I tried request.getSession().setAttribute("caseID",caseID) , and reading it with JS use API in slightly. IT seems to be working. But, I am not sure will there be any issue with this in production since we have two publishers ?

I feel this request.getSession().setAttribute("caseID",caseID) is not recommended one and it is tied to a user session, what if the request goes to other publish server, you need to use sticky connections and setup dispatcher configurations. please refer this for more information https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/new-httpsession-on-every-r...

create a sling model called CaseDetails, inside the sling model. you can read the cookie value by using the below code, once you have cookie value assign it to the property.

 

public String getCaseID()
{
  return readCookie(SlingHttpServletRequest slingRequest)
}

private String readCookie(SlingHttpServletRequest slingRequest) {
		Cookie[] cookies = slingRequest.getCookies();

		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("cased")) {
					return cookie.getValue();
				}
			}
		}
		
	}

 

 Create a new component and instantiate data-sly-use.case="com.org.CaseDetails" then in the sightly call case.getCaseID to display cased id on the page.

You need to author this component on the redirected page.

Javascript:

You can not read query string in the JS use API, you need to use plain javascript, put a placeholde on the redirected page something lie this <span id="cased"></span>

In javascript/jquery write logic to read cookie value then if you use jQuery you need to assign cookie value to cased somethig like this

$("cased").text(<cookievalue">)

Got it! This is helpful, will get it try. One more question - Once I get the case ID, I have text component and text authored as "Thank you for your information, your case ID is caseIDNew" and on slightly page, have this script added to replace "caseIDNew" with the ID I get from sling model or JS, it seems to work, but just want to check if there is any other approach.

 

<sly data-sly-use.info="${'formparam.js'}">

<script id="temp-script">
$(document).ready(function () {

$('body').find('*').each(function() {
var html= $(this).html().replace(/caseIDNew/g, ${info.title @ context='scriptString'});
$(this).html(html);
});
});

</script>

</sly>

If you use java sling model or js use api, you do not need script, you can directly use HTL expression language., ${' Thank you for your information, your case ID is {1}' @ format=[cased.getCaseId]}, refer this https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/expression-language.html

if you want to read query string value using JS Use API, then the above HTL expression language also should work.

 

If you want to read query string value using plain js, then you need to follow below code.

<script id="temp-script">
$(document).ready(function () {

$('body').find('*').each(function() {
var html= $(this).html().replace(/caseIDNew/g, ${info.title @ context='scriptString'});
$(this).html(html);
});
});

With Sling model, the sentence is hard coded - ${' Thank you for your information} , want to give ability to author to author and format the message however they want. So, I used the text component with rte plugin, and then just replace the case ID in the text. Is there any way to maintain authoring ability here ?

So I used the model code as you have mentioned, had to update little but as it was giving error for SlingHttpServletRequest in model class. So here is my code -

 

 

In the servlet, I am setting cookie value as before redirect - 

 

Cookie cookie = new Cookie("caseid", caseId);
response.addCookie(cookie);
response.sendRedirect(redirect_URL);

 

In HTL/Slightly , printing the value as - 

<sly data-sly-use.case="xyz.core.sling.models.CaseDetails">
<h1> Thank you for information, your case id is:${case.getCaseID} </h1>

</sly>

It is is printing all the log statements from model class, but can't find the cookie with name caseid, so it's not displaying on page, Am I missing anything while setting cookies ?

 

I think setting cookie max age and path worked! Thanks for the sling model solution.