'Page's HTML code written into file when file is downloaded from web app

I have a downloadfile servlet code that will dynamically add contents into a CSV file for users to download. However instead of having the contents I want added into the CSV file, the page's HTML code appears in the file instead. Can anyone tell me what is causing this bug? Here's my controller code

response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment"; 
filename="\\evaluations.csv\\");

try
{
    OutputStream outputStream = response.getOutputStream();
    String outputResult = "xxxx, yyyy, zzzz, aaaa, bbbb, ccccc, dddd, eeee, ffff, gggg\n";
    outputStream.write(outputResult.getBytes());
    outputStream.flush();
    outputStream.close();
} catch(Exception e) {
    status = "Error exporting file, please try again later";
    System.out.println(e.toString());
}
        
//request.setAttribute("status",status);
//dispatcher = request.getRequestDispatcher("/viewEvaluations.jsp");
//dispatcher.forward(request, response);

EDIT:

Removing the forward request actually stop the HTML code from being copied into the file and I also realized its redundant and I've commented them out. This was the code that was causing the problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source