Do we need ajax to call a servlet in AEM 6.5? | Community
Skip to main content
Level 2
December 1, 2022
Solved

Do we need ajax to call a servlet in AEM 6.5?

  • December 1, 2022
  • 2 replies
  • 3740 views

I was trying to call a path type servlet but it didn't work, so i read somewhere that we need to call it through ajax but where should i write the ajax code?

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

Hi chiragtakkar5,

A servlet can be registered via path using the following property -

Ex :
"sling.servlet.paths=" + "/bin/demo/querybuilder"

There is an OSGi Configuration named Apache Sling Servlet/Script Resolver and Error Handler in the felix console.

It has a property called "Execution Paths(servletresolver.paths)" which holds a list of absolute paths under which the servlet is accessible as a Resource.

By default bin is allowed in the resolver path which is why it works.

If you want your servlet with /api path to be accessible you will need to allow the path in the resolver paths. Please see the screenshot below:

It is recommended to use resource type based servlet over path based now. Please refer the link below:

https://sling.apache.org/documentation/the-sling-engine/servlets.html

Pls check your implementation .Hope this will help you .

Thanks

2 replies

krati_garg
Adobe Employee
Adobe Employee
December 1, 2022

 

@chiragta1 

 

A servlet can be triggered from backend, front end, 3rd Party application or even manually, hitting the Servlet URL - anything or everything that can use Http Protocol.

 

From Client or Browser end, ajax call is the best way to call the servlet. Write your ajax code, in jsp, js, jquery etc anything that is handling your front end logic.

 

Can you please explain how did you try to call the Path Type Servlet which didn't work?

 

 

 

ChiragTa1Author
Level 2
December 1, 2022

@krati_garg

Like when we create a project we get a default"simple servlet"servlet, which prints the JCR title of the page.
So, where its ajax is being called?

/*
 *  Copyright 2015 Adobe Systems Incorporated
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package dev.core.servlets;

import com.day.cq.commons.jcr.JcrConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;

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

/**
 * Servlet that writes some sample content into the response. It is mounted for
 * all resources of a specific Sling resource type. The
 * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
 * idempotent. For write operations use the {@link SlingAllMethodsServlet}.
 */
@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes="dev/components/page",
        methods=HttpConstants.METHOD_GET,
        extensions="txt")
@ServiceDescription("Simple Demo Servlet")
public class SimpleServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
        final Resource resource = req.getResource();
        resp.setContentType("text/plain");
        resp.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE));
    }
}
krati_garg
Adobe Employee
Adobe Employee
December 1, 2022

This is not a Path Based Servlet, this is a Resource type servlet.
Please follow below link to find out the difference between Path based and Resource Type Servlet:
https://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/

 

Coming to your previous question, your ajax call can be called from Clientlibs or Javascript implemented for your Front End (Page and Page Components)

KNanCommunity AdvisorAccepted solution
Community Advisor
December 1, 2022

Hi chiragtakkar5,

A servlet can be registered via path using the following property -

Ex :
"sling.servlet.paths=" + "/bin/demo/querybuilder"

There is an OSGi Configuration named Apache Sling Servlet/Script Resolver and Error Handler in the felix console.

It has a property called "Execution Paths(servletresolver.paths)" which holds a list of absolute paths under which the servlet is accessible as a Resource.

By default bin is allowed in the resolver path which is why it works.

If you want your servlet with /api path to be accessible you will need to allow the path in the resolver paths. Please see the screenshot below:

It is recommended to use resource type based servlet over path based now. Please refer the link below:

https://sling.apache.org/documentation/the-sling-engine/servlets.html

Pls check your implementation .Hope this will help you .

Thanks