There is much talk about how to display images through JSP pages or Servlets when they are stored in a database as a Blob field, I personally found it quite ideal to use a servlet and simply invoke it through a dynamic page with the following method:
Notice that the following example can be invoked either statically or dynamically:
Static Context
<img src='showImage?id=1>
Dynamic Context
<img src='showImage?id=" + ImageID + "'>
The following is the servlet that will handle the invocation sent from above (both doPost and doGet)
Furthermmore it has been simulated for mySQL Database and the stored attribute is a blob jpeg image.
showImage.java
import java.io.*;
import java.text.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* Author: Kyriakos T. Fourniadis
* Scope: Display Image Servlet
* Version: 1.0
*
*/
public class showImage extends HttpServlet {
private Connection con;
private Statement stmt;
private String trace;
/**
* Setup database connection and create SQL statement
*/
public void init( ServletConfig config ) throws ServletException
{
// Try database connection
try{
// The call to Class.forName explicitly loads the driver class
//Class.forName("org.gjt.mm.mysql.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
//jdbc:mysql://<HOST>:<PORT>/<DB>
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=''&password=''");
con = DriverManager.getConnection("jdbc:mysql://localhost/mydb");
// Once a connection has been established we can create an instance
// of Statement, through which we will send queries to the database.
stmt = con.createStatement();
}
catch (Exception exception ) {
exception.printStackTrace();
throw new UnavailableException(exception.getMessage());
}
} // End init()
/**
* Forward to doGet
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/*
* NOTE: doPost -> HTTP post requests | doGet -> HTTP get resuest
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// Simple trace variable for debugging...
trace = "";
/*
SELECT *
FROM image
WHERE image.image_id = ?variable?
*/
try {
String SQL =
"SELECT * " +
"FROM image " +
"WHERE image.image_id = '" + request.getParameter("id") + "'";
ResultSet rs = stmt.executeQuery(SQL);
rs.next();
Blob blob = null;
blob = rs.getBlob("image");
response.setContentType("image/jpeg");
System.out.println("just above OutputStream");
InputStream in = blob.getBinaryStream();
OutputStream out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.flush();
out.close();
}
catch (Exception exception ) {
throw new ServletException("Error");
}
} // end doPost()
/*
* Close SQL statements & database connection
*/
public void destroy()
{
// Attempt to close statements and database connection
try{
stmt.close();
con.close();
}
// Handle database exceptions by returning error to client
catch (SQLException sqlException){
sqlException.printStackTrace();
}
} // End destroy()
} // End class showImage
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<!-- General description of your Web application -->
<display-name>
Kyriakos T. Fourniadis Java Tech. Example
</display-name>
<!-- showImage definitions -->
<servlet>
<servlet-name>showImage</servlet-name>
<description>
Display Image
</description>
<servlet-class>
showImage
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showImage</servlet-name>
<url-pattern>showImage</url-pattern>
</servlet-mapping>
</web-app>
This code is virtually plug & play, the only modification you will need is to modify the database connection and the web.xml file. Remember that the example can be invoked either from a static HTML page or request, a dynamic JSP page, or a Servlet.
Author: Kyriakos T. Fourniadis