I tried the following sample program for creating servlet in CQ but getting below error in logs:
Solved! Go to Solution.
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
Views
Replies
Total Likes
Thanks both of you for quick reply. Let me try . Will let you know shortly how it goes
Views
Replies
Total Likes
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
Views
Replies
Total Likes
This code works nicely for a Sling Servlet:
package
com.adobe.cq.sling;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.PrintWriter;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
java.rmi.ServerException;
import
java.util.Dictionary;
import
org.apache.felix.scr.annotations.Properties;
import
org.apache.felix.scr.annotations.Property;
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;
import
org.apache.sling.commons.osgi.OsgiUtil;
import
org.apache.sling.jcr.api.SlingRepository;
import
org.apache.felix.scr.annotations.Reference;
import
org.osgi.service.component.ComponentContext;
import
javax.jcr.Session;
import
javax.jcr.Node;
import
org.json.simple.JSONObject;
import
java.util.UUID;
@SlingServlet
(paths=
"/bin/mySearchServlet"
, methods =
"POST"
, metatype=
true
)
public
class
HandleClaim
extends
org.apache.sling.api.servlets.SlingAllMethodsServlet {
private
static
final
long
serialVersionUID = 2598426539166789515L;
@Reference
private
SlingRepository repository;
public
void
bindRepository(SlingRepository repository) {
this
.repository = repository;
}
@Override
protected
void
doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws
ServerException, IOException {
try
{
..... Do something in the DoPost
To see a step by step community article on building this Sling Servlet -- see this community article:
http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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:
Cannot serve request to /bin/company/repo in /libs/sling/servlet/errorhandler/404.jsp
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
Views
Replies
Total Likes
Hi smacdonald2008
Could you please share your complete code for Sling Servlet which you are mentioning. I will try the same now.
Views
Replies
Total Likes
Thats quite strange, have tried it both in 5.6 and 5.5 with the exact code i wrote above and it works fine :S
Is there anything indicating that there might be something wrong in the OSGi console about the servlet ?
Now the logs clearly points out that the servlet handling your request is not your servlet but the default get servlet.
This would lead to the error you get since there is actually no resource there. Is there any other things that you have added to your servlet ?
Scotts great example works just in the same way except for the "post" method and you would probably benefit from looking at the stuff there as well.
/Johan
Views
Replies
Total Likes
The complete code is in the article that i referenced. All content is there and listed step by step - including how to build this with Maven. The Servlet is invoked using an AJAX request from a JSP.
//Use JQuery AJAX request to post data to a Sling Servlet
$.ajax({
type: 'POST',
url:'/bin/mySearchServlet',
data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,
success: function(msg){
alert(msg); //display the data returned by the servlet
}
});
Views
Replies
Total Likes
Hi Ojjis,
Thanks for your reply. I donot think anything is missing. I started with the CQ Advance Training example. Not sure what could have been missing. I can see the servlet active in bundle , component , services. Also in logs there is no error when I deploy the servlet. Only when I try to access it via URL then I get NOT FOUND error...does this gives some clue.
Views
Replies
Total Likes
Can you try and invoke the servlet (for testing purposes) from a JSP using an AJAX request (as shown in the community article) - lets make sure that it works.
Views
Replies
Total Likes
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>
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]
|
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>
Views
Replies
Total Likes
Hi Rohit, You post is helped me today to resolve the exact problem. thanks.
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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies