3
votes

We have created a Simple Sling Servlet as an OSGI bundle (using cqblueprits/maven/eclipse) in CQ5.

    import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

@SlingServlet(methods = { "GET" }, paths = { "/bin/acme/my/servlet" }, extensions = { "html" })
public class HelloWorld extends SlingSafeMethodsServlet {
    private static final long serialVersionUID = 217529099388338070L;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,
            IOException {
       response.setContentType("text/plain");
       response.getOutputStream().print("Hello AbsoluteUrlServlet World!");
    }
}

Buy if request from the browser using browser

    http://localhost:4502/bin/acme/my/servlet

I'm getting an error

    No resource found

Cannot serve request to /bin/acme/my/servlet in /libs/sling/servlet/errorhandler/404.jsp
Request Progress:

  0 (2014-04-18 13:53:24) TIMER_START{Request Processing}
  0 (2014-04-18 13:53:24) COMMENT timer_end format is {<elapsed msec>,<timer name>} <optional message>
  0 (2014-04-18 13:53:24) LOG Method=GET, PathInfo=/bin/acme/my/servlet
  0 (2014-04-18 13:53:24) TIMER_START{ResourceResolution}
  0 (2014-04-18 13:53:24) TIMER_END{0,ResourceResolution} URI=/bin/acme/my/servlet resolves to Resource=NonExistingResource, path=/bin/acme/my/servlet
  0 (2014-04-18 13:53:24) LOG Resource Path Info: SlingRequestPathInfo: path='/bin/acme/my/servlet', selectorString='null', extension='null', suffix='null'
  0 (2014-04-18 13:53:24) TIMER_START{ServletResolution}
  0 (2014-04-18 13:53:24) TIMER_START{resolveServlet(NonExistingResource, path=/bin/acme/my/servlet)}
  1 (2014-04-18 13:53:24) LOG {0}: no servlet found
  1 (2014-04-18 13:53:24) TIMER_END{1,resolveServlet(NonExistingResource, path=/bin/acme/my/servlet)} Using servlet org.apache.sling.servlets.get.DefaultGetServlet
  1 (2014-04-18 13:53:24) TIMER_END{1,ServletResolution} URI=/bin/acme/my/servlet handled by Servlet=org.apache.sling.servlets.get.DefaultGetServlet
  1 (2014-04-18 13:53:24) LOG Applying Requestfilters

In Error log of CQ, we can see

    18.04.2014 14:19:53.879 *INFO* [0:0:0:0:0:0:0:1 [1397855993874] GET /apps/path/to/my/servlet HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /apps/path/to/my/servlet not found

I'm unable to deploy any custom SlingServlet in CQ. for Crating the the bundle with servlet I'm using Eclipse/Maven/cqblueprints combination.

Please give us some pointers to solve the issue.

2

2 Answers

3
votes

If the above code posted is your entire code of the servlet, then it might not even compile due to missing imports.

In case all the imports are correct and the servlet compiles fine, then check whether the component is registered under /system/console/components in the Felix Console and is in active state.

Since the servlet path mentioned is /bin/acme/my/servlet, you needn't register the path in script resolver as /bin/ would have been registered by default. But in case you are using custom paths such as /apps/xyz/abc, then make sure you have registered the path in the Apache Sling Servlet/Script Resolver and Error Handler configuration available under /system/console/configMgr

0
votes

You defined html extension for your servlet:

@SlingServlet(
  methods = { "GET" },
  paths = { "/bin/acme/my/servlet" },
  extensions = { "html" })  // <- here

You need to use this extension to access the servlet:

http://localhost:4502/bin/acme/my/servlet.html