Accessing SlingServlet Data into SlingModel | Community
Skip to main content
Level 2
March 25, 2022
Solved

Accessing SlingServlet Data into SlingModel

  • March 25, 2022
  • 3 replies
  • 1006 views

Hi All,

I have created Search Servlet  and as per the requirement I need to track search data like search count ,search result.

so for that I have created Sling Model for Analytics Tracking of current page data and For that Sling Model I need search data as well.

does anyone worked on same type of requirement.

 

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 BrettBirschbach

If you are running server-side code to push this information over to Analytics or some other back end system, then I would agree with Brian's post here.  Sling models are generally for representing a JCR resource that correlates with a page component.  An OSGi service is going to provide you access to all of the same contexts that would be applicable to your task, and would be easier to leverage from your search servlet.

 

However, if you're looking for Analytics to pick this data up from the front end web page (similar to other Analytics data), it would be great to simply push this context into the Analytics datalayer.  Rather than using a servlet for search results, you can create a Sling Model for your "search_results" component - that sling model could A) execute the search and then B) update the datalayer with the data to by picked up by Analytics.

 

3 replies

Kiran_Vedantam
Community Advisor
Community Advisor
March 25, 2022

Hi @savi50,

 

You can inject servlets like this

 

@Inject private List<Servlet> servlets;

 

Thanks,

Kiran Vedantam.

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
March 25, 2022

As a best practice to enforce re-usability, Maybe you can extract the business logic from the servlet into an OSGI service, so that all of your OSGI components can be using the same service. 

BrettBirschbach
Adobe Champion
BrettBirschbachAdobe ChampionAccepted solution
Adobe Champion
March 25, 2022

If you are running server-side code to push this information over to Analytics or some other back end system, then I would agree with Brian's post here.  Sling models are generally for representing a JCR resource that correlates with a page component.  An OSGi service is going to provide you access to all of the same contexts that would be applicable to your task, and would be easier to leverage from your search servlet.

 

However, if you're looking for Analytics to pick this data up from the front end web page (similar to other Analytics data), it would be great to simply push this context into the Analytics datalayer.  Rather than using a servlet for search results, you can create a Sling Model for your "search_results" component - that sling model could A) execute the search and then B) update the datalayer with the data to by picked up by Analytics.