Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image Upload  RSS feed

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an image in my database,and i want to display it on a jsp using can i write this in servlet ??
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Julie

You need to be specific about the type of database and the data type in which your images are stored (typically in Oracle: BLOB or BFILE, on older versions Long Raw).

For Oracle, you will find many useful examples on

Below a simple example of a servlet that reads the content of a BLOB column - without struts. Works on Oracle 9i R2 with JDBC thin driver (taken from OTN). The servlet is called with the parameter "imageID" to pass the value of the primary key that corresponds to the image you want to display. It uses the Oracle specific type oracle.sql.BLOB
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;

public class ReadBlobServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// the lImageID is used to read the PK from the tables that containst the BLOB
long lImageID = new Long(request.getParameter("imageID")).longValue();
String sql = "select content from img where id = ?";
String connStr = "jdbc racle:thin:@host ort:instance";
String usrStr = "usr";

ServletOutputStream lOut = response.getOutputStream();

try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(connStr, usrStr,usrStr);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setLong(1, lImageID);
ResultSet rs = ps.executeQuery();
while ( {
// using oracle.sql.BLOB and oracle.jdbc.driver.OracleResultSet datatype
BLOB b = ((OracleResultSet)rs).getBLOB(1);
// using the java.sql.Blob data type
// Blob b = rs.getBlob(1);
long len = b.length();
InputStream is = b.getBinaryStream();
byte[] data = new byte[(int) len];
int byte_read =;


} catch (SQLException sqle) {
} catch (IOException ioe) {
} // service
HTH and good luck
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't there be a way to do it with a JNDI/JDBC Datasource, in a way that's not specific to a database vendor?

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!