Hello Community - I have a servlet which is registered with the path. I wanted to pass some path parameters in the servlet path like the below.
www.company.com/apps/test/product/value1/value2
I will invoke this servlet based on user action not based on resourceType. I am not able to retrieve the path parameters. I tried with the below. Is there any way to retrieve the values? Can someone tell me the way to retrieve the values?
req.getRequestPathInfo().getSuffix();
req.getResource().getPath();
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @aemninja ,
Use ajax as below
var request = $.ajax({ url: 'url', type: 'GET', data: { field1: "hello", field2 : "hello2"} , contentType: 'application/json; charset=utf-8' }); request.done(function(data) { // your success code here }); request.fail(function(jqXHR, textStatus) { // your failure code here });
And in java servlet as
String field1 = request.getParameter("<field1_Property>");
String field2 = slingRequest.getParameter("<field2_Property>");
Hope this helps!!
Thanks
1. Are Value1 & Value 2 belongs to the product node?
2. Where Path servlet is getting called inside AEM component/ Page Or as an Ajax Call?
Then use following code
$.ajax({
url : "/bin/servletPath",
type : "GET",
data : {
<value1_Property>: <Value1>,
<value2_Property>: <Value2>
},
});
& inside Servlet get the values
String value_1 = slingRequest.getParameter("<value1_Property>");
String value_2 = slingRequest.getParameter("<value2_Property>");
OR
Enumeration enumeration = slingRequest.getParameterNames(); // in case multiple parameters
Hi @aemninja ,
Use ajax as below
var request = $.ajax({ url: 'url', type: 'GET', data: { field1: "hello", field2 : "hello2"} , contentType: 'application/json; charset=utf-8' }); request.done(function(data) { // your success code here }); request.fail(function(jqXHR, textStatus) { // your failure code here });
And in java servlet as
String field1 = request.getParameter("<field1_Property>");
String field2 = slingRequest.getParameter("<field2_Property>");
Hope this helps!!
Thanks
Hi @aemninja!
Looking at your url example "www.company.com/apps/test/product/value1/value2" it is not quite clear where your servlet path ends and the "parameters" you want to retrieve start - at least to the used API that's unclear.
Please refer to the Sling API documentation about URL decomposition. A URL may have the following parts:
Your example url has only the first, a resource path (because there are not dots present). There are no selectors, no extension and there isn't a suffix. Please note the hint from the above mentioned documentation page on suffixes:
"Note, that after the resource path at least a dot must be in the URL to let Sling detect the suffix."
That's why your first approach using .getSuffix() does not work.
I'm not 100% sure about your second approach but it will probably not work because your path is only registered for the servlet (and only the first part of it) and there is no real resource present at that path in the repository.
A simple solution to your issue would be to add an extension to your serlvet, e. g. binding it to:
This way Sling will be able to correctly identify the suffix properly and .getSuffix() will give you "/value1/value2".
However, from an architectural perspective I recommend to reevaluate your approach:
Hope that helps!
Dear @aemninja ,
It is not entirely clear where your servlet route finishes and the "parameters" you wish to retrieve begins when looking at your url example, "www.company.com/apps/test/product/value1/value2" - at least to the utilised API that is ambiguous.
Regarding URL decomposition, please consult the Sling API documentation. These elements could be found in a URL:
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies