Expand my Community achievements bar.

SOLVED

Sling Servlet vs OSGi Service vs Taglib

Avatar

Level 4

1) Sling Servlet

2) OSGi Service (good if you want to change any config on the fly through felix console)

3) Taglib

I know above three serve a different purpose but want to know exact use of each of those in my scenario.

I need to call an API which returns me a JSON response and then I need to traverse through JSON and show results on my page. I can achieve my target with any of the above but want to know which one is better and good in performance.

 

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Servlets are good to implement your use case. We have a community article that shows this exact use case:

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

This shows use of the Sling Servlet encoding the data as JSON and the AEM component parsing the JSON and displaying the results.  

Using taglibs does represent another way - but i would use a Sling Servlet/AJAX for your use case. 

View solution in original post

7 Replies

Avatar

Correct answer by
Level 10

Servlets are good to implement your use case. We have a community article that shows this exact use case:

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

This shows use of the Sling Servlet encoding the data as JSON and the AEM component parsing the JSON and displaying the results.  

Using taglibs does represent another way - but i would use a Sling Servlet/AJAX for your use case. 

Avatar

Level 4

Thanks Scott !!

But what if I use OSGi Service instead and let it call API. We can access OSGi service thru JSP and JS too.

Referring this article: http://helpx.adobe.com/experience-manager/using/restful-services.html

 

Please let me know, I am trying to find exact use case difference between sling servlet and OSGi service. or if both are same, is there any performance aspect?

Avatar

Level 10

You are 100% correct - as I pointed out in the Sling Servlet article - its really a matter of preference - there is no performance gain either way. When collecting data via a form - i typically recommend posting it to AEM via AJAX. However - you can also create a service and call - for example:

com.community.aem.CustomerService cs = sling.getService(com.community.aem.CustomerService.class);
 cs.<call a method exposed by the service>

Both serve the same purpose - getting data from a web page to an OSGi bundle where you can code app logic using Java. 

Avatar

Level 2

smacdonald2008, Would you mind articulating the difference between using OSGI and SLING to achieve something?

1.)Will it be right to say that all Sling Servlets are OSGI bundles?

or

2.) Is it more related to using appropriate layers of web to achieve Dependency Management and to control the way this service / functionality (of the servlet) is exposed to outside world.

or

3.) Is it more of a verbal turmoil where the abstractions and perspectives are not understood as it is.? Like Say "Servlet serves the request (URL) and Service serves the Servlet; Hence, Final result is serve the request (page) to the user (browser)."

Apologies for the inconvenience caused;

Avatar

Level 10

Sling Servlets are always implemented as OSGi bundles. When you create a Sling Servlet using DS Annotations - you define it as:

@Component(service=Servlet.class,

        property={

                Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

                "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                "sling.servlet.paths="+ "/bin/invokeWF"

           })

public class SimpleServlet extends SlingSafeMethodsServlet {

Notice the use of @Component.

In contrast - you can define a Sling Servlet like this using Felix SRC Annotations:

@SlingServlet(paths="/bin/mySearchServlet", methods = "POST", metatype=true)

public class HandleClaim extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

Notice the use of @SlingServlet

However - either way - they are always implemented as OSGi bundles and must be in Active State.

Avatar

Level 2

Thanks smacdonald2008​.

So I understand that there is no real meaning when someone says "create an OSGI service" or "write an OSGI service". Servlets are the implicit and inherent way in which URLs requests are served in AEM although managed by OSGI for DM and Configuration purpose like passing params to the Servlet (when metatype=true).

am I correct?

Avatar

Level 10

Correct - Servlets are written using @Component (using DS Annotations) and must be running within an OSGi bundle. See this new article that shows an example of writing a Servlet that calls a workflow.

Scott's Digital Community: Invoking an Adobe Experience Manager Custom 6.4 Workflow from a web page