MySqlTalk.com  

Go Back   MySqlTalk.com > Programming Languages and MySQL > Java
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 04-30-2005, 08:23 AM   #1
gertcuppens
Member
 
Join Date: Feb 2005
Location: ANtwerp - Belgium
Posts: 34 gertcuppens is on a distinguished road
How to display a BLOB in java

In my database I have a table containing a blob. This blob is the poster of a corresponding film. To display a blob inside a JSP, you have to refer to a servlet, like this :

Code:
<td width="200" rowspan="6"><div align="center"><img src="/gco/cluifimg?nocache=<%=new java.util.Date()%>" width="150" height="200"></div></td>

The parameter after /gco/cluifimg is only necessary to prevent the first blob shown to be left in the cache. Without this parameter, I only showed the poster of the first film, and after that the cache showed always this first poster with whatever movie was selected.

The /gco/cluifimg is inside the web.xml to let Tomcat know that the servlet to show the blob is called imageServlet.
ANd here's the code for this servlet.

Code:
/* * Deze servlet dient om gegevens uit een BLOB terug om te zetten * in een JPG of GIF. Eerste toepassing hiervoor is het afbeelden * van de posters die in de CLUIF-databank zitten */ package org.gertcuppens.controller; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.ServletException; import java.io.OutputStream; import java.io.IOException; import java.sql.Blob; import java.sql.SQLException; import org.apache.log4j.Logger; //import org.gertcuppens.algemeen.Melding; import org.gertcuppens.cluif.Film; /** * @author GC1494 * * */ public class ImageServlet extends HttpServlet { public void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException { byte[] blobBytesArray = null; OutputStream stream = _response.getOutputStream(); System.out.println("in imageservlet"); Logger gcoLogger = Logger.getLogger("gco.log"); gcoLogger.debug("in imageServlet "); HttpSession sessie = _request.getSession(false); if (sessie != null) { System.out.println("sessie opgehaald"); Film film = (Film) sessie.getAttribute("film"); if (film != null) { System.out.println("film opgehaald"); gcoLogger.debug("imageservlet - filmnummer " + film.getFilmnummer()); try { Blob poster = film.getPoster(); if (poster != null) { int len = new Integer( new Long( poster.length()).toString() ).intValue(); blobBytesArray = poster.getBytes(1,len); if (blobBytesArray != null) { gcoLogger.debug("imageservlet - blob <> null "); if (blobBytesArray.length > 0 ) { gcoLogger.debug("imageservlet - blob.length > 0 "); stream.write(blobBytesArray); } } /* blobBytesArray != null */ } /* poster != null */ } catch (SQLException sqlEx) { // Melding melding = new Melding(sqlEx); System.out.println("sqlEx bij blob"); gcoLogger.debug("sqlEx bij blob"); } catch (IOException ioEx) { // Melding melding = new Melding(sqlEx); System.out.println("ioEx bij blob"); gcoLogger.debug("ioEx bij blob"); } } /* film != null */ } /* (sessie != null) */ } /* public void service() */ } /* public class ImageServlet */
gertcuppens is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Links to Java Tutorials Administrator Java 0 07-26-2004 08:29 PM



All times are GMT -4. The time now is 01:23 PM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".