Can we call a background service in AEM through normal html page | Community
Skip to main content
arjuns71585267
Level 4
February 13, 2019
Solved

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

  • February 13, 2019
  • 11 replies
  • 6764 views

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.
Best answer by antoniom5495929

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

arjuns71585267
Level 4
February 15, 2019

Thanks. I am kind of confused beginner who knows things here and there! I hope wknd tutorial is something with a predefined order for learning