You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2013/03/07 13:17:03 UTC

svn commit: r1453801 - /sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext

Author: fmeschbe
Date: Thu Mar  7 12:17:03 2013
New Revision: 1453801

URL: http://svn.apache.org/r1453801
Log:
SLING-2777 Add sample servlets with Felix SCR annotations (thanks Radu Cotescu for the patch)

Modified:
    sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext

Modified: sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext?rev=1453801&r1=1453800&r2=1453801&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext (original)
+++ sling/site/trunk/content/documentation/the-sling-engine/servlets.mdtext Thu Mar  7 12:17:03 2013
@@ -17,15 +17,54 @@ Servlets can be registered as OSGi servi
 
 A `SlingServletResolver` listens for `Servlet{`}services and - given the correct service registration properties - provides the servlets as resources in the (virtual) resource tree. Such servlets are provided as `ServletResource` instances which adapt to the `javax.servlet.Servlet` class.
 
-For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, the either or both of the `sling.servlet.paths` or the `sling.servlet.resourceTypes` service reference properties must be set. If neither is set, the Servlet service is ignored.
+For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either or both of the `sling.servlet.paths` or the `sling.servlet.resourceTypes` service reference properties must be set. If neither is set, the Servlet service is ignored.
 
 Each path to be used for registration - either from the `sling.servlet.paths` property or constructed from the other `sling.servlet.\*` properties - must be absolute. Any relative path is made absolute by prefixing it with a root path. This prefix may be set with the `sling.servlet.prefix` service registration property. If this property is not set, the first entry in the `ResourceResolver` search path for the `ResourceResolver.getResource(String)` method is used as the prefix. If this entry cannot be derived, a simpe slash - `/` \- is used as the prefix.
 
 If `sling.servlet.methods` is not specified, the servlet is only registered for handling GET requests. Make sure to list all methods you want to be handled by this servlet.
 
+#### Registering a Servlet using Java Annotations
+
+If you are working with the default Apache Sling development stack you can use Java Annotations from [Apache Felix Maven SCR Plugin](http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin.html) to describe the binding details for your Sling servlets:
+
+1. The `@SlingServlet` annotation
+
+        :::java
+        @SlingServlet(
+            resourceTypes = "sling/servlet/default",
+            selectors = "hello",
+            extensions = "html",
+            methods = "GET")
+        public class MyServlet extends SlingSafeMethodsServlet {
+
+            @Override
+            protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
+                ...
+            }
+        }
+
+2. The `@Properties` and `@Property` annotations
+
+        :::java
+        @Component(metatype = true)
+        @Service(Servlet.class)
+        @Properties({
+            @Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
+            @Property(name = "sling.servlet.selectors", value = "hello"),
+            @Property(name = "sling.servlet.extensions", value = "html"),
+            @Property(name = "sling.servlet.methods", value = "GET")
+        })
+        public class MyServlet extends SlingSafeMethodsServlet {
+
+            @Override
+            protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
+                ...
+            }
+        }
+
 ### Automated tests
 
-The [launchpad/test-services]({{ refs.http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/.path }}) module contains test servlets that use various combinations of the above properties. 
+The [launchpad/test-services]({{ refs.http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/.path }}) module contains test servlets that use various combinations of the above properties.
 
 The [launchpad/integration-tests]({{ refs.http://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests/.path }}) module contains a number of tests (like the [ExtensionServletTest|http://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolution/ExtensionServletTest.java] for example) that verify the results.
 
@@ -124,4 +163,4 @@ In this case, the servlet is only select
 
 ## Error Handler Servlet(s) or Scripts
 
-Error handling support is now described on the [Errorhandling]({{ refs.errorhandling.path }}) page.
\ No newline at end of file
+Error handling support is now described on the [Errorhandling]({{ refs.errorhandling.path }}) page.