Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Issue with Sling Servlet and Resource Types

Avatar

Level 1

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

10 Replies

Avatar

Community Advisor

 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

Avatar

Level 1

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

Avatar

Correct answer by
Level 10

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

Avatar

Employee Advisor

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

Avatar

Level 3

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>

Avatar

Level 10

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.

Avatar

Employee

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

Avatar

Level 7

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 ?