Hello Team,
In my application, customServlet has these details:
@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "/apps/abc/mycustomName",
"sling.servlet.methods=GET" }, immediate = true)
public class customServletName extends SlingSafeMethodsServlet { ....
}
In a javascript method, below code is written to call the custom Servlet.
$.ajax({
url: "/content/data/abc/servlet/mynode",
type: "GET",
// other necessary details
});
/content/data/abc/servlet/mynode This is the jcr node, which has sling:resourceType as /apps/abc/mycustomName
Instead of this, in the js method, I can call directly url: "/apps/abc/mycustomName" Why above approach is used? is it known as Proxy servlet? I am bit confused. Could you please give me some clarity?
cc @aanchal-sikka @Jörg_Hoh @BrianKasingli
-Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @EstebanBustamante @Raja_Reddy
Please ignore my query. I got the solution. Writing here, so that others can be aware of.
@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "/apps/abc/mycustomName",
"sling.servlet.methods=GET" }, immediate = true)
public class customServletName extends SlingSafeMethodsServlet { ....
}
In a javascript method, below code is written to call the custom Servlet.
$.ajax({
url: "/content/data/abc/servlet/mynode",
type: "GET",
// other necessary details
});
/content/data/abc/servlet/mynode This is the jcr node, which has sling:resourceType as /apps/abc/mycustomName
In above case, servlet is called just because, js calls /content/data/abc/servlet/mynode. This node has sling:resourceType: /apps/abc/mycustomName Thats why servlet call is happening. Instead, in the JS method, if I write url: "/apps/abc/mycustomName", this JS is not aware of the servlet.
The approach used in your code is known as a "resource type-based servlet resolution". In this approach, the servlet is registered with a specific `sling.servlet.resourceTypes` property, which specifies the resource type that the servlet is intended to handle. When a request is made to a resource with that resource type, the servlet is automatically selected to handle the request.
In your case, the custom servlet is registered to handle requests for resources with the resource type `/apps/abc/mycustomName`. When a request is made to `/content/data/abc/servlet/mynode`, which has the same resource type, the servlet is selected to handle the request.
This approach is useful when you want to associate a specific servlet with a specific resource type, and have the servlet automatically selected when a request is made to a resource with that type.
On the other hand, calling the servlet directly using its registered path (`/apps/abc/mycustomName`) is also a valid approach. This approach is known as a "path-based servlet resolution". In this approach, the servlet is registered with a specific path, and is selected to handle requests made to that path.
Both approaches have their own advantages and disadvantages, and the choice between them depends on the specific requirements of your application.
Regarding your question about proxy servlets, a proxy servlet is a servlet that acts as an intermediary between a client and a server. It is used to forward requests from the client to the server, and to forward responses from the server back to the client. Proxy servlets are commonly used in web applications to provide access to remote resources, or to implement load balancing and failover mechanisms.
Hi @Raja_Reddy
Thanks for your reply.
As you have mentioned: "On the other hand, calling the servlet directly using its registered path (`/apps/abc/mycustomName`) is also a valid approach. This approach is known as a "path-based servlet resolution". In this approach, the servlet is registered with a specific path, and is selected to handle requests made to that path."
But in my case, Servlet is registered with "sling.servlet.resourceTypes=" + "/apps/abc/mycustomName",. Servlet is not registered with path based servlet.
So, my question is with resourceType only. From javascript, calling the node: /content/data/abc/servlet/mynode this node has sling:resourceType as /apps/abc/mycustomName. why this is required?
please refer
http://www.sgaemsolutions.com/2017/12/apache-sling-servlets-and-scripts.html
When we register a servlet using path, we must be specify what all paths are allowed as if we define something randomly, our servlet might not be functioning properly. Only a limited set of paths are allowed and the rest are blocked. We can add more path using Apache Sling Servlet / Script Resolver and Error Handler. Allowing more paths to execute servlets makes our application vulnerable. That’s why we should not open more doors for servlets to run until and unless it is required and cannot be achieved using resource type.
We might also need to tell specific paths to your consumers, who are consuming servlet response using ajax and any change in that path could have a serious affect. This might not be the case when you use resourceType.
Hi,
I don't fully understand your question. Are you asking why calling "/apps/abc/mycustomName" via JS is invoking your servlet registered with a different resourceType? If so, the simple answer is, I don't know. There might be additional code in your application that is redirecting, proxying, or performing other processes that you haven't discovered yet. The easiest way to check what's happening is to use the Sling Servlet Resolver[1] to determine which servlet is handling that request.
Hope this helps
Hi @EstebanBustamante @Raja_Reddy
Please ignore my query. I got the solution. Writing here, so that others can be aware of.
@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "/apps/abc/mycustomName",
"sling.servlet.methods=GET" }, immediate = true)
public class customServletName extends SlingSafeMethodsServlet { ....
}
In a javascript method, below code is written to call the custom Servlet.
$.ajax({
url: "/content/data/abc/servlet/mynode",
type: "GET",
// other necessary details
});
/content/data/abc/servlet/mynode This is the jcr node, which has sling:resourceType as /apps/abc/mycustomName
In above case, servlet is called just because, js calls /content/data/abc/servlet/mynode. This node has sling:resourceType: /apps/abc/mycustomName Thats why servlet call is happening. Instead, in the JS method, if I write url: "/apps/abc/mycustomName", this JS is not aware of the servlet.
@Mahesh_Gunaje Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies