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:



Adobe Digital Marketing Community

Join the Adobe Digital Marketing Community. Start by clicking this banner

About the Author

I (Scott Macdonald) am a Senior Digital Marketing Community Manager at Adobe Systems with over 16 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.

Linked Inhttp://www.linkedin.com/in/scottmacdonald2010

8 comments:

  1. Any reason why I am getting 403 Forbidden when using this method ?

    ReplyDelete
  2. It means that you have either not deployed the OSGi that contains the Servlet or you have not mapped the AJAX to the Path prop set in the annotation in the Java class.

    ReplyDelete
  3. Thanks for the comment, but that are the obvious things to do.
    Actually the problem was I was using Postman to send requests to the server and the ReferrerFilter kicked in.

    ReplyDelete
  4. i have implemented the bundle and the jason in the same way it was directed but still getting the following error please help
    java.lang.NoClassDefFoundError: org/json/simple/JSONObject
    at com.adobe.cq.sling.HandleClaim.doPost(HandleClaim.java:58)
    at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:148)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:508)
    at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:146)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:356)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:168)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:254)
    at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl.doFilter(AuthoringUIModeServiceImpl.java:301)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(Abs

    ReplyDelete
  5. This means that the OSGi bundle fragment that contains the JSON JAR is not active. Plase check that you placed this bunlde into an active state.

    ReplyDelete
  6. Not sure whether I am too late.
    I followed all the steps but the servlet is not getting invoked :(

    Any ideas ?

    ReplyDelete
  7. Website Development Virginia I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.

    ReplyDelete