SEVERE: Servlet.service() for servlet [jsp] in context with path [/FileFinalText] threw exception
[java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
Please, explain me, how can I resolve the cause with the getOutputStream() and prevent this exception to occur?
So for any request you can either send back text, or send back an image, but not both.
The API allows you to call getWriter() or getOutputStream() but obviously not both.
JSP is designed to send back text. It is possible to have it return image data (as has been demonstrated here) but it requires jumping through hoops to do so.
A JSP page will automatically call getWriter/getOutputStream when it has to output some template text to the page.
If you call getOutputStream() yourself you get the exception the OP has posted.
By removing the spaces from your JSP page, and only including scriptlet tags, the JSP page will never attempt to do output, so calling getOutputStream from scriptlet code will succeed.
But it is fragile. One carriage return in the 'wrong' place will bring it down around your ears.
As Bear pointed out - the appropriate place for this code is a Servlet.