• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieve a BLOB from the MySQL DB

 
Robert Insanovation White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to retrieve an image from the MySQL DB and display it in my JSP. The code below retrieves and displays the image with success. But, an exception is thrown:

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?
 
Robert Insanovation White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resolved:

1) added this directive at the top of the page <%@ page trimDirectiveWhitespaces="true" %>
2) removed blank space between the <%, %> tags and the beginning of code
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this code in a JSP rather than a servlet? :confused:
 
Robert Insanovation White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault, I already put it in a Servlet =D
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify this for other people that might come across this post, HTTP protocol is one request --> one response.
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More details in this article.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!