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.
SOLVED

Passing path parameters in Servlet

Avatar

Level 4

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();

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

 

 

 

View solution in original post

7 Replies

Avatar

Community Advisor

@aemninja 

 

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?

Avatar

Level 4
@Suraj_Kamdi - The servlet path is "/apps/test/product" and value1 & value2 are path parameters that I wanted to pass it to the servlet. I am calling the servlet from the ajax call.

Avatar

Community Advisor

@aemninja

 

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

 

 

Avatar

Correct answer by
Employee Advisor

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

 

 

 

Avatar

Level 4
@Bimmi_Soi - I don't want to go with the query parameter way of implementation. I would like to go with path paramters route only. Query parameters implementation is already in place and we are changing it to path parameters.

Avatar

Employee Advisor

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:

  • Resource Path
  • Selectors
  • Extension
  • Suffix

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:

  • Why are you binding the servlet to a fixed path?
    This has a couple of implications and is therefore not recommended for the majority of use cases. There are some edge cases where binding a servlet to a path may be necessary of beneficial but in my experience that's almost never the case. Please see this documentation for additional information on the caveats/drawbacks when binding servlets by path.
  • Why are you trying to pass parameters as a suffix?
    This depends on your application architecture, but from the information share this does not look ideal to me. What's the reason/requirement behind this decision? If you need to pass parameters, there are two common approaches:
    • Query-Parameters: This is the "classic" way to go if you don't want/need to cache the pages (as query parameters will cause the dispatcher to not cache these requests). It's commonly used, e. g. for search queries or similar, dynamic requests.
    • Selectors: selectors can be used quite similar to query parameters but allow caching on the dispatcher. There are some things to keep in mind (number of variations, order of selectors, etc.) but they all apply to suffixes as well, so these are not disadvantages compared to your current approach. 

Hope that helps!

 

Avatar

Level 1

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: