简体   繁体   中英

Java Servlet - Download and Forward to a page

I am trying to send a download and then forward to a page. but once the download completes the forward doesn't happen.

That's normal behavior. A file download will write the content of the file into the response and close the response stream, and a forward will try to write new content on the already closed response, resulting in nothing new being written in the response.

Solution:

In your client, use JavaScript to fire the file download, and also fire a request to the page you want to forward.

Client code adapted from here: Download a file and redirect...or alternative

<script>
function thanks() {
    setTimeout(function () {
        document.location.pathname = "another.jsp";
    }, 1000);
}
</script>

<a href="${request.contextPath}/yourServlet?file=foo.dat" onclick="thanks()">Download now!</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM