You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Andreas Kollegger <ak...@tembopublic.org> on 2009/05/06 23:12:10 UTC
Registering Servlets and Filters
With the current "Servlet Resolution" wiki page out-of-sync, does
anyone have a bare-bones example of registering a servlet and/or
filter with the Sling framework? It looks like a bundle just has to
register a javax.servlet.Servlet service with the proper sling
properties for it to get picked up. I've tried a simple OSGi
Activator, but can't seem to get it to work.
Example BundleActivator.start...
public void start( BundleContext bc )
throws Exception
{
Dictionary props = new Properties();
props.put("sling.servlet.paths", "/myservlet/html, /myservlet/txt");
bc.registerService( javax.servlet.Servlet.class.getName(),
new MyServlet(), props );
}
Thanks for any guidance,
Andreas
Re: Registering Servlets and Filters
Posted by Andreas Kollegger <ak...@tembopublic.org>.
Thanks to both Felix and Bertrand, servlets and filters are both now
working.
Now, I'm moving on to deploying sitemesh as a filter. An initial
challenge is that SiteMesh attempts to load configuration files using
the ServletContext, which doesn't seem to search the bundle that
loaded the filter. Is there a way to register search paths for the
shared ServletContext? Otherwise I'll look into hacking SiteMesh to
use alternative configuration methods.
Thanks,
Andreas
On May 7, 2009, at 3:02 AM, Felix Meschberger wrote:
> Hi,
>
> Andreas Kollegger schrieb:
>> With the current "Servlet Resolution" wiki page out-of-sync, does
>> anyone
>> have a bare-bones example of registering a servlet and/or filter with
>> the Sling framework? It looks like a bundle just has to register a
>> javax.servlet.Servlet service with the proper sling properties for
>> it to
>> get picked up. I've tried a simple OSGi Activator, but can't seem
>> to get
>> it to work.
>>
>> Example BundleActivator.start...
>>
>> public void start( BundleContext bc )
>> throws Exception
>> {
>> Dictionary props = new Properties();
>> props.put("sling.servlet.paths", "/myservlet/html, /myservlet/
>> txt");
>> bc.registerService( javax.servlet.Servlet.class.getName(), new
>> MyServlet(), props );
>> }
>
> This is almost correct. The property value is either a single string
> or
> an array of strings. So your example should really be:
>
> props.put("sling.servlet.paths",
> new String[]{ "/myservlet/html", "/myservlet/txt" });
>
> But since you registered with absolute paths, the resource types must
> also be absolute. Thus your example (with the fix for the property
> value) should work for .html and .txt requests to resources whose
> resource type "/myservlet".
>
> If your resource types are relative -- e.g. "myservlet" -- you should
> register with relative paths as in :
>
> props.put("sling.servlet.paths",
> new String[]{ "myservlet/html", "myservlet/txt" });
>
> or better yet to register with two properties, one for the resource
> type
> and for the extensions supported:
>
> props.put("sling.servlet.resourceTypes", "myservlet");
> props.put("sling.servlet.extensions", new String[]{"html","txt"});
>
>
> HTH
>
> Regards
> Felix
>
>>
>> Thanks for any guidance,
>> Andreas
>>
>>
Re: Registering Servlets and Filters
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
Andreas Kollegger schrieb:
> With the current "Servlet Resolution" wiki page out-of-sync, does anyone
> have a bare-bones example of registering a servlet and/or filter with
> the Sling framework? It looks like a bundle just has to register a
> javax.servlet.Servlet service with the proper sling properties for it to
> get picked up. I've tried a simple OSGi Activator, but can't seem to get
> it to work.
>
> Example BundleActivator.start...
>
> public void start( BundleContext bc )
> throws Exception
> {
> Dictionary props = new Properties();
> props.put("sling.servlet.paths", "/myservlet/html, /myservlet/txt");
> bc.registerService( javax.servlet.Servlet.class.getName(), new
> MyServlet(), props );
> }
This is almost correct. The property value is either a single string or
an array of strings. So your example should really be:
props.put("sling.servlet.paths",
new String[]{ "/myservlet/html", "/myservlet/txt" });
But since you registered with absolute paths, the resource types must
also be absolute. Thus your example (with the fix for the property
value) should work for .html and .txt requests to resources whose
resource type "/myservlet".
If your resource types are relative -- e.g. "myservlet" -- you should
register with relative paths as in :
props.put("sling.servlet.paths",
new String[]{ "myservlet/html", "myservlet/txt" });
or better yet to register with two properties, one for the resource type
and for the extensions supported:
props.put("sling.servlet.resourceTypes", "myservlet");
props.put("sling.servlet.extensions", new String[]{"html","txt"});
HTH
Regards
Felix
>
> Thanks for any guidance,
> Andreas
>
>
Re: Registering Servlets and Filters
Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,
On Wed, May 6, 2009 at 11:12 PM, Andreas Kollegger
<ak...@tembopublic.org> wrote:
> With the current "Servlet Resolution" wiki page out-of-sync, does anyone
> have a bare-bones example of registering a servlet and/or filter with the
> Sling framework?...
The http://svn.apache.org/repos/asf/incubator/sling/trunk/launchpad/test-services/
example bundle contains a few servlets using different registration
mechanisms.
For filters, there's an example at [1].
-Bertrand
[1] https://svn.eu.apache.org/repos/asf/incubator/sling/trunk/samples/simple-demo/src/main/java/org/apache/sling/sample/filter/ZipFilter.java