What is the difference between ServletService
and PoorServlet
in general? When to use just a servlet and when to define a servlet as OSGI Service?
PoorServlet
:
public class PoorServlet extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}
}
ServiceServlet
:
@Properties({
@Property(name="service.pid", value="mycompany.ServiceServlet",propertyPrivate=false),
@Property(name="service.description",value="Service servlet", propertyPrivate=false),
@Property(name="service.vendor",value="mycompany", propertyPrivate=false)
})
public class ServiceServlet extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}
}
PoorServlet
is not an OSGi service and therefore won't be registered in the Sling servlet engine. As a result it won't be available under any path. It is a simple POJO and Sling don't know anything about it.
ServiceServlet
in your example is not really an OSGi component as well. In order to make it OSGi component, you should use add @Component
and @Service
annotations. Then you can add sling.servlet.paths
property which should contain the servlet path or paths. After these modifications, you can access your servlet under appropriate path.
There is also useful @SlingServlet
annotation which can be used instead of @Component
, @Service
and all servlet-related properties:
@SlingServlet(paths="/bin/my/path")
public class ServiceServlet extends SlingAllMethodsServlet {
...
Please find more information in the Sling documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.