Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to call sling servlet directly using it's path?

Avatar

Level 9

Hi All,

I have written sling servlet and i want to check if it's working propelrly.

Before including in component and i want to check whether it's returning data properly.

I heard we can call from browser and check the same.

Can some one help how to execute like this

Thanks,

Kishore

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Kishore,

Why are you declaring it as a service and a component? Try and create a simple servlet and see if that works.

I don't see the declaration for it being a servlet e.g.

@SlingServlet(paths="/bin/mySearchServlet", methods = "GET", metatype=true)

Regards,

Opkar

[1https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

View solution in original post

5 Replies

Avatar

Level 9

i am calling in the same way but 404 is coming.

My Servlet code looks as below.


@Component(name = "com.symantec.elibrary.core.servlets.ProductManagementServlet", label = "Product Management servlet", description = "This servlet filters the doctypes", specVersion = "1.1", metatype = true, immediate = true, policy = ConfigurationPolicy.REQUIRE)
@Service
@Properties({
        @Property(name = "service.description", value = "Symantec ProductManagementServlet Service"),
        @Property(name = "service.vendor", value = "Symantec Corporation."),
        @Property(name = "sling.servlet.paths", value = "/c/symantec/servlets/ConnectToSABA", propertyPrivate = true),
        @Property(name = "sling.servlet.methods", value = "GET", propertyPrivate = true)})

 

public class ProductManagementServlet extends SlingAllMethodsServlet{
    private static final long serialVersionUID = 1L;
    
    /** Default log. */
    protected final Logger log = LoggerFactory.getLogger(ProductManagementServlet.class);
    /**
     * The set of argument type classes for the reflection method calls.
     */

    @SuppressWarnings("rawtypes")
    protected static final Class[] parameterTypes = { SlingHttpServletRequest.class, SlingHttpServletResponse.class };
    
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,
                IOException {
        
        try {
            response.setContentType("application/json");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("Key1", "Value1");
            
            response.getWriter().print(jsonObj.toString());
            
        }catch (Exception e) {
            log.error("Exception in doGdet() method", e);
            return;
        }
        
    }
    

    /**
     * This method is called the OSGI framework when the bundle is activated. It
     * reads the properties from the component context.
     * 
     * @param componentContext
     */

    @SuppressWarnings("rawtypes")
    public void activate(ComponentContext componentContext) {
        log.debug("Entering activate method.");
        Dictionary properties = componentContext.getProperties();
        if (null != properties) {
            // /
        }
        log.debug("Exiting activate method.");
    }

    /**
     * 
     * @param componentContext
     * @throws RepositoryException
     */

    protected void deactivate(ComponentContext componentContext) {
        log.debug("Entering deactivate method.");
        log.debug("Exiting deactivate method.");
    }


}

Thanks.

Kishore

Avatar

Correct answer by
Employee

Hi Kishore,

Why are you declaring it as a service and a component? Try and create a simple servlet and see if that works.

I don't see the declaration for it being a servlet e.g.

@SlingServlet(paths="/bin/mySearchServlet", methods = "GET", metatype=true)

Regards,

Opkar

[1https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Avatar

Level 4

@Opkar_Gill Links on this page are no longer available. Please refresh for AEM 6.5 content or retire/remove/deactivate page. (Saves us a little time in the field when searching for solutions.)