FileDocCategorySizeDatePackage
TestBlobServlet.javaAPI DocExample3061Tue Jul 10 17:23:20 BST 2001None

TestBlobServlet

public class TestBlobServlet extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


    ServletOutputStream out = response.getOutputStream();

    Blob       photo      = null;
    Connection connection = CacheConnection.checkOut();
    Statement  statement  = null;
    ResultSet  resultSet  = null;
    String     sql        =
     "select photo " +
     "from   person p, person_information i " +
     "where  p.person_id = i.person_id " +
     "and    last_name = " + formatWithTicks(request.getParameter("last_name")) + " " +
     "and    first_name = " + formatWithTicks(request.getParameter("first_name"));

    try { 
      statement = connection.createStatement();
      resultSet = statement.executeQuery(sql);
      if (resultSet.next()) {
        photo = resultSet.getBlob(1);
      }
      else {
        response.setContentType("text/html");
        out.println("<html><head><title>Person Photo</title></head>");
        out.println("<body><h1>No data found</h1></body></html>");
        return;
      }
      response.setContentType("image/gif");

      InputStream in = photo.getBinaryStream();
      System.out.println("after getBinaryStream");

      int length     = (int)photo.length();
      System.out.println("lenght of the blob is " + length);

      int bufferSize = 1024;
      System.out.println("buffer size is " + bufferSize);

      byte[] buffer = new byte[bufferSize];

      while ((length = in.read(buffer)) != -1) {
        System.out.println("writing " + length + " bytes");
        out.write(buffer, 0, length);
      }

      System.out.println("written");
      in.close();
      out.flush();
    }
    catch (SQLException e) {
      System.out.println("TestBlobServlet.doGet() SQLException: " + 
       e.getMessage() + "executing ");
      System.out.println(sql);
    }
    finally {
      if (resultSet != null) 
        try { resultSet.close(); } catch (SQLException ignore) { }
      if (statement != null) 
        try { statement.close(); } catch (SQLException ignore) { }
    }
    // let's return the conection
    CacheConnection.checkIn(connection);
  
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

    doGet(request, response);
  
private java.lang.StringformatWithTicks(java.lang.String string)

    if (string != null) {
      char[]       in  = string.toCharArray();
      StringBuffer out = new StringBuffer((int)(in.length * 1.1));
      if (in.length > 0)
        out.append("'");
      for (int i=0;i < in.length;i++) {
        out.append(in[i]);
        if (in[i] == '\'") 
          out.append(in[i]);
      }
      if (in.length > 0)
        out.append("'");
      return out.toString();
    }
    else {
      return "NULL";
    }