Issue with Sling Servlet and Resource Types | Community
Skip to main content
josh_saunders
October 16, 2015
Solved

Issue with Sling Servlet and Resource Types

  • October 16, 2015
  • 10 replies
  • 8111 views

I am deploying a Sling Servlet to AEM and would like to use resourceTypes, selectors, and extensions, but they're not working. Paths on the other hand, do work. Any idea what the issue is?

This works:

@SlingServlet(paths = {"/services/powerproxy/groups"}) public class GroupServlet extends SlingAllMethodsServlet {...}

localhost:4502/services/powerproxy/groups result in a HTTP 200

This doesn't:

@SlingServlet(methods = {"GET"}, metatype = true, resourceTypes = {"services/powerproxy"}, selectors = {"groups"}) public class GroupServlet extends SlingAllMethodsServlet {...}

localhost:4502/services/powerproxy/groups results in a HTTP 404

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 smacdonald2008

Runal.Trivedi answer works nicely. I tested it out and got:

20.04.2015 13:16:54.081 *INFO* [0:0:0:0:0:0:0:1 [1429550214075] GET /content/geometrixx/en/servletpage.groups.html HTTP/1.1] com.adobe.cq.sling.HandleClaim ---> THIS IS THE GET METHOD OF slingSevletApp/components/page/slingTemplate

 

Of course my servlet simply printed this line to the log file. 

WHen you want to bind a servlet to a page - use ResouceType binding.

However - when you want to post values from a CQ page to a servlet  (ie - using AJAX):

[img]Client.png[/img]

bind you servlet to a path - which is a supported way:

https://sling.apache.org/documentation/the-sling-engine/servlets.html

For those reading this and want to use AJAX to post form values to a Sling Servlet (that uses Path binding) - see:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Here is the AEM article that talks about binding a servlet using ResourceType - including how to invoke it:

https://helpx.adobe.com/experience-manager/using/resourcetypes.html

10 replies

Level 3
October 16, 2015

You may need to change how you are accessing the servlet, Please see this - https://sling.apache.org/documentation/the-sling-engine/servlets.html#example-registration-by-resource-type-etc

Runal_Trivedi
Level 6
October 16, 2015

 Basically you POST/GET to a node of your repository which has sling:resourceType property same as declared in your Servlets annotation.

You can further let your Servlet get invoked if your post/get path contains selectors, which in your case is "groups".

Now coming to your example:

  • Create a page in your repository with sling:resourceType having value services/powerproxy let assume it is - /content/geometrixx/en/servletpage.html
    • servletpage has got sling:resourceType as services/powerproxy
  • hit the page with selector as groups i.e. hit the url - http://localhost:4502/content/geometrixx/en/servletpage.groups.html
    • here you will be able to see the response from GET method of your Servlet.

Hope it clarifies your doubts.

- Runal

josh_saunders
October 16, 2015

I've read that several times. I don't understand what you mean by "change how you are accessing the servlet". Can you elaborate?

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

Runal.Trivedi answer works nicely. I tested it out and got:

20.04.2015 13:16:54.081 *INFO* [0:0:0:0:0:0:0:1 [1429550214075] GET /content/geometrixx/en/servletpage.groups.html HTTP/1.1] com.adobe.cq.sling.HandleClaim ---> THIS IS THE GET METHOD OF slingSevletApp/components/page/slingTemplate

 

Of course my servlet simply printed this line to the log file. 

WHen you want to bind a servlet to a page - use ResouceType binding.

However - when you want to post values from a CQ page to a servlet  (ie - using AJAX):

[img]Client.png[/img]

bind you servlet to a path - which is a supported way:

https://sling.apache.org/documentation/the-sling-engine/servlets.html

For those reading this and want to use AJAX to post form values to a Sling Servlet (that uses Path binding) - see:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Here is the AEM article that talks about binding a servlet using ResourceType - including how to invoke it:

https://helpx.adobe.com/experience-manager/using/resourcetypes.html

joerghoh
Adobe Employee
Adobe Employee
October 16, 2015

Hi,

you can use the binding on the resource type irrespective of the HTTP method you want to use. Of course it makes the creation of the javascript a bit easier, if you can hardcode a path for the POST request, but on the server side this doesn't matter.

kind regards,
Jörg

Level 3
October 16, 2015

the correct way to access the servlet registered by resourceTypes would be something like the following-

localhost:4502/<path-to-a-resource-with-resourcetype-services-powerproxy>.groups

 

the following code in powerproxy.jsp should call the servlet-

 

<form name="frm" method="post" action="<c:out value="${resource.path}" />.group">     //form stuff     //     <input type="submit" /> </form>
smacdonald2008
Level 10
October 16, 2015

This needs to be captured in a community article.  If this works, we will get this use case in an AEM community article.  We have one for path.

Adobe Employee
February 7, 2018

Hi,

I am new to AEM technology . Can someone tell me what is services/powerproxy  resourceType  Exactly mean?? When and Where it is used for

AdobeID24
Level 5
July 10, 2019

really helpful....this is wat the way i can invoke my servlet (which has been regeistered by using resourceType instead of path).

just need to hit the page(obviously having the same resoruceType for which i made my servlet ) with the extension mentioned in servlet.


is that correct ?