Expand my Community achievements bar.

SOLVED

What is a good path for a Servlet?

Avatar

Level 6

Hi here,

So my servlet got a path "/bin/...."  copying a post from Adobe help.

But inside Dispatcher

bin is not allowed, of course I can allow it. But ....

what path would be OK for a servlet?

1 Accepted Solution

Avatar

Correct answer by
Level 10

WHen using paths - (which is a valid SLing way- https://sling.apache.org/documentation/the-sling-engine/servlets.html) - you can invoke the Servlet without accessing a JCR node.

That is - you can use an AJAX request or write HTTP GET/POSTs. All of these are accomplished without accesing a JCR node. So when you want to invoke a servlet from outside of AEM (a web client or desktop app that does not access a JCR node or using an AJAX request) - binding by path is the way to do it. 

Here is an example of a path - under bin - and an AJAX call invoking the servlet:

$('#submit').click(function() {
    var failure = function(err) {
             alert("Unable to retrive data "+err);
   };
  
    //Get the user-defined values that represent claim data to persist in the Adobe CQ JCR
    var myFirst= $('#FirstName').val() ; 
    var myLast= $('#LastName').val() ; 
    var date= $('#DateId').val() ; 
    var cat= $('#Cat_Id').val() ; 
    var state= $('#State_Id').val() ; 
    var details= $('#Explain').val() ; 
    var city= $('#City').val() ; 
    var address= $('#Address').val() ; 
    var claimId = createUUID(); 
  
  
    //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){
 
           var json = jQuery.parseJSON(msg); 
            var msgId=   json.id;
            var lastName = json.lastname;
            var firstName = json.firstname;
              
            $('#ClaimNum').val(msgId); 
            $('#json').val("Filed by " + firstName + " " + lastName);   
         }
     });
  });

 

In Dispatcher - you can allow access to this path. See:

https://docs.adobe.com/docs/en/cq/5-6-1/deploying/security_checklist.html

That way - AJAX requests will work

View solution in original post

6 Replies

Avatar

Level 2

Hi Xena,

It's recommended to rely on the resource type resolution instead. This way you can benefit from the Sling and JCR features including the security model.

[1] shows you how to define the resource type in your Servlet implementation.

Hope that helps,

[1] https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/ad...

Alex

Avatar

Level 6

hi Alex, thank you, i know it... but

my Servlet with resourceType didn't work, yesterday we discussed it here:

 

http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

 

Could you please have a look and tell me your trick?

Avatar

Level 2

Xena,

So once your Servlet that defines a resource type is deployed, you need to create a node in your repository with the sling:resourceType property set to your resourceType.

You can then call your Servlet by hitting the path of your node.

Have you tried that?

Cheers,

Alex

Avatar

Level 6

yes i tried it.

but the logout button is inside a component, and components don't allow sling:resourceType property.

So why the original code of this project  had @Property(name="resourceTypes", value="sling/servlet/default")

I also changed 

@Component(enabled = true)
@Service(Servlet.class)
 

to

@SlingServlet

 

didn't help.

Avatar

Correct answer by
Level 10

WHen using paths - (which is a valid SLing way- https://sling.apache.org/documentation/the-sling-engine/servlets.html) - you can invoke the Servlet without accessing a JCR node.

That is - you can use an AJAX request or write HTTP GET/POSTs. All of these are accomplished without accesing a JCR node. So when you want to invoke a servlet from outside of AEM (a web client or desktop app that does not access a JCR node or using an AJAX request) - binding by path is the way to do it. 

Here is an example of a path - under bin - and an AJAX call invoking the servlet:

$('#submit').click(function() {
    var failure = function(err) {
             alert("Unable to retrive data "+err);
   };
  
    //Get the user-defined values that represent claim data to persist in the Adobe CQ JCR
    var myFirst= $('#FirstName').val() ; 
    var myLast= $('#LastName').val() ; 
    var date= $('#DateId').val() ; 
    var cat= $('#Cat_Id').val() ; 
    var state= $('#State_Id').val() ; 
    var details= $('#Explain').val() ; 
    var city= $('#City').val() ; 
    var address= $('#Address').val() ; 
    var claimId = createUUID(); 
  
  
    //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){
 
           var json = jQuery.parseJSON(msg); 
            var msgId=   json.id;
            var lastName = json.lastname;
            var firstName = json.firstname;
              
            $('#ClaimNum').val(msgId); 
            $('#json').val("Filed by " + firstName + " " + lastName);   
         }
     });
  });

 

In Dispatcher - you can allow access to this path. See:

https://docs.adobe.com/docs/en/cq/5-6-1/deploying/security_checklist.html

That way - AJAX requests will work

Avatar

Level 6

Hi

thank you.

Yes I used http://localhost:4502/system/console/jcrresolver  to check the servlet, it returned a correct mapping

Ajax fine.

in Dispatcher I allowed the whole path to the servlet.

This way the login worked.