Expand my Community achievements bar.

My custom servlet is not being called

Avatar

Level 3

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:

 

@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 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

 

30 Replies

Avatar

Community Advisor

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;

@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 Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class);

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

Avatar

Level 3

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.

Avatar

Community Advisor

Hi, 

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



Esteban Bustamante

Avatar

Level 8

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

Avatar

Level 4

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

Avatar

Level 3

Hi

 

I checked it and /bin is allowed. I also added a path for my specific path just in case, but it doesn't work

BenBr9_0-1730794922855.png

I also checked http://localhost:4502/system/console/servletresolver

But it returns that there is no resource for my path

 

BenBr9_0-1730794979727.png

 

Avatar

Level 4

Can you update as /bin/accessibility-issues-render and post the screenshot ? '/' is missed in the screenshot you posted ! 

Avatar

Level 4

Servlet is not registered and it is unable to resolve. Please re-check the annotations and the import statements

 

Try to register servlet using resourceType : 

 

https://sling.apache.org/documentation/the-sling-engine/servlets.html#caveats-when-binding-servlets-...

 

Hope this helps 

Avatar

Level 3

Thanks

 

I tried registering by servlet by using:

@component(
service = Servlet.class,
immediate = true,
property = {     
    "sling.servlet.methods=GET",
    "sling.servlet.resourceTypes=/apps/my/type",
    "sling.servlet.extensions=html",
    "sling.servlet.selectors=hello"    
    })

 

And access it via:

http://localhost:4502/apps/my/type/hello.html

But it is still not found

 

Maybe I should access it differently? 

Avatar

Level 4

@BenBr9 It will work only if resource /apps/my/type exists in your instance, else it is unable to find the resource to resolve and execute it 

 

Please create / refer existing component and refer that in your servlet  (or) access using page resource similar to below -

 

PageName : Use existing page in pageName and .hello is selector (if resourceType doesn't exist, selector will take priority)

 

For reference : 

https://medium.com/@techzette2/sling-resource-resolution-aem-a72fabc07a59

 

Access like below :

http://localhost:4502/{pageName}.hello.html

 

Examplehttp://localhost:4502/content/wknd/us/en.hello.html

 

PRATHYUSHA_VP_2-1730804362737.png

 

 

 

Hope this helps !

Avatar

Level 3

Thanks

 

I tried to access an existing page:

BenBr9_0-1730805871794.png

 

 

Using:

http://localhost:4502/content/we-retail/us/en.hello.html

 

But the regular en.html page is being rendered and not my servlet.

 

Avatar

Level 3

I should probably also change the resourceType to be what is defined in my servlet:

BenBr9_0-1730807019480.png

 

But

1. I tried that and it didn't work. I get an empty page loaded.

2. This is not my desire flow. This will require the user to do too much changes to his existing AEM setup. All I want is to implement a servlet that serves a static basic HTML page under /bin/my-content ....

 

Avatar

Level 3

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.

Avatar

Level 3

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

 

BenBr9_1-1730795042697.png

 

Avatar

Administrator

@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