creating servlet in CQ - cannot be cast to javax.servlet.Servlet , Failed getting the service for reference | Community
Skip to main content
October 16, 2015
Solved

creating servlet in CQ - cannot be cast to javax.servlet.Servlet , Failed getting the service for reference

  • October 16, 2015
  • 14 replies
  • 6056 views
Hi all,
 

I tried the following sample program for creating servlet in CQ but getting below error in logs:

 
07.10.2013 11:27:19.156 *WARN* [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver bindServlet: Failed getting the service for reference [javax.servlet.Servlet] java.lang.ClassCastException: com.adobe.training.core.MySafeMethodServlet cannot be cast to javax.servlet.Servlet
at org.apache.sling.servlets.resolver.internal.SlingServletResolver.createServlet(SlingServletResolver.java:974)
at org.apache.sling.servlets.resolver.internal.SlingServletResolver.bindServlet(SlingServletResolver.java:936)
 
Any reason why this is happening. Why its giving error like:
1)  Failed getting the service for reference 
2) cannot be cast to javax.servlet.Servlet
 
 
 
Below is the sample code.
-----------------------------------------------
 
package com.adobe.training.core;
import java.io.IOException;
 
 
import javax.servlet.ServletException;
import javax.servlet.Servlet;
 
import org.apache.felix.scr.annotations.Reference;
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(paths = "/bin/company/repo" , methods = "GET")
public class MySafeMethodServlet extends SlingSafeMethodsServlet  
{
private static final long serialVersionUID = -3960692666512058118L;
 
@Override 
 
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException , IOException
{
response.setHeader("Content-Type","application/json");
response.getWriter().print("{\"coming\" : \"soon\"}");
}
}
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ojjis

Hi,
have you tried to use the @Component annotation with it like this small example:

 

@Component(immediate = true, metatype = true, name = "com.adobe.training.core.MySafeMethodServlet", label = "Safe Methd Servlet", description = " A small test servlet") @SlingServlet(methods = { "GET" }, paths = "/bin/company/repo", generateComponent = false) public class MySafeMethodServlet extends SlingSafeMethodsServlet { private static final Logger log = LoggerFactory.getLogger(MySafeMethodServlet.class); protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response){ log.info("DoGetting"); } protected void activate(ComponentContext ctx) { log.info("Activating"); } protected void deactivate(ComponentContext ctx) { log.info("Deactivating"); } }

/Johan

14 replies

October 16, 2015

Hi all,

First of all thanks to all for jumping in and helping with suggestion. Finally this issue is resolved now.

Root Cause:

In my pom.xml I was using dependencies for Apache Sling and Servlet API jars. I had left the <scope> tag so by default maven takes <scope> as "compile". Due to this it was adding these JAR to the bundle jar. These jars (apache sling , servlet api) were conflicting with what we have on CQ Server. And due to this Servlet was not getting called.

If you remember I tried creating the bundle jar using CRX .bnd file and in that case my servlet was working fine. Then I tried comparing the bundle jar created by maven and one created using .bnd file. Observed that in my maven jar these 3 dependent jars were include which was not the case with the bundle jar created using .bnd file

Fix

I added <scope>provided</scope> to these dependencies and it created the bundle jars without these dependent jars in it. As you know "provided" scope in pom.xml means that these depedencies will be provided by the server where this bundle jar is getting installed. There is no need to include the dependent jars in bundle jar package.

For your reference below is the pom.xml

    <!-- Apache Sling Dependencies -->
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.api</artifactId>        
            <version>2.4.2</version>  
            <scope>provided</scope>
        </dependency>
        
        
        
        <!-- Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>    
            <version>2.5</version>  
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>            
            <version>2.0</version> 
            <scope>provided</scope>
        </dependency>
        

October 16, 2015

I tried creating the servlet using CRX by creating .bnd file. This worked fine and the servlet created out of it also works. I followed these steps.

http://therealcq.blogspot.com/2013/01/how-to-write-custom-slingservlet.html

Seems like when I try to create the OSGI bundle using my exclipse something is going wrong. 

One thing which I noticed was that when the servlet was created using .bnd file then in "Services" in OSGI Console I see additional service called:

 
    
[org.apache.sling.api.resource.ResourceProvider]
             
provider.roots/bin/nascar/rohit.servlet
/bin/nascar/rohit
Service DescriptionServletResourceProvider for Servlets at [/bin/nascar/rohit/ashwini.servlet, /bin/nascar/rohit]
Using Bundlesorg.apache.sling.jcr.resource (110)
org.apache.sling.bundleresource.impl (95)

This was not getting created in case of eclipse deployment 

Also the OSGI jar created using eclipse (maven build) contained these 3 jars inside it whereas the OSGI Jar created using .bnd file did not had these 3 jars:

jsp-api-2.0.jar
org.apache.sling.api-2.4.2.jar
servlet-api-2.5.jar

 

Also I have added following in the pom.xml

<!-- Apache Sling Dependencies -->
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.api</artifactId>        
            <version>2.4.2</version>  
        </dependency>
        
        
        
        <!-- Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>    
            <version>2.5</version>  
            
        </dependency>
        
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>            
            <version>2.0</version> 
            
        </dependency>

 

 

Level 2
October 16, 2015

Hi Rohit, You post is helped me today to resolve the exact problem. thanks.

smacdonald2008
Level 10
October 16, 2015

My advice would be use Maven to build the Sling Servlet as specified in this community article:

http://helpx.adobe.com/adobe-cq/using/custom-sling-servlets.html

Please follow each step in this article - it includes everything you need and has got other community members up and running with Sling Servlets. You do not need to use BDN file or CRXDE. Just Maven and POM. THen deploy to Adobe CQ. In this artilce - its only pulled into Eclipse to use as IDE to modify the POM and Java files. Maven compiles the code and builds the OSGi bundle.