Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Not being able to map Sling Servlet to a URL in CQ5.6

Avatar

Former Community Member

Hi,

I have created a test sling servlet as shown below:

package com.mercer.servlets; 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.SlingAllMethodsServlet; import javax.servlet.ServletException; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SlingServlet(paths="/insights", methods = "GET", metatype=true) public class SlingServletExample extends SlingAllMethodsServlet   { /** * */ private static final long serialVersionUID = 1L; Logger logger = LoggerFactory.getLogger(SlingServletExample.class); @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { logger.info("Inside doGet()"); response.setContentType("text/plain"); response.getOutputStream().print("Hello World!"); } }

I am now accessing the page http://localhost:4502/cf#/insights. But there is nothing I can see in the logs. I want to verify if the request is coming to the doGet method. One more thing, the /insights is a vanity URL.

Thanks in advance,

Ankit

1 Accepted Solution

Avatar

Correct answer by
Level 10

JSON is used for the community article as a demo -- in the community article - a fictional use case is to encode the submitted data to JSON data.

However -- in your use case -- I assume you are not using JSON. Therefore -- you do not need to worry about JSON Java libs or including them into the OSGi service layer. JSON is not required to get Sling Servlets to work.

However -- you use case is different from the one that is in the community article. In the community article - a sling servlet was invoked from a form's submit button that resulted in form data being submitted to the servlets doPost using an AJAX request:

//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
         }
     });

This community article addresses this community question:
http://forums.adobe.com/thread/1120136 (How to post a form to a servlet in CQ5.5?)

You are trying to invoke the servlet directly from an URL. See this stack overflow topic - it may help you.

http://stackoverflow.com/questions/11194586/adobe-cq5-custom-servlet-path 

 


 

View solution in original post

8 Replies

Avatar

Level 10

Hi 

See this community article that talks about how to develop a Sling Servlet and how to map to it from a JSP. 

http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html

This article provides step by step instructions. This article encodes data as JSON and returns the data to the client so we know that we are successfully invoking the sling servlet. 

Avatar

Former Community Member

Hi Scott,

Thank you for the response and I am doing exactly that. The path I have included in my Servlet is exactly the same as that of the page I access. Please see.

Ankit.

Avatar

Level 10

Have you deployed the Sling Servlet as an OSGi bundle? Is the OSGi bundle in an active state. If so -- you can map to the servlet from a CQ JSP using an AJAX request like this:

//Use JQuery AJAX request to post data to a Sling Servlet
 $.ajax({
         type: 'GET',   
         url:'/insights',
         success: function(msg){
           alert(msg); //display the data returned by the servlet
         }
     });

 

Try that from a JSP

Avatar

Former Community Member

Scott, I am not trying to map any JSP to the servlet. All I am trying to do is access a page http://localhost:4502/cf#/insights and want that when this page is hit the control should go the servlet. I have mapped /insights to the servelet using path attribute. Please let me know if what I am trying to do is correct or not. Also, to answer your question, I need to add "org.json.simple.parser" to the export package. But since I am using eclipse for development and maven to build the project, I am not sure where do I add "org.json.simple.parser" in the pom.xml. AFAIK, the MANIFEST.MF is created when the project is build. I don't have a export-package element anywhere in my pom.xml yet I have the export-package element in MANIFEST.MF. Any idea where do I include org.json.simple.parser in pom.xml.

Also, I am not sure if I am not using json at all even then I will have to include org.json.simple, org.json.simple.parser in export package.

Thanks,

Ankit

Avatar

Correct answer by
Level 10

JSON is used for the community article as a demo -- in the community article - a fictional use case is to encode the submitted data to JSON data.

However -- in your use case -- I assume you are not using JSON. Therefore -- you do not need to worry about JSON Java libs or including them into the OSGi service layer. JSON is not required to get Sling Servlets to work.

However -- you use case is different from the one that is in the community article. In the community article - a sling servlet was invoked from a form's submit button that resulted in form data being submitted to the servlets doPost using an AJAX request:

//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
         }
     });

This community article addresses this community question:
http://forums.adobe.com/thread/1120136 (How to post a form to a servlet in CQ5.5?)

You are trying to invoke the servlet directly from an URL. See this stack overflow topic - it may help you.

http://stackoverflow.com/questions/11194586/adobe-cq5-custom-servlet-path 

 


 

Avatar

Level 7

Have you checked in the OSGi console that the servlet you have created is actually alright and up and running ?
The vanity url part seems to me as the problem here. Can try to add the full path in the servlet instead and then see if your servlet is working correctly? 

/Johan

 

Avatar

Level 10

TO further add to Ojjis's great suggestion -- try invoking the servlet from a test JSP using an AJAX request to see if its returning data. This will inform you if the servet is returning data., 

Once you know your back-end servlet is working. Then try using the   full path in the servlet as Ojjis suggests.