Expand my Community achievements bar.

SOLVED

Handle Dynamic URL in CQ

Avatar

Former Community Member

We need to handle Dynamic URL to generate XML based on users request. URL: localhost:4502/feed/[search-term].xml

Ex: localhost:4502/feed/webinar.xml

It should return an xml, which will be generated by system based on search-term mentioned in the URL.

How can we handle such request? I've tried using servlet but it seems sling servlet doesn't support wildcards in the sling.servlet.paths.

Thanks,

Punit Jindal

1 Accepted Solution

Avatar

Correct answer by
Level 8

Have you included the XML extension in you script name? .html is the default extension so you don't need to include it in script names, but when you need to support other script names they need to be included in the extension. So for example rather than just webinar.jsp you need to name your script webinar.xml.jsp. 

View solution in original post

9 Replies

Avatar

Employee Advisor

A similar approach to the one orotas mentioned: Do not use selectors, but a suffix, then you could create URLs like http://localhost/feed.xml/searchterm; the implementation is quite identical to the use of selectors, but it might give you some more flexibility.

Jörg

Avatar

Level 8

One approach I know will work is create a page at the URL you want to use and then pass the search term in as with a selector or a query string. The template you case your page off of could be implemented as a servlet - which would potentially be cleaner given you want to generate XML. 

Avatar

Former Community Member

Hi orotas,

I tried using selectors.

URL: "localhost:4502/feed/rss.webinar.html"

Selectors work alright when extension is .html. But, when we use .xml (which is acutally required) instead of .html, selectors do not find the resource.

Note: localhost:4502/feed/rss.xml displays the resource.

Any idea?

Avatar

Employee Advisor

Pjindal,

how does your code look like? It should work irrespective of the extension.

Jörg

Avatar

Former Community Member

Hi Jörg,

As suggested, I created a page under /content/<site name>/us/en/public/test.

I am able to access the page using: http://localhost:4502/content/<site name>/us/en/public/test.webinar.html.

and http://localhost:4502/content/<site name>/us/en/public/test.xml.

But when I include selector in the 2nd URL, it doesn't return the page.

http://localhost:4502/content/<site name>/us/en/public/test.webinar.xml

-PJindal

Avatar

Level 1

I think you should set response content type as xml like below.

 response.setContentType("text/xml;charset=utf-8");

- Subba Rao A

Avatar

Former Community Member

I tried to set Content Type but to no use. I think my problem can be solved by using suffix instead of selectors, like localhost:4502/feed.xml/[search-term].

But I would need to change extension from .xml to .html after request is submitted by the user. Can you please suggest how can I do that?

Thanks in advance.

PJindal

Avatar

Correct answer by
Level 8

Have you included the XML extension in you script name? .html is the default extension so you don't need to include it in script names, but when you need to support other script names they need to be included in the extension. So for example rather than just webinar.jsp you need to name your script webinar.xml.jsp. 

Avatar

Former Community Member

Hi orotos,

It worked. Now am getting control on the required jsp. Thanks a lot for your input.

-PJindal