Servlet for external Url to fetch html | Community
Skip to main content
Level 3
Aug 30, 2023 9:49 AM UTC
Solved

Servlet for external Url to fetch html

Hi Everyone,

Can anyone help me how to write sevlet for this dialog, I want fetch html from external URL.

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
ShaileshBassi
ShaileshBassiAccepted solution
New Participant
Aug 31, 2023 7:23 AM UTC

Hi @vinuu123  I am referring the API to the external URL. You can get it via annoations.

@Inject @Optional @Named("externalUrl") protected String externalUrl;

Thanks

5 replies

Mahedi_Sabuj
New Participant
Aug 30, 2023 6:26 PM UTC

Hi @vinuu123 

I'm curious about the choice not to use an iframe to embed external URL. Could you provide insight that influenced this decision?

Translate
Mahedi Sabuj
ShaileshBassi
New Participant
Aug 30, 2023 2:57 PM UTC

Hi @vinuu123 You can read the value of the URL in the sling dialog and then use the below code to get the extract of the entire html section

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class HtmlModel{ private static final Logger logger = LoggerFactory.getLogger(HtmlModel.class); @Getter private String body; @PostConstruct void init() { final String api = <read it from dialog>; final HttpGet httpRequest = new HttpGet(api); try (final CloseableHttpResponse response = HttpClients.createDefault().execute(httpRequest)) { body = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); } catch (final IOException e) { logger.error("IOException while reading HTML API %s: ", e); } } }

 

Hope this helps!

Thanks

Translate
vinuu123Author
Level 3
Aug 31, 2023 5:38 AM UTC

@shaileshbassi , here what is api ?

 this is my html for entering external url from dialog

can you help me here?

 

Translate
ShaileshBassi
ShaileshBassiAccepted solution
New Participant
Aug 31, 2023 7:23 AM UTC

Hi @vinuu123  I am referring the API to the external URL. You can get it via annoations.

@Inject @Optional @Named("externalUrl") protected String externalUrl;

Thanks

Translate
sherinregi-1
New Participant
Aug 30, 2023 10:38 AM UTC

Hi @vinuu123 

I am not sure if I am exactly following the requirement here but in case your ask is to invoke the servlet from dialog you can try the implementation in the below link.

https://aemsimplifiedbynikhil.wordpress.com/2018/07/30/call-any-servlet-in-touchui-dialog-aem-6-3/

 

You can also bind with a slingmodel in backend and then read the content in backend with URL configured from the dialog also and can avoid servlet if that works.

 

 

 

Translate