Servlet not getting registered | Community
Skip to main content
New Participant
Feb 13, 2020 10:25 AM UTC
Solved

Servlet not getting registered

 

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();
        }
    }
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
arunpatidar
arunpatidarAccepted solution
New Participant
Feb 18, 2020 12:43 PM UTC

3 replies

arunpatidar
arunpatidarAccepted solution
New Participant
Feb 18, 2020 12:43 PM UTC
BrianKasingli
New Participant
Feb 14, 2020 11:10 AM UTC

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-by-path

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

Translate
SureshDhulipudi
New Participant
Feb 14, 2020 4:30 AM UTC

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

Translate