Skip to main content
arjuns71585267
Level 4
2月 13, 2019 2:29 下午 UTC
已解决

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.

此主题已关闭回复。
antoniom5495929
Level 7
2月 14, 2019 10:30 上午 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回复

arjuns71585267
Level 4
2月 15, 2019 9:16 上午 UTC

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

翻译