What is a good path for a Servlet? | Community
Skip to main content
Xena_bcn
Level 5
October 16, 2015
Solved

What is a good path for a Servlet?

  • October 16, 2015
  • 6 replies
  • 2035 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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

6 replies

Adobe Employee
October 16, 2015

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/adobe/acs/samples/servlets/impl/SampleSafeMethodsServlet.java#L44

Alex

Xena_bcn
Xena_bcnAuthor
Level 5
October 16, 2015

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-manager.topic.html/forum__vcs8-_hi_my_logoutse.html

 

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

Adobe Employee
October 16, 2015

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

Xena_bcn
Xena_bcnAuthor
Level 5
October 16, 2015

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.

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

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

Xena_bcn
Xena_bcnAuthor
Level 5
October 16, 2015

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.