How to call sling servlet directly using it's path? | Community
Skip to main content
GK-007
Level 9
October 16, 2015
Solved

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

  • October 16, 2015
  • 4 replies
  • 5985 views

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

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 ogill

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

4 replies

ogillAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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

Level 4
January 14, 2024

@ogill 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.)

GK-007
GK-007Author
Level 9
October 16, 2015

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

smacdonald2008
Level 10
October 16, 2015

We have this documented in this community article: 

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

Lokesh_Shivalingaiah
Level 10
October 16, 2015

you can call by http://<host>:<port>/<servletpath>