While it is true that it is more usual to write a binary attachment using a servlet rather than a jsp, it is certainly possible to write a binary attachment from a jsp. And the advantage of doing so is that you need not worry about configuring web.xml or reloading your application. That can be an important consideration, depending on your web server environment.
Here is an example jsp that uses poi to send a binary attachment to a browser.
<%@page import="org.apache.poi.hssf.usermodel.*" %><%@page import="java.io.*" %><%
// create a small spreadsheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");
// write it as an excel attachment
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
response.setContentType("application/ms-excel");
response.setContentLength(outArray.length);
response.setHeader("Expires:", "0"); // eliminates browser caching
response.setHeader("Content-Disposition", "attachment; filename=testxls.xls");
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();
%>
The important trick is to make sure there is only one line with all your imports and other directives prior to the "<%" that begins your code. Otherwise, the jsp may output some initial new lines and corrupt your output.
Also, I suggest you always set the content length. Some browsers will not work correctly if it is not set. That is why I first output my spreadsheet to a byte array, so I could set the length prior to actually sending the data.