Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Cannot serve request to Servlet in org.apache.sling.servlets.get.DefaultGetServlet

Avatar

Level 2

Hello everyone,

 

I've created one ajax function which has servlet path in it so, whenever I am trying to submit my form, my servlet is not getting hit. The response (in developers tool) from that servlet is coming blank but it is showing response code as 200.

 

I've done following debugging but still facing the same problem:-
1. Added my servlet path in script resolver.

2. Removed Post method from Adobe Granite CSRF Filter.

The same code is working correctly on my colleague's local machine and production as well. I tried all different ways to work with that servlet but nothing is working on my machine.

Please help me to find a solution regarding this.

 

Regards, 

Sayali

11 Replies

Avatar

Community Advisor

@045_SayaliDa : Is it possible that you are getting this error somewhere else and not on the servlet. Are you logged-in to your 4502 (author) instance while hitting this?
Few questions:

1. Are you hitting your servlet using specific path i.e /bin/some-path or resource based. Can you share a sample of how you are accessing it from ajax function?

2. If it is has only GET method, you don't have to do -> Added my servlet path in script resolver.

3. What is GET method in your servlet returning? Is there a logic/condition to return something? Try hardcoding servlet response to debug.

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write("{create-a-valid-test-json-string}");

 

Avatar

Level 2

1. My servlet is using a specific path only i.e /myfolder/myservlet

2. It is using both get and post method.

Previously response was coming completely blank and now it is showing content modified.

Avatar

Level 6

Hi @045_SayaliDa, you can try with the following dubbing options to check what could be wrong:

1. Use a debugger to see up to which point your servlet is working. On hitting the form submit button you will be redirected to the code and then you can go line by line and find the error. If you think the problem is in ajax call, hit your servlet using postman, this way you will make sure that your servlet is working fine. To debug the ajax code further, use console statements or browser's debugger.

2. Visit system/console/components to see if your servlet is successfully registered.

3. Make sure you are testing a correct path with correct selectors and extensions.

4. Check your servlet code is correct.

5. Try restarting your instance.

 

Avatar

Level 2

1.I kept a breakpoint on the ajax call but whenever I am trying to debug my ajax call, the servlet is not getting hit and the debugger is not redirecting to the code. It is coming out of that ajax function.
2. My servlet is registered on system/console/components.

 

The same code is working correctly on my colleague's local machine and on production as well.

Avatar

Level 6

Hi @045_SayaliDa,
If the debugger is not redirecting you to the servlet code, you can try with the following options:
1. Check your registered servlet is in active state by going to system/console/components. If it is not in the active state, see what's the problem there.

2. Try changing the "selectors" name in your servlet code and make changes in the ajax code as well for the same and deploy your code again. If it doesn't work, try restarting your instance and re-deploy the code.

Generally, trying above solves the problem you are facing.

Also, I suggest you to use postman to hit your servlet and do the debugging so that there's no dependency on the ajax code.

Avatar

Community Advisor

Try this block of code

@component(
    service = { Servlet.class },
    property = {
        "sling.servlet.resourceTypes=yourapp/components/content",
        "sling.servlet.methods=GET"
    }
)
public class ContentDisplayServlet extends SlingSafeMethodsServlet {
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        // Fetch and render content
        // ...

        // Send JSON response
        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put("success", true);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonResponse.toString());
    }
}


Reference: https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code

 

Avatar

Community Advisor

Hi @045_SayaliDa 

Were the user suggestions helpful? Let us know if more details are needed or mark the answer as correct. If you found a solution independently, please share it with the community.



Arun Patidar

Avatar

Level 2

The same code is working correctly on my colleague's local machine and production as well. I tried all different ways to work with that servlet but nothing is working on my machine.

Please help me to find a solution regarding this.

Avatar

Community Advisor

Hi @045_SayaliDa 
Please compare java version in your local machines vs your friends machin.



Arun Patidar

Avatar

Administrator

@045_SayaliDa Did you find the suggestions from users 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