How to read Sling model values into Servlet class | Community
Skip to main content
supriya-hande
New Participant
October 13, 2023
Solved

How to read Sling model values into Servlet class

  • October 13, 2023
  • 1 reply
  • 1003 views

Hello All,

 

I am trying to read Sling model values into servlet class. My sling model is having:

 

@ValueMapValue

@14766979 

 public String recaptchaSecretKey;

 

and I am reading it in servlet as below:

ReadForm model = request.adaptTo(ReadForm.class);
String keys = model.recaptchaSecretKey;

 

But this is getting null. Is there any other way to achieve this.

 

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 aanchal-sikka

Hello @supriya-hande 

 

option-1: You should take the reCaptcha out of this component into a separate OSGi service.

Anything reusable should not be part of a component.

 

option-2: You can read configs of different service via Configuration Admin

https://techrevel.blog/2020/09/20/configurationadmin-access-osgi-configuration-of-other-services/ 

So, servlet can read the config from Model ( if its configurable from OSGI)

 

Also, I might be wrong, but I guess a Sling HTTP request cannot be adapted to a Model.

Lets wait if someone can guide or confirm

1 reply

arunpatidar
New Participant
October 13, 2023

Hi @supriya-hande 
Could you please share what is the use case? and what are the Model and Servlet classes.

 

How does the flow works?

Arun Patidar
supriya-hande
New Participant
October 13, 2023

hi @arunpatidar 

There is a task to integrate google reCaptcha module in AEM component. I have created a custom HTML form component and authoring reCaptcha secret and site key in this component. On submit action it invokes reCaptcha execute method and gives token. 

Then I have written a servlet to verify the token and passing the secret key to get response from google. Here I need both token and secret key in the servlet. I am able to get token in this servlet as this is query parameter but am unable to get secret key value from model class. I have implemented this feature by referring: https://aeminmyway.blogspot.com/p/google-recaptcha-v3-in-aem.html
aanchal-sikka
aanchal-sikkaAccepted solution
New Participant
October 13, 2023

Hello @supriya-hande 

 

option-1: You should take the reCaptcha out of this component into a separate OSGi service.

Anything reusable should not be part of a component.

 

option-2: You can read configs of different service via Configuration Admin

https://techrevel.blog/2020/09/20/configurationadmin-access-osgi-configuration-of-other-services/ 

So, servlet can read the config from Model ( if its configurable from OSGI)

 

Also, I might be wrong, but I guess a Sling HTTP request cannot be adapted to a Model.

Lets wait if someone can guide or confirm

Aanchal Sikka