This is a servlet I use for my own. You can easily modify it to read the binary date from your database. Important part is the contenttype you send out. It should be image/jpeg or image/gif.
Extract:
import javax.servlet.http.*;
import javax.servlet.*;
import com.hd.common.web.servlet.*;
import java.io.IOException;
import java.io.*;
/** A servlet that reads a GIF file off the local system
* and sends it to the client with the appropriate MIME type.
* Includes the Content-Length header to support the
* use of persistent HTTP connections unless explicitly
* instructed not to through "usePersistence=no".
* Used by the PersistentConnection servlet.
*/
/**
* <p>Title: ImageRetrieverServlet</p>
* <p>Description: retrieves a image from a permanent source</p>
* <p>Copyright: Copyright (c) 2003</p>
* @author Fabian Dierckxsens
* @version 1.0
*/
public class ImageRetrieverServlet extends HttpServlet {
private static String sourcePath = "c:/";
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
/**
* This call is critical! The ServletConfig object is used elsewhere in
* the servlet, and the init method of the superclass registers it where
* the servlet can find it later.
*/
super.init(config);
try {
sourcePath = config.getInitParameter("sourcePath"

;
}
catch (Exception ex) {
System.out.println(ex);
}
}
public ImageRetrieverServlet() {
}
public void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String picLocation = request.getParameter("picLocation"

;
if ((picLocation == null)
|| (picLocation.length() == 0)) {
reportError(response, "Image File Not Specified"

;
return;
}
//String file = sourcePath + getServletContext().getRealPath(picLocation);
String file = sourcePath + (picLocation.startsWith("/"

? "" : "/"

+ picLocation;
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
int imageByte;
while((imageByte = in.read()) != -1) {
byteStream.write(imageByte);
}
in.close();
String persistenceFlag =
request.getParameter("usePersistence"

;
boolean usePersistence =
( (persistenceFlag == null)
|| (!persistenceFlag.equals("no"

));
response.setContentType((file.endsWith(".gif"

? "image/gif" : "image/jpeg"

);
if (usePersistence) {
response.setContentLength(byteStream.size());
}
byteStream.writeTo(response.getOutputStream());
}
catch(IOException ioe) {
reportError(response, "Error: " + ioe);
}
}
public void reportError(HttpServletResponse response, String message)
throws IOException {
response.sendError(response.SC_NOT_FOUND, message);
}
}
Should be helpfull
Fabian Dierckxsens