This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes Display IMG from Blob database field ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Display IMG from Blob database field ?" Watch "Display IMG from Blob database field ?" New topic
Author

Display IMG from Blob database field ?

Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
Posts: 75
Hi there

I would like to create a JSP where the user can click and display a picture in a new browser window.

Pictures are stored in database as BLOBs.
I succesfully managed to code this in a servlet with a simple BufferedOutputStream straight forward the HttpServletResponse as a new window.

The problem is that i cannot handle the window size according to the image's real size... The browser (IE) use a default size. Far too big most of time

Does anyone know a trick to specify the size of the new window ?
Or maybe there is another way to solve the problem...

Notes :
- I don't know the size of the image before the user click on the link
- I need to handle a binary Stream since I don't have possibility to store the picture as temporary file on the server

Thanx in advance
[ December 02, 2004: Message edited by: Franck Tranchant ]

Franck<p>--------------------
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

I'll assume that you are showing the image via an <img> tag that references the servlet that fetches the image from the DB.

In order to open a window of a specific size, you must use the Javascript window.open() method, and must know the dimensions you want.

So here's one approach:

1) When the link is clicked, a request is submitted to a hidden iframe on the page. This way, you can submit something to the server without changing the current page.

2) The "hidden" request looks up the size of the image to be displayed and returns a response that contains the Javascript necessary to open the new window to the size you desire.

3) The URL specified for the new window references a JSP that contains the <img>tag that displays the image.

Using this method you need two server trips per image: one to look up the size and one to actually display the image.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
Posts: 75
I solved this issue using another approach :

Instead of providing the user a link pointing on the Servlet (<A TARGET="_blank" HREF="retrievePicture?someParam=xxx">) , I provide a link pointing on a JSP.

In this JSP, the HTML tag <IMG> is included similar to this one : <IMG SRC="retrievePicture?someParam=xxx">

Using a simple JavaScript code, I can easily resize the window size according to the IMG size...
[ December 06, 2004: Message edited by: Franck Tranchant ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Display IMG from Blob database field ?