Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

AEM 6.4 Servlet POST request not hitting servlet

Avatar

Level 3

My problem is similar to the one described here: AEM 6.4 - Java sling servlet does not work with uber 6.4 jar, but does with uber 6.3

I have the following Servlet:

@Component(service=Servlet.class,

    property={

        Constants.SERVICE_DESCRIPTION + "=Video servlet",

        "sling.servlet.methods=" + HttpConstants.METHOD_POST,

        "sling.servlet.paths="+ "/bin/damUpload/video"

    }

)

public class VideoServlet extends SlingAllMethodsServlet {

// ...

@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

            System.out.println("POST TO /bin/damUpload/video");

             // ...

When I do a POST to localhost:4502/bin/damUpload/video, I get a 500 error:

18.05.2018 14:47:24.020 *ERROR* [0:0:0:0:0:0:0:1 [1526676444017] POST /bin/damUpload/video HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Unable to create resource named damUpload in /bin

18.05.2018 14:47:24.020 *ERROR* [0:0:0:0:0:0:0:1 [1526676444017] POST /bin/damUpload/video HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing.

org.apache.sling.api.resource.PersistenceException: Unable to create node at /bin/damUpload

    at org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProvider.create(JcrResourceProvider.java:480) [org.apache.sling.jcr.resource:3.0.8]

    at org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider.create(AuthenticatedResourceProvider.java:182) [org.apache.sling.resourceresolver:1.5.34]

    at org.apache.sling.resourceresolver.impl.helper.ResourceResolverControl.create(ResourceResolverControl.java:381) [org.apache.sling.resourceresolver:1.5.34]

I don't see the output for the System.out.println in my stdout.log, so I don't think it ever gets hit.

My bundle appears to be installed and active, and my service appears to be registered:

1490465_pastedImage_4.png

1490466_pastedImage_5.png

1490467_pastedImage_6.png

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Yours imports are fine. could you please try to invoke simple servlet with Get method for testing, to check whether issue is with code or something else.

@Component(service=Servlet.class,

property={

Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths="+ "/bin/damUpload/video",

})

public class SimplePostServlet extends SlingAllMethodsServlet {

/**

  *

  */

  private static final long serialVersionUID = 1L;

@Override

protected void doGet(final SlingHttpServletRequest req,

final SlingHttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/plain");

        resp.getWriter().write("Servlet found!");

        System.out.println("Servlet found!");

    }

}



Arun Patidar

View solution in original post

9 Replies

Avatar

Community Advisor

Hi,

I tried your code for me its working.

Are you using new osgi annotation or felix annotation? Please check Jorg's answer in related Post.

Thanks

Arun



Arun Patidar

Avatar

Level 3

arunp99088702

I think I'm using the new osgi annotation. I have also tried the solution in Jorg's answer on the related post before asking this question.

My imports for the servlet are these:

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.jcr.api.SlingRepository;

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import java.io.IOException;

import javax.jcr.Session;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.sling.api.servlets.HttpConstants;

import org.osgi.framework.Constants;

These are in my pom file:

<dependency>

            <groupId>org.apache.felix</groupId>

            <artifactId>org.apache.felix.scr</artifactId>

            <version>1.6.0</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>org.apache.felix</groupId>

            <artifactId>org.apache.felix.scr.annotations</artifactId>

            <version>1.9.6</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>biz.aQute</groupId>

            <artifactId>bndlib</artifactId>

            <version>1.50.0</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>org.osgi.core</artifactId>

            <version>4.2.0</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>org.osgi.service.component.annotations</artifactId>

            <version>1.3.0</version>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>org.osgi.service.metatype.annotations</artifactId>

            <version>1.3.0</version>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>org.osgi.annotation</artifactId>

            <version>6.0.0</version>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>org.osgi.compendium</artifactId>

            <version>4.2.0</version>

<scope>provided</scope>

        </dependency>

Avatar

Correct answer by
Community Advisor

Yours imports are fine. could you please try to invoke simple servlet with Get method for testing, to check whether issue is with code or something else.

@Component(service=Servlet.class,

property={

Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths="+ "/bin/damUpload/video",

})

public class SimplePostServlet extends SlingAllMethodsServlet {

/**

  *

  */

  private static final long serialVersionUID = 1L;

@Override

protected void doGet(final SlingHttpServletRequest req,

final SlingHttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/plain");

        resp.getWriter().write("Servlet found!");

        System.out.println("Servlet found!");

    }

}



Arun Patidar

Avatar

Level 4

I have created a servlet with the above the code mentioned by you, I have also build it successfully, when I accessed the page:http://localhost:/bin/damUpload/videohttp://localhost:8502/bin/damUpload/video, I found the message "servlet found!"

I am making a post call using the code:

$.post(

                           "/content/we-retail/ca/fr/jcr:content/",

                       {variableName : value},

                       function(result) {

                           window.alert('Here is your result : <strong>' + result + '</strong>');

                        }

                );

it is creating a node and writing the data to the node's property without a servlet. I think it making use of defaultPostServlet, but how can I make a call to this custom servlet?

Avatar

Community Advisor

Shaheen ,

  For the above code Arun provided , did you add doPost before testing POST call ?

Avatar

Community Advisor

Hi,

What is your action url? why are you making call to /content/we-retail/ca/fr/jcr:content/?

You should make a Ajax call to /bin/damUpload/video

e.g.

$.post(

                           "/bin/damUpload/video",

                       {variableName : value},

                       function(result) {

                           window.alert('Here is your result : <strong>' + result + '</strong>');

                        }

                );



Arun Patidar

Avatar

Level 4

Sorry, my mistake, I dint mention it clearly.

My first query is:

When I am using this code:

$.post(

                           "/content/we-retail/ca/fr/jcr:content/",

                       {variableName : value},

                       function(result) {

                           window.alert('Here is your result : <strong>' + result + '</strong>');

                        }

                );

, in a ".js" file,

the data gets persisted to the node at this path, without a servlet. I am not sure how that is happening. Is there any default servlet provided by AEM which is being used? 

Avatar

Level 4

No, I just added the exact code and made a call like:

$.get(

                           "/bin/damUpload/video",

                       {comment : comment},

                       function(result) {

                           window.alert('Here is your result : <strong>' + result + '</strong>');

                        }

                );

it works, but when I changed the path to :/content/we-retail/ca/fr/jcr:content, it stopped working.

Avatar

Community Advisor

Obviously, this is the expected behaviour in this case , as you are actually doing a "POST" to a resource path. If the resource path points to a custom servlet , or has a servlet registered for its resourceType , then your custom servlet will be executed. Otherwise it will be treated a default POST call and all your data will get saved to the node you mentioned.