Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying BLOB image in a jsp page

 
meera rao
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to display an image in a jsp page which is stored as a blob in the oracle table.

I wrote a stored procedure to get all the images for a particular id.

and in the servlet, I wrote the following:



I am passing the Arraylist as a request Attribute to the jsp page, from where the Img tag calls a servlet passing the byte[].

<img name= "titleImage"src="/Images?<bean:write name="images" property="imgData"/>" >

the servlet converts the byte to image and writes as an OutputStream



but I think when the byte[] gets passed from the jsp page to the servlet, it is sending it as null. can somebody tell me where i am doing wrong
[ September 23, 2005: Message edited by: meera rao ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Images are requested by the client in a separate HTTP GET. You'd have to create another Action that takes that separate request and returns the contents of the graphic along with the appropriate MIME type. Strut's "image" tag may be a starting point for you.
 
meera rao
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain me in some more detail?
 
meera rao
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have edited the first post, can anybody help me out
 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meera

As Nathaniel previously said, all you need to do is to code a Struts Action that will take care of the picture 'retrieval', then call this action from the image tag of your JSP.

For exemple, you you might have a DisplayImageAction class with an execute method in it.
This action is supposed to return a stream of bytes, so you may set the returning type of the execute method to void instead of the default ActionForward since no forward is involved here.

Code the access of your stored procedure right into the execute method.
Basically, get a byte[] containing your picture, set the ContentType of your response (at least "image"), then send the bytes to your response outputStream (response.getOutputStream).

Once it's done, just call your Action from your JSP with basic HTML tag or Struts IMG tag. Don't forget to send a parameter in your request and use it in the Action to identify the file you need to retrieve :

<img src="displayImage.do?imageID=123" border="0">

Hope it helps...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic