Expand my Community achievements bar.

SOLVED

Conceptual Question : When do we use servlets and when do we use sling models?

Avatar

Level 5

I was asked in an interview : When do we use servlets and when do we use sling models?

Can someone please help me out with the answer? Thanks!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@arindam6600 :

 

@SlingModel: 

Sling Models are "pure" POJOs which maps Sling objects (resources, request objects etc.).

 They allow you to map resource properties, assign default values, inject OSGi services and much more.

 

Predominently, if you want to use the property values in Sightly , you can use data-sly-use and call the slingmodel to retrive the property values, while retrieving you can make changes to the values if needed. 

We are using @Model annotation to specify that this model class is a Sling Model. Each data member is annotated with @Inject. This class will be mapped to a resource in JCR. 

 

 

 

@sling servlet : 

Sling servlet can be registered by using paths or resourceType.

 

Sling servlet are basically used when front end developers need to make ajax call and want to get response in form of json

2
3
4
5
6
7
8
9
10
11
12
13
$.ajax({
    type : "GET",
    url : '/bin/custom/path',
    /*data : {
        pass your request parameter here, currently we are not passing any data
    },*/
    success : function(data, textStatus, jqXHR) {
        //write your logic that you need to perform on sucess
                },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        //write your logic that you need to perform on error
    }
});

 

@SuppressWarnings("serial")
@SlingServlet(paths = "/bin/custom/path")
public class SimpleServlet extends SlingSafeMethodsServlet {
}
 

 

 

Thanks,
Siva

View solution in original post

2 Replies

Avatar

Community Advisor

@arindam6600 A Servlet is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. In AEM you can extend SlingSafeMethodsServlet for read-only implementation or SlingAllMethodsServlet in order to implement all RESTful operations. Sling Servlets has doGet(..) and doPost() methods.

 

Sling Models are annotation-driven Java POJOs that facilitate the mapping of data from the JCR to Java variables. In AEM you have Resouces.class and SlingHttpServletRequest.class as adaptable - When using Resource you can access the JCR resource without a UI in mind and when using request you can also access request-info. Sling model has @PostConstruct that gets called when the component is loaded.

 

Hope this helps!

Avatar

Correct answer by
Community Advisor

@arindam6600 :

 

@SlingModel: 

Sling Models are "pure" POJOs which maps Sling objects (resources, request objects etc.).

 They allow you to map resource properties, assign default values, inject OSGi services and much more.

 

Predominently, if you want to use the property values in Sightly , you can use data-sly-use and call the slingmodel to retrive the property values, while retrieving you can make changes to the values if needed. 

We are using @Model annotation to specify that this model class is a Sling Model. Each data member is annotated with @Inject. This class will be mapped to a resource in JCR. 

 

 

 

@sling servlet : 

Sling servlet can be registered by using paths or resourceType.

 

Sling servlet are basically used when front end developers need to make ajax call and want to get response in form of json

2
3
4
5
6
7
8
9
10
11
12
13
$.ajax({
    type : "GET",
    url : '/bin/custom/path',
    /*data : {
        pass your request parameter here, currently we are not passing any data
    },*/
    success : function(data, textStatus, jqXHR) {
        //write your logic that you need to perform on sucess
                },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        //write your logic that you need to perform on error
    }
});

 

@SuppressWarnings("serial")
@SlingServlet(paths = "/bin/custom/path")
public class SimpleServlet extends SlingSafeMethodsServlet {
}
 

 

 

Thanks,
Siva
The following has evaluated to null or missing: ==> liql("SELECT id, subject, body, depth, post_time, author.login, author.id, author.rank, metrics.views FROM messages WHERE topic.id = '${topicId}' AND is_solution = true").data.items[0] [in template "analytics-container" at line 82, column 31] ---- Tip: It's the final [] step that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign acceptedAnswer = liql("SELECT... [in template "analytics-container" at line 82, column 5] ----