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
  • 6075 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 smacdonald2008

Could you please share your complete code for Sling Servlet which you are mentioning. I will try the same now.

October 16, 2015

Hi all,

Just for your info I am trying to access the servlet using below URL

http://localhost:4502/bin/company/repo

And below is the output I am getting:

 

No resource found

Cannot serve request to /bin/company/repo in /libs/sling/servlet/errorhandler/404.jsp

Request Progress:

0 (2013-10-07 12:20:41) TIMER_START{Request Processing} 0 (2013-10-07 12:20:41) COMMENT timer_end format is {<elapsed msec>,<timer name>} <optional message> 0 (2013-10-07 12:20:41) LOG Method=GET, PathInfo=/bin/company/repo 0 (2013-10-07 12:20:41) TIMER_START{ResourceResolution} 0 (2013-10-07 12:20:41) TIMER_END{0,ResourceResolution} URI=/bin/company/repo resolves to Resource=NonExistingResource, path=/bin/company/repo 0 (2013-10-07 12:20:41) LOG Resource Path Info: SlingRequestPathInfo: path='/bin/company/repo', selectorString='null', extension='null', suffix='null' 0 (2013-10-07 12:20:41) TIMER_START{ServletResolution} 0 (2013-10-07 12:20:41) TIMER_START{resolveServlet(NonExistingResource, path=/bin/company/repo)} 2 (2013-10-07 12:20:41) LOG {0}: no servlet found 2 (2013-10-07 12:20:41) TIMER_END{2,resolveServlet(NonExistingResource, path=/bin/company/repo)} Using servlet org.apache.sling.servlets.get.DefaultGetServlet 2 (2013-10-07 12:20:41) TIMER_END{2,ServletResolution} URI=/bin/company/repo handled by Servlet=org.apache.sling.servlets.get.DefaultGetServlet 2 (2013-10-07 12:20:41) LOG Applying Requestfilters 2 (2013-10-07 12:20:41) LOG Calling filter: org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter 2 (2013-10-07 12:20:41) LOG Calling filter: org.apache.sling.rewriter.impl.RewriterFilter 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.core.impl.WCMRequestFilter 2 (2013-10-07 12:20:41) LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.theme.impl.ThemeResolverFilter 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet 2 (2013-10-07 12:20:41) LOG Calling filter: org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter 2 (2013-10-07 12:20:41) LOG RedirectFilter did not redirect (request extension does not match) 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.core.impl.warp.TimeWarpFilter 2 (2013-10-07 12:20:41) LOG Applying Componentfilters 2 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.core.impl.WCMComponentFilter 3 (2013-10-07 12:20:41) LOG Calling filter: com.day.cq.wcm.core.impl.WCMDebugFilter 3 (2013-10-07 12:20:41) TIMER_START{org.apache.sling.servlets.get.DefaultGetServlet#0} 3 (2013-10-07 12:20:41) TIMER_END{0,org.apache.sling.servlets.get.DefaultGetServlet#0} 3 (2013-10-07 12:20:41) LOG Applying Error filters 3 (2013-10-07 12:20:41) LOG Calling filter: org.apache.sling.rewriter.impl.RewriterFilter 3 (2013-10-07 12:20:41) TIMER_START{handleError:status=404} 3 (2013-10-07 12:20:41) TIMER_END{0,handleError:status=404} Using handler /libs/sling/servlet/errorhandler/404.jsp 3 (2013-10-07 12:20:41) LOG Found processor for post processing ProcessorConfiguration: {contentTypes=[text/html],order=-1, active=true, valid=true, processErrorResponse=true, pipeline=(generator=Config(type=htmlparser, config={}), transformers=(Config(type=linkchecker, config={}), Config(type=mobile, config=org.apache.sling.jcr.resource.JcrPropertyMap@400fad05), Config(type=mobiledebug, config=org.apache.sling.jcr.resource.JcrPropertyMap@4528441e), Config(type=contentsync, config=org.apache.sling.jcr.resource.JcrPropertyMap@5690310a), serializer=Config(type=htmlwriter, config={}))} 4 (2013-10-07 12:20:41) TIMER_END{4,Request Processing} Dumping SlingRequestProgressTracker Entries
October 16, 2015

Hi ,

 

I tried Ojjis code first and now though i am not getting any error in logs but when I tried to access the servelet using browser i am getting below error:

07.10.2013 12:13:57.808 *INFO* [0:0:0:0:0:0:0:1 [1381162437795] GET /bin/company/repo HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /bin/company/repo not found
07.10.2013 12:13:58.154 *INFO* [0:0:0:0:0:0:0:1 [1381162438144] GET /bin/company/repo HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /bin/company/repo not found

For your reference below is the complete error.log

07.10.2013 12:13:43.572 *INFO* [JcrInstaller.1] org.apache.sling.installer.provider.jcr.impl.JcrInstaller Registering resource with OSGi installer: [InstallableResource, priority=200, id=/apps/company/install/company-core-0.0.1-SNAPSHOT.jar]
07.10.2013 12:13:43.589 *INFO* [OsgiInstallerImpl] com.adobe.training.company-core Service [com.adobe.training.core.MySafeMethodServlet,1144] ServiceEvent UNREGISTERING
07.10.2013 12:13:43.589 *INFO* [OsgiInstallerImpl] com.adobe.training.core.MySafeMethodServlet Rohit service deactivated
07.10.2013 12:13:43.624 *INFO* [OsgiInstallerImpl] org.apache.sling.audit.osgi.installer Updated bundle com.adobe.training.company-core [231] from resource TaskResource(url=jcrinstall:/apps/company/install/company-core-0.0.1-SNAPSHOT.jar, entity=bundle:com.adobe.training.company-core, state=INSTALL, attributes=[org.apache.sling.installer.api.tasks.ResourceTransformer=:50:16:, Bundle-SymbolicName=com.adobe.training.company-core, Bundle-Version=0.0.1.SNAPSHOT], digest=1381162423439)
07.10.2013 12:13:43.624 *INFO* [FelixDispatchQueue] com.adobe.training.company-core BundleEvent STOPPED
07.10.2013 12:13:43.624 *INFO* [FelixDispatchQueue] com.adobe.training.company-core BundleEvent UNRESOLVED
07.10.2013 12:13:43.625 *INFO* [FelixDispatchQueue] com.adobe.training.company-core BundleEvent UPDATED
07.10.2013 12:13:43.625 *INFO* [FelixDispatchQueue] org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED
07.10.2013 12:13:43.627 *INFO* [FelixDispatchQueue] org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED
07.10.2013 12:13:43.880 *INFO* [FelixDispatchQueue] com.adobe.training.company-core BundleEvent RESOLVED
07.10.2013 12:13:43.890 *INFO* [OsgiInstallerImpl] com.adobe.training.core.MySafeMethodServlet Rohit service activated
07.10.2013 12:13:43.893 *INFO* [OsgiInstallerImpl] com.adobe.training.company-core Service [com.adobe.training.core.MySafeMethodServlet,1145] ServiceEvent REGISTERED
07.10.2013 12:13:43.893 *INFO* [OsgiInstallerImpl] org.apache.sling.installer.core.impl.tasks.BundleStartTask Bundle started (bundle ID=231) : com.adobe.training.company-core
07.10.2013 12:13:43.893 *INFO* [FelixDispatchQueue] com.adobe.training.company-core BundleEvent STARTED
07.10.2013 12:13:57.808 *INFO* [0:0:0:0:0:0:0:1 [1381162437795] GET /bin/company/repo HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /bin/company/repo not found
07.10.2013 12:13:58.154 *INFO* [0:0:0:0:0:0:0:1 [1381162438144] GET /bin/company/repo HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /bin/company/repo not found

smacdonald2008
Level 10
October 16, 2015
Content backfill required