Expand my Community achievements bar.

SOLVED

Servlet not getting registered

Avatar

Level 1
 

I have created the below servlet. I have added the servlet path in Apache sling servlet resolver. I am firing an ajax call with the url same as the path of servlet. But the servlet is getting ignored or not getting registered.

Could any one please provide some inputs for getting the servlet registered. I checked the servletresolver console and it says servlet does not exist. 

 

PFB code.

import java.io.IOException;

import java.rmi.ServerException;

import com.aem.poc.core.service.WaterHeightService;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.jcr.api.SlingRepository;


import java.util.*;

//DS Annotations

import org.apache.sling.servlets.annotations.SlingServletPaths;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;

import org.osgi.framework.Constants;

import org.apache.sling.api.servlets.HttpConstants;

import org.slf4j.LoggerFactory;

import org.slf4j.Logger;

@SlingServletPaths("/bin/heightSearchServlet")

@Component(service=Servlet.class)


public class WaterHeightServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

    //private static final long serialVersionUID = 2598426539166789515L;
    @Reference
    private SlingRepository repository;

    @Reference
    private WaterHeightService whs;
    List<Map> waterHeightList = new ArrayList<>();

    public void bindRepository(SlingRepository repository) {
        this.repository = repository;
    }

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
        try
        {
            String requiredHeight = request.getParameter("id");            waterHeightList.addAll(whs.getHeightList());
            Logger log = LoggerFactory.getLogger(WaterHeightServlet.class);
            //log.debug("passed param value is"+requiredHeight);
            for (Map item: waterHeightList) {                log.debug("map item is"+item.get("datevalue"));
            }
            //response . write() trturn the data
        }
        catch(Exception e)
        {            e.printStackTrace();
        }
    }
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor
3 Replies

Avatar

Community Advisor

Please try this way

@Component(service=Servlet.class,

property={

        "sling.servlet.methods=" + HttpConstants.METHOD_GET,

        "sling.servlet.paths="+ "

/bin/heightSearchServlet

"

})

public class WaterHeightServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

 

helps article:

https://helpx.adobe.com/experience-manager/using/aem64_mbean.html

Avatar

Community Advisor

hello Roslin, 

It seems like you are missing some annotations, OSGi DS 1.4 (R7) component property type annotations for Sling Servlets.

Take an example from Apache's website where they share an example of how to create a Sling Servlet, registering by ResourceType:

 

(service = { Servlet.class })
@SlingServletResourceTypes(
    resourceTypes="/apps/my/type", 
    methods= "GET",
    extensions="html",
    selectors="hello")
public class MyServlet extends SlingSafeMethodsServlet {

    
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ...
    }
}

 

Documentation: https://sling.apache.org/documentation/the-sling-engine/servlets.html

The documentation also explains why we would want to register a Sling Servlet by ResourceType rather than by Path. It's a good read - https://sling.apache.org/documentation/the-sling-engine/servlets.html#caveats-when-binding-servlets-...

Lastly, If you require to write a Unit test for your Sling Servlet, references can be found here - https://sourcedcode.com/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples

Avatar

Correct answer by
Community Advisor