3

I have a Servlet that is responding to GET requests as expected. The problem is that POST requests are not getting handled by my Servlet, rather by SlingPostServlet according to /system/console/requests . My Servlet is using the SCR annotations.

@SlingServlet(
  methods = {"POST","GET"}, 
  resourceTypes = {"company/components/pages/thepage"},
  extensions = { "html" }, 
  selectors = { "edit" },
  generateService = true,
  generateComponent = true,
  name = "com.company.services.osgi.package.EditServlet",
  label = "Profile Update Handler"
)

@Properties({
    @Property(name = "service.vendor", value = "Our Company"),
    @Property(name = "service.description", value = "Update Handler") })

public class EditServlet extends SlingAllMethodsServlet {
    ... 
    @Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response){
        // during GET requests this code works!
  }

    @Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response){
  // during POST requests this code is not executing
}

I see in http://localhost:4502/system/console/components that my servlet properties are set, and is active

component.id = 3463
component.name = com.company.services.osgi.package.EditServlet
sling.servlet.methods = [POST, GET]
sling.servlet.resourceTypes = [company/components/pages/thepage]
sling.servlet.selectors = [edit]
sling.servlet.extensions = [html]

I don't see errors when the bundle is installed. It seems both are REGISTERED

15.07.2015 13:05:24.017 INFO [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver Registered ServletResourceProvider: servlet=com.company.services.osgi.package.EditServlet, paths=[/libs/foundation/components/primary/company/components/pages/thepage/edit.html.POST.servlet, /libs/foundation/components/primary/company/components/pages/thepage/edit.html.GET.servlet]

I've confirmed the page has the correct resourceType (if it didn't then I suppose the doGet would also not be working). What an I doing wrong with POST? Any ideas why doGet works but doPost doesn't?

Update The only way I can get this working was to create another servlet for the doPost which operates on resourceTypes = {"sling/servlet/default" }, I updated the first one removing POST from methods

@SlingServlet(
  methods = {"POST"}, 
//  resourceTypes = {"company/components/pages/thepage"},
  resourceTypes = {"sling/servlet/default" },
  extensions = { "html" }, 
  selectors =  {"post-servlet"} ,
  generateService = true,
  generateComponent = true,
  name = "com.company.services.osgi.people.UpdateServlet",
  label = "Update Handler",
  metatype=true 
) 
Cris Rockwell
  • 904
  • 2
  • 16
  • 30
  • maybe this is just a copy past error in your posted code, but you have different resourceTypes in place: "company/components/pages/department_person_profile" and "company/components/pages/thepage". What is also confusing for me is the path in he log file: "/libs/foundation/components/primary/company/components/pages". Why is it in libs foundation and not somewhere in /apps. And last but not least also in the log "edit" seems to be a part of the path and not a selector... – Thomas Jul 16 '15 at 06:38
  • I corrected the copy paste issue with the resource type in the log message above. I've checked my other selector servlets and the logs report paths similar to this one under libs upon install. I don't have answers your questions. it seems this is how sling installs servlets based on resource types, selectors and methods. – Cris Rockwell Jul 16 '15 at 13:09

2 Answers2

2

we don't see a sample of your POST request, but my guess is you might be POSTing to the page path, while the resource type your servlet is registered upon is on the child resource jcr:content.

GET requests with the html extension on cq:Page resource are redirected internally to the jcr:content resource's html rendering, but there is no such thing for the POST requests.

0

Which AEM verison are you using, because from 6.x POST request are defined in CSRF token filter.If version is 6.x the remove it from CSRF filter configuration or add CQ.jquery clientlib with property dependency.It will automatic handle this filter.

amita
  • 1
  • 1