OSGI R6 servlet paths list property | Community
Skip to main content
New Participant
August 13, 2018
Solved

OSGI R6 servlet paths list property

  • August 13, 2018
  • 16 replies
  • 10864 views

I am trying to use multiple paths in servlet. I am able to achieve this using below syntax:

@Component(service=Servlet.class,

name="Sample Servlet",

property={

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

"sling.servlet.paths="+ "/bin/path1",

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

})

Is there a way to improve above annotation so that I don't have to define paths multiple time.Instead I should be able to set it as a list or with REGEX pattern?

I tried below syntax but seems it is not working:

@Component(service=Servlet.class,

name="Sample Servlet",

property={

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

"sling.servlet.paths="+ "/bin/path*"

})

@Component(service=Servlet.class,

name="Sample Servlet",

property={

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

"sling.servlet.paths="+ "[/bin/path1,/bin/path2]"

})

Please share the various options for above.

Thanks,
Rajeev

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 joerghoh

You clearly need to separate the client-side part from the server-side part. On the client-side you need to know which endpoint to call in what situation, but this is the case for both approaches. In your case I even wonder if you need to have 2 distinct URLs, but you should be able to handle the details of the submission based on request parameters.

You need to map a servlet to a Sling resource via the sling:resourceType property. Then you don't need to hardcode any path in the servlet annotations itself.

I don't know if you can combine path-based bindings and selectors/extensions (haven't tried it). But you can always use URL parameters with path-based bindings (and cacheability isn't a topic here).

But still, I recommend to use resourcetype-based binding. Because then your form can be self-contained in a way, that

GET /.../form.html

will render the form, while

POST /.../form.submission

will invoke the handling of the form submission. When you build the form, you don't need to hardcode any knowledge about other functions (like the path to the submission servlet) into the form rendering logic. Also if you annotate the form which kind of submission you want to use, this can be easily determined by the submission servlet. In most cases this means that all settings regarding the form and the submission logic applying to this specific form instance can and should be made on the form itself. Without any duplication.

(Maybe I should do a blog post about it ...)

16 replies

New Participant
August 13, 2018

These paths are limited but I don't want to write "sling.servlet.paths" line for each path added for controller. I want to define it in an array or using regex.

Jitendra_S_Toma
New Participant
August 13, 2018

Hi Rajeev,

To differentiate multiple requests based on paths is one way to solve the problem, however, selectors or query parameter can serve the purpose.

Are the paths you are predicting not limited?

New Participant
August 13, 2018

We are trying to call single servlet as controller for 2 separate paths and then trying to delegate the request to appropriate service based on path from which the request came.

Thanks,

Rajeev

arunpatidar
New Participant
August 13, 2018

Hi Rajiv,

You can't use pattern while registering servlet using path or resourceType.

In case you want to use servlet for many paths Slings provide option to create default servlet.

Apache Sling :: Servlets and Scripts

but for this you need to use particular selector e.g.

  • model selector which convert all the request in JSON.
  • offline.doc selector+extension which converts page into doc
  • export.zip selector+extension which export page as zip.

You can do something like below

@Component(service=Servlet.class,

name="Sample Servlet",

property={

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

"sling.servlet.resourceTypes="+ "sling/servlet/default",

"sling.servlet.selectors = mypathselector"

})

Thanks

Arun

Arun Patidar
Ratna_Kumar
New Participant
August 13, 2018

Hi Rajeev,

As Scott stated, please use the static value for path property. I don't see anywhere using multiple paths in sling servlet.

See this docs: Apache Sling :: Servlets and Scripts

Hope this helps!!

Thanks,

Ratna Kumar.

smacdonald2008
New Participant
August 13, 2018

I have always used a static value for a path prop for a Servlet - which works well.  What benefit is there to try and use multiple values like this?