1

I have a JSF application when the user clicks a button, have to do some actions and direct to a JSF new page. The same action must send a request to a sms gateway, where the response it recorded in the application silently, but must not alter any navigation of the JSF pages to user. How can I achieve this functionality?

For example, clicking the save button in report.xhtml page must take the user to new_report.xhtml page, but that click also have to send a http request to an external url and if possible, record the external response.

If I use faces context > request, as in this answer, it redirects to that external page.

Community
  • 1
  • 1
Buddhika Ariyaratne
  • 1,902
  • 3
  • 44
  • 79

1 Answers1

3

You can always connect to any other site by using URLConnection as explained here: Using java.net.URLConnection to fire and handle HTTP requests. If you don't like to have too much boilerplate code, then you may want to use a third party library like Apache HttpComponents or another library built on top of this one like Unirest. Using unirest, the code would be very easy:

String yourUrl = "http://url.you.want.to/connect/";
HttpResponse<String> stringResponse = Unirest.post(yourUrl)
    .field("parameter1", "parameter1Value")
    .field("parameter2", "parameter2Value")
    .asString();
//retrieving the string content of the response
saveResponseFromExternalSite(stringResponse.getBody());

DISCLAIMER: I am not related to unirest library at all. I'm not a developer nor a vendor nor a supporter of this library. I'm just a happy user.

Community
  • 1
  • 1
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306