Image - Generation JSP examples

User 48fe63575a

28-10-2009 12:41:27

Hi everyone - hope that you are all well.


I have a question on displaying images in JSP and hopefully I can get some pointers.


I have created a simple test database of ID, name and structure image in Oracle.  The structure images were created using MarvinBeans as png files and loaded back into the database.


I can search the database by name and this returns rows in a table.  This is done using JSP.  What I would like to do is stream the image file from the Oracle database back to the JSP results page to appear alongside the name and ID in the results table.


I have found and installed the Image-Generation JSP examples on my local Apache-Tomcat instance - and they work fine.  Although examples 3 and 5 are close in that they can define the image type, I'm a bit confused on how to stream the image back from the results set.  Do I make use of the generate_image.jsp file?  Like I said I have the image as a BLOB in the result set - can I just display my image without the need to pass molfile or smiles strings?


I hope that someone can help and many thanks in advance,
Wyn

ChemAxon 7c2d26e5cf

28-10-2009 13:21:15

No, you do not need generate_image.jsp. But it can be a good example how to generate image content.


There are two ways to display image stream in the browser.


1. The result that the web server provides is the image itself.


In this case, define the HTTP header as an image and push the image stream directly into the response. The first example among Image Generation examples demonstrates it. The generate_image.jsp creates an image stream with the help of the Marvin Beans API than write it into the response stream.
So you need the following code:


response.setContentType("image/png");
byte[] b; // give image stream into the byte array
ServletOutputStream outs = response.getOutputStream();
outs.flush();
outs.write(b,0,b.length); // write out image
outs.close();

2. If you would like to embed into a web page, use the following html code:


<img src="URI_OF_THE_IMAGE" width=200 height=200>

The URI can be the URL of the permanently existing image (like myimage.png) or it can be a jsp or php page that provides image content dynamically (like "image.jsp?id=101" where "id=101" can be a parameter that is passed to the jsp code). In the second case, you can use the jsp sniplet that I inserted above.

I suggest to create an own jsp page that read image source from the database and write this stream into the response code. You can refer this jsp directly (like in the first case) or you can use it into an img tag to embed image into a web page.