Creating ‘download file’ functionality in our ADF Web Applications is pretty easy, especially when using JHeadstart. Using a few declarative settings, we can generate the download capability in no time at all:
There is one just little thing left to be desired: the download dialog for some reason does not know the correct name of the file we are downloading:
Here we are downloading AdfMatrixDemo.zip but the browser calls it ordDeliverMedia.zip, a generic name used for all files we download.
Fortunately, this can be remedied very easily. (this happens to have been my very first job with Java technology, back in 1999). All we have to do is set a header in the Response object in which we return the file:
This informs the browser – or at least the ones that are listening, as some browsers I believe ignore this piece of information – of the correct file name.
You may – and probably should -wonder where you can inject this line of code – and where to get the filename from. It turns out to be pretty simple for JHeadstart environments.
All downloads are handled by the OrdPlayMediaServlet that is configured in the web.xml file. If we register our own extension of this Servlet, and set the required response header in it, we are in business. The steps:
- create class AMISOrdPlayMediaServlet, extending oracle.jheadstart.ord.html.OrdPlayMediaServlet; let’s keep it simple and just copy all code from OrdPlayMediaServlet to AMISOrdPlayMediaServlet.
- locate the renderContent() method; add a single line of code, following the assignment of ordObj:
- register the AMISOrdPlayMediaServlet in web.xml
<servlet><br /> <servlet-name>ordDeliverMedia</servlet-name><br /> <servlet-class>nl.amis.media.AMISOrdPlayMediaServlet</servlet-class><br /> <init-param><br /> <param-name>releaseMode</param-name><br /> <param-value>Stateful</param-value><br /> </init-param><br /> </servlet><br /><br />
And let’s run the application:
And there we have it: the browser uses the correct filename!