April 26, 2020
Estimated Post Reading Time ~

The Servlet status is disabled in AEM 6.4

The Servlet created with Archetype 10 is disabled while deploying the same to AEM 6.4(the same is applicable for the Servlet developed in AEM 6.4 with OSGI annotation)







The servlet code(enabled with scr annotation)

package config.core.servlets;

import java.io.IOException;
import java.util.*;
import javax.servlet.*;


import org.apache.sling.api.*;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.*;
import org.osgi.framework.Constants;
import org.apache.sling.commons.json.*;
import org.apache.felix.scr.annotations.*;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.felix.scr.annotations.ConfigurationPolicy;

@Service
@Component(policy = ConfigurationPolicy.REQUIRE)
@SlingServlet(paths = "/bin/servlet/selector1", selectors = "dynamicpopulate",
extensions = "json", methods = "GET", generateComponent = false, generateService = false)

public class FirstServletSelector1 extends SlingAllMethodsServlet {

protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();

try {

jsonObject.put("value", "1");
jsonObject.put("text", "Albin1");
jsonObject1.put("value", "2");
jsonObject1.put("text", "Albin2");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

jsonArray.put(jsonObject1);
jsonArray.put(jsonObject);
response.getWriter().write(jsonArray.toString());
}

}


The issue is ConfigurationPolicy is defined as REQUIRE but there is no sling:OsgiConfig defined in the repository for the servlet PID.

When we make component 'policy' to 'REQUIRE', OSGi container expects corresponding configuration object (osgi:Config node) to become satisfied

The issue can be resolved either one of the below approach

Change the ConfigurationPolicy.REQUIRE to ConfigurationPolicy.OPTIONAL in case the sling:OsgiConfig is not mandatory to enable this servlet
Enable the sling:OsgiConfig in the repository with required configuration values for this servlet


The servlet is in Active state after following one of the above approach.




The details mentioned in this post is one of the reasons to mark the Servlet status as disabled.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.