Avoiding ADF loopback script in servlets
A few versions ago ADF introduced a loopback script which is returned to client the first time an URL is accessed.
|ADF Loopback script|
The problem is, when you want to serve binary content -such as images- from a servervlet, and you need access to FacesContext in that servlet, you need to call the servlet from within /faces/ URL, or FacesContext.getCurrentIstance() returns null:
Pointing browser to http://localhost:7101/myapp/imageservlet?empId=72
|Accessing imageservlet without /faces leaves us with null faces context.|
The obvious thing to do is to access the servlet using http://localhost:7101/myapp/faces/imageservlet?empId=72 . But if we do this, the loopback script is returned to client by ADF framework.
Now imagine having a html page with <img alt=”” src=”http://localhost:7101/myapp/faces/imageservlet?empId=72″ /> tag. The image will not be rendered, since it will receive loopback script as image source.
The workaround is quite simple. Turns out adf loopack script is not generated for some requests, including requests for jpg, png, js, css… So all we need to do is change the request url a bit (assuming the servlet listens to /imageservlet/*). Just append a dummy “/image.jpg” after the servlet url and voila, loopback is gone.
<img alt=”” src=”http://localhost:7101/myapp/faces/imageservlet/image.jpg?empId=72″ />
Note that the first request will still generate 302 redirect, but that will not affect image rendering.