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

Servlet for external Url to fetch html

Avatar

Level 4

Hi Everyone,

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

vinuu123_0-1693388890825.png

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

5 Replies

Avatar

Community Advisor

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.

 

 

 

Avatar

Community Advisor

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

Avatar

Level 4

@ShaileshBassi , here what is api ?

 this is my html for entering external url from dialog

vinuu123_0-1693460148205.png

vinuu123_1-1693460255371.png

can you help me here?

 

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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?