AEM 6.3: Error 500 while doing a POST request | Community
Skip to main content
Level 4
November 21, 2017
Solved

AEM 6.3: Error 500 while doing a POST request

  • November 21, 2017
  • 18 replies
  • 12424 views

I have created a POST servlet like below:

package com.aem.sites.servlets;

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

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

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

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

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.aem.sites.interfaces.SubscriptionConfiguration;

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

@Component(

immediate = true,

service = Servlet.class,

configurationPid = "com.aem.sites.servlets.SubscriptionSevlet",

property = {

"sling.servlet.methods=POST",

"sling.servlet.selectors=newsletters",

"sling.servlet.resourceTypes=aemsite-project/components/structure/page",

"sling.servlet.extensions=html"

}

)

@Designate(ocd=SubscriptionConfiguration.class)

public class SubscriptionSevlet extends SlingAllMethodsServlet{

/**

*

*/

private static final long serialVersionUID = 1L;

/** The Constant logger. */

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Override

protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse res) throws IOException, ServletException {

logger.info("======================inside do post for subscription servlet========================================");

}

/**

* Activate.

*

* @param config the config

*/

@Activate

    @Modified

    protected void Activate(SubscriptionConfiguration config) {

logger.info("********************************inside subscription servlet*****************************************"+config.sampleProperty());

    }

}

and this is the configuration Interface

package com.aem.sites.interfaces;

import org.osgi.service.metatype.annotations.AttributeDefinition;

import org.osgi.service.metatype.annotations.AttributeType;

import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name="Subscription Configuration", description="Configuration interface for Subscription Servlet")

public @interface SubscriptionConfiguration {

@AttributeDefinition(

        name = "Sample property",

        description = "Sample property",

        type = AttributeType.STRING

    )

public String sampleProperty() default "sample test";

}

I am calling the servlet using resourceType and selector.The servlet is being called through a form:

<div id="login-box" style="padding-top:5%;padding-left:3%">

  <div class="left">

    <h1>Sign up</h1>

    <form method="POST" name="subscriptionForm" id="subscriptionForm" action="/content/aemsite/en/subscribe.newsletters.html">

    <input type="text" name="name" placeholder="Name" style="width:30%" />

    <input type="text" name="email" placeholder="E-mail" style="width:30%"/>

    <input type="submit" name="signup_submit" value="Sign me up" />

    </form>

  </div>

</div>

this is the JS being included through clientlibs

$(function() {

var formURL = $("#subscriptionForm").attr('action');

var method = $("#subscriptionForm").attr('method');

$("#subscriptionForm").submit(function(event) {

event.preventDefault();

$.ajax({

url:formURL,

data: $("#subscriptionForm").serialize(),

type: method,

success: function( data, textStatus, jQxhr ){

        $('#response pre').html( data );

    },

    error: function( jqXhr, textStatus, errorThrown ){

        console.log( errorThrown );

    }

});

});

});

The clientlibs that are being included as dependencies are given in the screenshot clientlibs.png below

The above is the error I am seeing on the chrome debuuger is  "Failed to load resource: the server responded with a status of 500 (Server Error)"

The screenshot is given below:

I have read that from AEM 6 onwards more security measures are being adopted for POST request and hence a CSRF token is being used but it is mostly handled if AEM's version of jQuery is used.

I have also used jcrresolver to check if the path is being resolved and here is the screenshot attached of that:

The servlet's status is also active.

I am not sure what is it that I am doing incorrectly. Any help is appreciated.

Thanks

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

I just tested Mail List Component solution. It works nicely --

Data was posted to Servlet and put into the database

18 replies

Level 4
November 22, 2017

The path used in action is /content/aemsite/en/subscribe.newsletters.html while the page that has the POST form is http://localhost:4502/editor.html/content/aemsite/en/subscribe.html  Since this is an AJAX call I have selected the path in action to be /content/aemsite/en/subscribe.newsletters.html while 'newsletters' is the selector. Is there something I am doing wrong here ?

Level 4
November 22, 2017

Update - When I use the path instead of resourceType, it works fine.

joerghoh
Adobe Employee
Adobe Employee
November 22, 2017

Sorry, but hardcoding the path in the servlet is no solution! Don't do that for production code.

My assumption is that the page at /content/aemsite/en/subscribe does not have the resourcetype "aemsite-project/components/structure/page" as specified in the annotations of the servlet. Fix that and it will work!

Jörg

Level 4
November 22, 2017

Never meant I found a solution. Was just an observation. The /content/aemsite/en/subscribe is made using an editable template and the resource type is set to aemsite-project/components/structure/page.

Level 4
November 22, 2017

And if the resourceType wasn't correct it would have never worked for GET request either.

joerghoh
Adobe Employee
Adobe Employee
November 22, 2017

That's true.

Anyway, the SlingPostServlet was used. Maybe you can confirm this by validating your request through the servlet resolver page in the webconsole (localhost:4502/system/console/servletresolver).

Looking again at your code, the annotations seems to be wrong ...

@Component(

immediate = true,

service = Servlet.class,

configurationPid = "com.aem.sites.servlets.SubscriptionSevlet",

property = {

"sling.servlet.methods=POST",

"sling.servlet.selectors=newsletters",

"sling.servlet.resourceTypes=aemsite-project/components/structure/page",

"sling.servlet.extensions=html"

}

)

I would expect something like this

@Compont(immediate=true)

@Service

@Properties({

  @Property(name="sling.servlet.methods" value="POST"),

  @Property(name="sling.servlet.selectors" value="newsletters"),

  @Property(name="sling.servlet.resourceTypes" value="aemsite-project/components/structure/page"),

  @Property(name="sling.servlet.extensions" value="html")

})

(or the use of the @SlingServlet annotation).

Can you check that?

Jörg

Level 4
November 22, 2017

I was looking to use OSGi DS annotations that's why I didn't use the @SlingServlet annotation. Do you think I should use the Felix SCR annotations for POSTrequest??

joerghoh
Adobe Employee
Adobe Employee
November 22, 2017

I haven't played that much with the DS annotations yet, I don't know from the top of my head if the property syntax is correct. When I used the Felix SCR annotations it worked for me very well. You might give it a try.

Jörg