{"id":23779,"date":"2015-08-04T00:31:04","date_gmt":"2015-08-03T19:01:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23779"},"modified":"2015-08-04T09:49:44","modified_gmt":"2015-08-04T04:19:44","slug":"registering-a-servlet-for-every-page-in-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/registering-a-servlet-for-every-page-in-aem\/","title":{"rendered":"Registering a Servlet for every Page in AEM"},"content":{"rendered":"<p><strong>UseCase<\/strong>: &#8211; I had a situation in which needed to return custom information corresponding to every page in AEM and also use it via component to display to end user .<\/p>\n<p>-Then i have to invoke a Servlet on every page and with the use of selector want to return some information in JSON Format which can be used to display via Component on a Page to end user .<\/p>\n<p><strong>Solution<\/strong> : -For such situations we use the resourceType as <strong>cq\/Page<\/strong> for registering Servlet , so that Servlet is registered for every Page.<\/p>\n<p>&#8211; Then with the use of a particular selector we can get results like list information\/required custom information for every AEM page and display either via component with its ajax request has a url of Current Page.Selector which invokes the Servlet and renders result .<\/p>\n<p><strong>Note<\/strong>: &#8211; Just for reference Sling resolves on basis of Primary Type example cq:Page for Page and dam:Asset for Dam in case sling:resourceType is missing .<\/p>\n<p>Code Snippet :<\/p>\n<p>[java]<br \/>\n@Component(name = &quot;Site-Results&quot;, metatype = false, configurationFactory = false, policy = ConfigurationPolicy.OPTIONAL)<br \/>\n@Service(Servlet.class)<br \/>\n@Properties([<br \/>\n    @Property(name = &quot;sling.servlet.resourceTypes&quot;, value = [&quot;cq\/Page&quot;]),<br \/>\n    @Property(name = &quot;sling.servlet.selectors&quot;, value = &quot;results&quot;),<br \/>\n    @Property(name = &quot;sling.servlet.extensions&quot;, value = [&quot;json&quot;]),<br \/>\n    @Property(name = &quot;sling.servlet.methods&quot;, value = &quot;GET&quot;),<br \/>\n    @Property(name = &quot;sling.servlet.description&quot;, value = &quot;Gives Results for Every Page&quot;)<br \/>\n])<br \/>\nclass ResultsServlet extends SlingSafeMethodsServlet {<\/p>\n<p>    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {<\/p>\n<p>\/\/Implement Your Logic<\/p>\n<p>}<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UseCase: &#8211; I had a situation in which needed to return custom information corresponding to every page in AEM and also use it via component to display to end user . -Then i have to invoke a Servlet on every page and with the use of selector want to return some information in JSON Format [&hellip;]<\/p>\n","protected":false},"author":127,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":65},"categories":[1],"tags":[1235,4847,2114],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23779"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/127"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23779"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23779\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}