I am using JSF framework in my application. I need to run a specific script before the render response phase in my Phase Listener class.
Condition for running this script is that, if the request triggered is a Ajax request i need to run the script, if the request triggered is a Http request i should not run that script.
Can anyone please help me to differentiate the requests recieved.?
Ajax requests have usually a X-Requested-With: XMLHttpRequest
request header. In JSF, you can obtain the request headers by ExternalContext#getRequestHeaderMap()
.
ExternalContext externalContext = facesContext.getExternalContext();
Map<String, String> headers = externalContext.getRequestHeaderMap();
boolean ajax = "XMLHttpRequest".equals(headers.get("X-Requested-With"));
Ajax requests set a server variable X-Requested-With
to XMLHttpRequest
. You can use that information to differentiate between ajax and normal requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With