Results 1 to 5 of 5
  1. #1
    aflores is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Download then show page from jsp

    How do you make a response from a jsp that starts the user download on a text file and then displays a page?

    Here is the code that starts the download:

    <%@ page import="jgvxb.*" %>
    <%
    response.setContentType("text/csv");
    response.setHeader("content-disposition","attachment; filename=feed.xml");
    Object result = Bvb.writeOut(out);
    session.setAttribute("result", result);
    %>

    Here is the code that generates the page:

    <%@ page import="jgvxb.*, java.util.*" %>
    <%
    Bvb bvb = (Bvb)session.getAttribute("result");
    List<String> errs = bvb.getErrors();
    if (null == errs){
    out.println("There were no error. " + "<br/>");
    } else {
    out.println("There were these errors: " + "<br/>");
    for (String s : errs) {
    out.println(s + "<br/>");

    }
    }
    %>

    Thanks ...

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I'm not sure you can do that, to have a download send and then on finish the download, have it redisplay to a html "success" page. With plain HTTP based downloading and regular web browsers anyway. I mean, if you used some kind of download manager client, such as something installed in the client computer natively, or some kind of flash or Java applet based widget object loaded into the page. Unless there is something funky with a JavaScript library that somehow polls the server side for complete transfer within the session and does the download using ajax based transport in the background.

    I think it would be simpler to try to do what most sites do, such as sourceforge, where they display a page that has a message "Your download should begin shortly. If it does not click here". And then have the file download offered.

  3. #3
    aflores is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    The last solution that you mentioned as similar to sourceforge is perfectly acceptable. How do I do that? Does it involve redirects, setting header attributes, or what?

    much appreciated,
    aflores

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I guess a simple solution might be have the jsp page render the "bla bla thankyou for downloading message" and then some javascript to invoke the download file URL operation, something like
    Java Code:
    <script type="text/javascipt">
    function startDownload()
    {
    var url='http://server/app/downloadServlet?file=file.ext';  
    window.open(url,'Download');
    }
    
    setTimeout("startDownload(), "2000"); // 2 seconds
    </script>
    Where here we assume that you have created a servlet mapped to downloadServlet inside your app "app". where this thing takes a url parameter for file name, or some other kind of identifier to tell it which file resource you wanted downloaded. and then this servlet would set the content type header, possibly content-length header as well, and then stream the file content over the response.

    the idea of having a download servlet mapped to a path like this is that it can also be the target of the direct link on the page in case the user does not have javascript enabled.

    if the resource is to be protected with making the user log in before they can download it, this download jsp page that renders the javascript and link to the download servlet might want to contain logic to check if the user has the correct authorization to download, and if not, display a message to indicate to go log in or register first. This same 'is user authenticated' logic would also need to be done in the download servlet as well, perhaps though it could be done in a filter too instead.

  5. #5
    aflores is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  2. Replies: 2
    Last Post: 03-28-2009, 06:50 AM
  3. show a value in javascipt in the jsp page
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-20-2008, 09:38 AM
  4. how direct her/ him to the Sun´s download page?
    By willemjav in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:03 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •