Discussions

EJB design: How to present file to user in Web based J2EE application

  1. I have a web application build on Tomcat and J2EE application server. (JSP/Servlet <---> EJBs <---> Database)

    In my web business, the application will generate a txt file and present it to the user. I prepare to provide three ways to aproach this:
    1- Send a mail attaching the file;
    2- Display the file content as upon the request;
    3- Give a hyper link in web page, allow the user download it immediatly.
    The 1 and 2 are easy to implement, but how to do 3? Should J2EE write a physical file or Servlet? How to manage the files while hundred people send the request to apply their own files?

    Could u help me?

    Thanks a lot!
  2. The URL could just be a servlet URL where it sends the file, but sets the mimetype to something like application/octet-stream which would force the user to download the content.
  3. Interesting, it's a good idea,

    thx!
  4. Hi, I find the right place here!
    Thanks for your replies!

    From help of Dave C, I write a servlet to let user download a text file which is generated dynamically:
    ...
    response.setContentType("application/octet-stream");
    PrintWriter pw = response.getWriter();
    pw.print(netfiles);
    ...

    It's simple and working. But now if it can prompt a file name to user when it starts that will be perfect, anyone can help!
  5. response.setHeader("Content-Disposition","attachment; filename="");
  6. Just provide link to the related file.If its a text file(or even word document) it will display in the browser itself.If it encounters some different format it will ask the user to download.
  7. See for example Content tag from Coldtags suite:
    www.servletsuite.com/jsp.htm

    you may include files that are located outside of your web application