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?
Solved! Go to Solution.
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
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?
@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)); } }
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)
@krati_garg
yes, the earlier which i was talking about was a path type, the one which i send it to you is the default servlet.
i just want to know that for resource type also we need to call it through ajax?
if yes, then for default also Ajax must be called somewhere i tried to find out but didn't get.
The link I shared above, will explain via code on how to call both resource type and path type servlets through ajax call
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
Views
Likes
Replies