Monday 10 June 2013

Submitting Adobe Experience Manager form data to custom Sling Servlets

You can create an Adobe Experience Manager application that lets a user enter data into a web page and post data to a custom Sling Servlet. The posted data is processed by a custom Sling Servlet. In this development article, the Sling Servlet is created by using Maven.

An end user filling in a CQ form and posting the data to a Sling Servlet

The Sling Servlet that is created encodes the submitted form data into JSON formatted data and returns the data to the web client where it is displayed.

A web application displaying JSON formatted data returned by a Sling Servlet

A custom Sling Servlet is an OSGi bundle. However, a difference between an OSGi bundle that contains a service and an OSGi bundle that contains a Sling Servlet is the former requires that you create an instance of the service. For example, assume that an OSGi bundle contains a service based on a Java class named com.adobe.cq.CustomerService. To get data from the client web page to this OSGi service, you have to create an instance of com.adobe.cq.CustomerService, as shown in this example.

com.adobe.cq.CustomerService cs = sling.getService(com.adobe.cq.CustomerService.class);

Then you invoke a service method, as shown in this example that invokes the injestCustData method.

cs.injestCustData(first, last, phone, desc) ;

For information about how to create an Adobe CQ application that builds an OSGi bundle that contains a service (not a Sling Servlet), see Querying Adobe Experience Manager Data using the JCR API.

In contrast, when working with an OSGi bundle that contains a Sling Servlet, you post data to the Sling Servlet's doPost method. That is, you can use a JQuery AJAX request to post data to the Sling Servlet, as shown in the following example.

//Use JQuery AJAX request to post data to a Sling Servlet
 $.ajax({
         type: 'POST',    
         url:'/bin/mySearchServlet',
         data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,
         success: function(msg){
           alert(msg); //display the data returned by the servlet
         }
     });

This article discusses how to use Maven to develop the Sling Servlet, how to deploy it, and then how to post data to it from a client web page.

To read this development article, click:

http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

To watch the video, click:



Join the Adobe Experience Cloud Community 

Join the Adobe Experience Cloud Community by clicking this banner




I (Scott Macdonald) am a Senior Digital Marketing Community Manager at Adobe Systems with 20 years in the high tech industry. I am also a programmer with knowledge in Java, JavaScript, C#,C++, HTML, XML and ActionScript. If  you would like to see more CQ or other Adobe Digital Marketing end to end articles like this, then leave a comment and let me know what content you would like to see.


TwitterFollow the Digital Marketing Customer Care team on Twitter @AdobeExpCare.

YouTube: Subscribe to the AEM Community Channel