If the resource is static, just put it in the public webcontent (there where your JSP/HTML/CSS/JS/etc files also are) and include a link to it in your JSP.
<a href="file.ext">download</a>
The servletcontainer will worry about setting the right HTTP response headers.
If the resource is dynamic, create a servlet which obtains an InputStream
of the content somehow (new FileInputStream
, resultSet.getBinaryStream()
, etc..etc..) and writes it to the OutputStream
of the response along at least the Content-Type
and Content-Disposition
response headers. Finally just link to that servlet in your JSP.
<a href="fileservlet/file.ext">download</a>
You can find a basic example in this article.
The Content-Type
header informs the client about the content type of the file so that it knows what application it should use to open it. The Content-Disposition
header informs the client what to do with it, displaying it inline or saving as attachment.