AEM servlet redirect with parameters and display result using slightly | Community
Skip to main content
New Participant
September 18, 2020
Solved

AEM servlet redirect with parameters and display result using slightly

  • September 18, 2020
  • 3 replies
  • 7731 views

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 ?

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

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.

3 replies

raj_mandalapu
raj_mandalapuAccepted solution
New Participant
September 19, 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.

New Participant
September 19, 2020
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 ?
BrianKasingli
New Participant
September 18, 2020

@bunny87948290,
There's a procedure in how code is structured while passing params from invoking a response.sendRedirect(), have a look at this article where code examples are present, https://www.mysamplecode.com/2011/06/java-servlet-redirect-vs-forward.html

New Participant
September 18, 2020
I have tried something similar but it didn't work, I am trying to show the parameter value in slightly. I am wondering if I need to use sling model to pass the value
Nikhil-Kumar
New Participant
September 18, 2020
New Participant
September 18, 2020
I don't see any parameter passed and retried using slightly with this approach, I am looking to display value using htl/slightly