访问CQ5中的Sling Servlet

时间:2014-04-18 21:08:01

标签: servlets cq5 sling

我们在CQ5中创建了一个Simple Sling Servlet作为OSGI包(使用cqblueprits / maven / eclipse)。

    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!");
    }
}

如果浏览器使用浏览器

请求购买
    http://localhost:4502/bin/acme/my/servlet

我收到错误

    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

在CQ的错误日志中,我们可以看到

    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

我无法在CQ中部署任何自定义SlingServlet。用于使用servlet创建捆绑包我正在使用Eclipse / Maven / cqblueprints组合。

请给我们一些解决问题的指示。

2 个答案:

答案 0 :(得分:3)

如果发布的上述代码是您的整个servlet代码,那么由于缺少导入,它甚至可能无法编译。

如果所有导入都正确并且servlet编译正常,则检查该组件是否在Felix控制台的/system/console/components下注册,并且处于活动状态。

由于提到的servlet路径是/ bin / acme / my / servlet,因此您无需在脚本解析器中注册路径,因为/ bin /默认情况下已注册。但是如果您使用的是自定义路径,例如/ apps / xyz / abc,请确保您已在Apache Sling Servlet/Script Resolver and Error Handler

下的/system/console/configMgr配置中注册了路径

答案 1 :(得分:0)

您为servlet定义了html扩展名:

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

您需要使用此扩展来访问servlet:

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