My custom servlet is not being called | Community
Skip to main content
Level 2
November 4, 2024

My custom servlet is not being called

  • November 4, 2024
  • 10 replies
  • 3567 views

Hi,

 

I'm building a custom servlet that just output hello world. I bundled it and loaded the bundle into AEM, and bundle seems to be "active" without any errors.

Still, when I try to reach my servlet, I get 404.

 

This is my servlet code:

 

@8220494( service = Servlet.class, immediate = true, property = { "sling.servlet.methods=GET", "sling.servlet.paths=/bin/accessibility-issues-render" } ) public class AccessibilityIssuesServlet extends SlingSafeMethodsServlet { private static final Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class); @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { LOG.info("AccessibilityIssuesServlet recieved GET: {}", request); } }

 

When I navigate to:
http://localhost:4502/bin/accessibility-issues-render

I get

Resource at '/bin/accessibility-issues-render' not found: No resource found

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

10 replies

SureshDhulipudi
Community Advisor
Community Advisor
November 4, 2024

Please check the error log for more details.

 

Change LogFactory initiation.

private static final Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class);

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.io.IOException;

@8220494(
service = Servlet.class,
immediate = true,
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/accessibility-issues-render"
}
)
public class AccessibilityIssuesServlet extends SlingSafeMethodsServlet {

private static final Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class);

@9944223
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
LOG.info("AccessibilityIssuesServlet received GET: {}", request);
response.getWriter().write("Servlet is working");
}
}

BenBr9Author
Level 2
November 5, 2024

Hi

 

There are no errors in the errors.log  it only state that my servlet has been registered, my bundle is active, but it is not getting called.

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 4, 2024

Hi, 

Pls check if the bundle is active and the servlet is correctly exposed. 

Esteban Bustamante
BenBr9Author
Level 2
November 5, 2024

The bundle is indeed active and correctly exposed..

 

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 4, 2024

Hi @benbr9,

check the OSGi Configuration named Apache Sling Servlet/Script Resolver and Error Handler if it allows for the /bin/* path. By default /bin/* should be allowed, but maybe you are not using the default config. Also, it is recommended to use resource type based servlets over path based.

 

Good luck,

Daniel

PRATHYUSHA_VP
Community Advisor
Community Advisor
November 5, 2024

Additionally you can check servlet path in below sling servlet resolver  for additional debugging purpose -

 

http://localhost:4502/system/console/servletresolver

 

Hope this helps 

 

Thanks

GabrielMircea
Level 2
November 5, 2024

In Sling, if you access a servlet that has been mapped to a path, it will only respond if there is no other resource at that path. If there's a resource with the same path, the servlet won't be invoked.

 

Check the CRXDE Lite (http://localhost:4502/crx/de) to ensure there isn't an existing resource at /bin/accessibility-issues-render.

BenBr9Author
Level 2
November 5, 2024

I checked crx/de and there is no existing resource under /bin

 

 

kautuk_sahni
Community Manager
Community Manager
November 5, 2024

@benbr9 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni
BenBr9Author
Level 2
November 5, 2024

Didn't find an answer yet, no

chavad
Level 2
November 5, 2024

Hi @benbr9 

Can you check in http://localhost:4502/system/console/components your servlet is there or not?

BenBr9Author
Level 2
November 6, 2024

Yes it's there, and it is active

 

 

BenBr9Author
Level 2
November 8, 2024

Hi,

 

Still struggling with this.

Happy to get any help on the topic.

 

PRATHYUSHA_VP
Community Advisor
Community Advisor
November 8, 2024

hi @benbr9 

 

Can you please post whole code along with import statements. 

 

Will re-check if any issue

BenBr9Author
Level 2
November 8, 2024

Thanks!

 

package com.evinced.aem.core; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.service.component.annotations.Component; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.nio.charset.StandardCharsets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component( service = Servlet.class, immediate = true, property = { "sling.servlet.methods=GET", "sling.servlet.paths=/bin/accessibility-issues-render" } ) public class AccessibilityIssuesServlet extends SlingSafeMethodsServlet { private static final String PUBLISH_HOST = "http://localhost:4503"; // URL of the publish instance private static final String TEMP_DIRECTORY = System.getProperty("java.io.tmpdir") + "/accessibility-data"; private static final Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class); @9944223 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { LOG.info("AccessibilityIssuesServlet recieved GET: {}", request); .... ....
GabrielMircea
Level 2
November 8, 2024

Verify that the sling.servlet.paths property is configured to allow access at this path. AEM sometimes restricts access to paths under /bin in the author instance due to security. You may need to:

 

  • Go to UserAdmin in AEM (localhost:4502/useradmin).
  • Check permissions for the path /bin/accessibility-issues-render for your user or system user associated with this servlet.
BenBr9Author
Level 2
November 8, 2024

This is what I see in the admin console. Not sure if this is ok or not.

 

 

/bin seems like a file there. It is strange.

But anyway, I also tried use /apps and it didn't work..

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 8, 2024

Hi @benbr9,

sometimes when you are struggling with an issue for a long time, it makes sense to start from scratch.

What I would suggest is that you define your custom API path instead of relying on the OOTB /bin.

For example, you can add /rest to the org.apache.sling.api.servlets.ServletPathsServlet config.

Then try using that as a path in your servlet:

@Component(service = Servlet.class)
@SlingServletPaths(value = "/rest/test")

In case you are testing on Dispatcher, you might need to add:

 

/allowedPaths { /rest/* }

 

BenBr9Author
Level 2
November 10, 2024

I tried to /rest path and it doesn't work.

 

How do I know if I'm using a dispatcher? Which file should I edit?

 

BenBr9Author
Level 2
November 10, 2024

Found the issue!

 

My pom maven-bundle-plugin had these private packages configured:

 

<Private-Package> org.apache.httpcomponents.*, org.apache.http.*, com.google.gson.*, com.google.gson.reflect.*, com.google.gson.stream.*, org.json.*, org.opentest4j.*, javax.servlet.*, org.apache.sling.*, </Private-Package>

 

I added it because of some bundle load errors. Seems I don't need this anymore, and when removing it - the servlet works!

 

Anyone knows why this configuration would cause issues?