In Sling, Servlets can be registered as OSGi services like below:
1. The
1. The
@SlingServlet annotation@SlingServlet(resourceTypes = "sling/servlet/default",selectors = "hello",extensions = "html",methods = "GET")public class MyServlet extends SlingSafeMethodsServlet { //TO DO
}
2. The
@Properties and @Property annotations@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 {@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
//TO DO
}}
3. Registering the servlet by path
@SlingServlet(paths={"/bin/customservlet/hashim"})@Properties({@Property(name="service.pid", value="com.day.servlets.SampleServlet",propertyPrivate=false),@Property(name="service.description",value="SampleDescription", propertyPrivate=false),@Property(name="service.vendor",value="SampleVendor", propertyPrivate=false)})public class SampleServlet extends SlingAllMethodsServlet{@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
//TO DO
}@Overrideprotected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
//TO DO
}}
4. Register servlet by Resource Type
@SlingServlet(resourceTypes = {"rep:User"},methods = {"GET", "POST"})@Properties({@Property(name="service.pid", value="com.day.servlets.SampleServlet",propertyPrivate=false),@Property(name="service.description",value="SampleDescription", propertyPrivate=false),@Property(name="service.vendor",value="SampleVendor", propertyPrivate=false)})public class SampleServlet extends SlingAllMethodsServlet{@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
//TO DO
}@Overrideprotected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
//TO DO
}}
Note: If you want your @SlingServlet to fetch some properties from Felix Console Configurations using @Properties , add the parameter “metatype=true” in the form of declaration where @SlingServlet is used.
This parameter is responsible for a Service component to be available in Felix Console configMgr.
why resourceType is much more prefered for writing SlingServlets. Below are the reasons:
- While defining a path , you must be specific what all paths are allowed to be used in the ServletResource OSGi service. If you define something randomly, your servlet might not be fucntional. Only a limited paths are allowed and the rest are blocked unless you open them up. This is resolved using resourceType.
- You may have also configure the dispatcher , if you use some random path for your servlet. This might be a potential security threat and a needless configuration.
- You might also have to specify the paths to your consumers for your servlet and any change in that path could have a serious affect. This might not be the case when you use resourceType