Can we call a background service in AEM through normal html page | Community
Skip to main content
arjuns71585267
Level 4
Feb 13, 2019 2:29 PM UTC
Solved

Can we call a background service in AEM through normal html page

I want to capture data from my html form and post it to a jsp servlet residing in AEM. Is this possible? Also is it possible to call a OSGI method which may be running as a background service directly from an HTML page?

I am getting error:500 when I try to post data into a jsp file which is residing in AEM. The following image shows my error.

Please help.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
antoniom5495929
antoniom5495929Accepted solution
Level 7
Feb 14, 2019 10:30 AM UTC

Hi,

you just only need to create your HTML without using jsp.

In your components you could insert an HTML like the following:

<form id="myform">

  ${properties.firstname}:<br>

  <input type="text" name="firstname" value="">

  <br>

  ${properties.lastname}:<br>

  <input type="text" name="lastname" value="">

  <br><br>

  <input class="action" type="submit" value="${properties.submit}">

</form>

Inside your clientlibs you could use ajax in order to get data and then send this data to your servlet:

function attachEvent(){

$(".action").click(function(e) {

    e.preventDefault();

    sendDataToAEM();

    });

}

function sendDataToAEM() {

var dataForm = $("#myform").serializeArray();

    $.post("/content/we-retail.aemtestservice.html", dataForm);

}

$(document).ready(function() {

attachEvent();

});

And in java you need to create your servlet that get your data and then call your service:

@SlingServlet(resourceTypes = {"cq:Page"}, methods = {"GET,POST"},

  selectors = {"aemtestservice"},

  extensions = {"html"})

public class AEMTestServiceServlet extends SlingAllMethodsServlet {

   private static final Logger LOGGER = LoggerFactory.getLogger(AEMTestServiceServlet.class);

   @Reference
   private HelloService helloService;

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

  String data = request.getParameter("firstname");

   //...
   helloService.mymethod();

   //...
   }

Let us know.

Thanks,

Antonio

11 replies

smacdonald2008
Level 10
Feb 13, 2019 2:36 PM UTC

"I want to capture data from my html form and post it to a jsp servlet residing in AEM."

You can post Form data to a post.POST.jsp and from that to a OSGi custom service where you can use Java logic to process that data to meet your business requirements.

In this example - as an example of custom Java logic - we stored the form data in the JCR.

Adobe Experience Manager Help | Persisting Adobe Experience Manager 6.4 JCR data using a Custom Form Action

Translate