Discussions

Web tier: servlets, JSP, Web frameworks: Generating Dynamic Files using Servlets / JSP

  1. Generating Dynamic Files using Servlets / JSP (1 messages)

    Hi, i was wondering if it is possible to generate dynmic files of any type not just HTML and send it to the user's browser without haveing to write on the server local disk first.

    I have to make an export function which will export the contents of a data base table in Comma Separated Values format in a text file.

    The data will be humungous, 10 to 15 MB so i cannot have the users wait while i write the data to a file on the disk and them let them d/l the file.

    How can i change the MIME type of the content from text/HTML to text/CSV (or anything else) so the browser saes it in a file rather than try and open it as an HTML page.

    Thanks

    Adi
  2. The simpliest way is to specify an unknown mime type, such as application/ms-exxxel. In this case, the browser will use the default behavior, which is to download the file instead of opening it in the browser. You can perform that by using:

    response.setContentType("application/ms-exxxel");

    in the doGet method of the servlet