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
Views
Replies
Total Likes
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");
}
}
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.
Hi,
Pls check if the bundle is active and the servlet is correctly exposed.
The bundle is indeed active and correctly exposed..
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
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
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
I also checked http://localhost:4502/system/console/servletresolver
But it returns that there is no resource for my path
Can you update as /bin/accessibility-issues-render and post the screenshot ? '/' is missed in the screenshot you posted !
Good point!
But I still get unresolved...
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 :
Hope this helps
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?
@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
Example : http://localhost:4502/content/wknd/us/en.hello.html
Hope this helps !
Thanks
I tried to access an existing page:
Using:
http://localhost:4502/content/we-retail/us/en.hello.html
But the regular en.html page is being rendered and not my servlet.
I should probably also change the resourceType to be what is defined in my servlet:
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 ....
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.
I checked crx/de and there is no existing resource under /bin
@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!
Views
Replies
Total Likes
Didn't find an answer yet, no
Views
Replies
Total Likes
Hi @BenBr9
Can you check in http://localhost:4502/system/console/components your servlet is there or not?
Yes it's there, and it is active
Views
Likes
Replies