Servlet for external Url to fetch html | Community
Skip to main content
Level 3
August 30, 2023
Solved

Servlet for external Url to fetch html

  • August 30, 2023
  • 3 replies
  • 1288 views

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.
Best answer by ShaileshBassi

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

3 replies

sherinregi-1
Community Advisor
Community Advisor
August 30, 2023

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.

 

 

 

ShaileshBassi
Community Advisor
Community Advisor
August 30, 2023

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

vinuu123Author
Level 3
August 31, 2023

@shaileshbassi , here what is api ?

 this is my html for entering external url from dialog

can you help me here?

 

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
August 31, 2023

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

Mahedi_Sabuj
Community Advisor
Community Advisor
August 30, 2023

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?

Mahedi Sabuj