title: Retrieve image (blob) from mysql using servlet and display it at jsp
body: <p>How can i do retrieval of image from database(mysql with blob data) and set it in table form<code>(PrintWriter pw = response.getWriter(); pw.write("&lt;img src=""/&gt;"); )</code> at servlet and display it at JSP page, making a table dynamically(base on my database data)(Although i'm not sure whether can it be achievable) But if it is achievable, how can i do that? If it is not achievable. What is the suggested possible answer? Your help will be much appreciated.
  Here is my code :</p>
  
  <pre><code>     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               byte[] binaryStream;
          ArrayList&lt;RetrievingPost&gt; combine=new ArrayList&lt;RetrievingPost&gt;();
  
             try{
                               // Register JDBC driver
                               Class.forName("com.mysql.jdbc.Driver");
  
                               // Open a connection
                               Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/asq?characterEncoding=UTF-8&amp;useSSL=false", "root", "root");
  
                               // Execute SQL query
                               Statement pst = connection.createStatement();
                               String sql;
                               sql = "select profilepic, displayname, r.userId, friendId from registration r inner join friendlist fl on r.userId=fl.userId where r.emailAddress='" + emailAddress + "'";
                               ResultSet rs = pst.executeQuery(sql);
  
                               // Extract data from result set
                               while(rs.next()){
                                  //Retrieve by column name
                           Retrieving retrieve=new Retrieve();
                                  userId  = rs.getInt("userId");
                                   System.out.println(userId+"---------userId-------");
                                  friendId =rs.getInt("friendId");
                                  displayname=rs.getString("displayname");
  
                                binaryStream = rs.getBytes("profilepic");
      retrieve.setFriendId(friendId);
      retrieve.setDisplayName(displayname);
       retrieve.setBinaryStream(binaryStream);
       Combine.add(retrieve);
                          }
  
  
                               // Clean-up environment
                               rs.close();
                               pst.close();
                               connection.close();
                            }catch(SQLException se){
                               //Handle errors for JDBC
                               se.printStackTrace();
                            }catch(Exception e){
                               //Handle errors for Class.forName
                               e.printStackTrace();
                            }finally{
                               //finally block used to close resources
                               try{
                                  if(stmt!=null)
                                     stmt.close();
                               }catch(SQLException se2){
                               }// nothing we can do
                               try{
                                  if(conn!=null)
                                  conn.close();
                               }catch(SQLException se){
                                  se.printStackTrace();
                               }//end finally try
                            } //end try
  
          for(int j=0;j&lt;combine.size();j++){
            PrintWriter pw = response.getWriter();
                                         pw.write("&lt;HTML&gt;&lt;BODY&gt;");
                                         pw.write("&lt;TABLE border='1'&gt;");
                                         pw.write("&lt;TR&gt;");
                                         pw.write("&lt;TD&gt;");
  
  
  
                                         pw.write(combine.get(i).getDisplayname());
                                         pw.write("&lt;/TD&gt;");
                                      //if possible, i would like to display my image into the table
                                         pw.write("&lt;TD&gt;");
                                         String tPosted=combine.get(i).getTimePosted().toString();
                                         pw.write(tPosted);
                                         pw.write("&lt;/TD&gt;");
                                         pw.write("&lt;/TR&gt;");
                                       }}
  getServletContext().getRequestDispatcher("/DisplayingImage.jsp").include(request, response);}
  </code></pre>
  
  <p>This is my bean</p>
  
  <pre><code>package registration;
  
  import java.io.InputStream;
  import java.sql.Timestamp;
  import java.util.Date;
  
  import javax.servlet.http.Part;
  
  public class RetrievingPost {
  int userId;
  int friendId;
  String privacyDesc;
  String status;
  String description;
  Timestamp timePosted;
  String category;
  String displayname;
  
  byte[] binaryStream ;
  public byte[] getBinaryStream() {
      return binaryStream;
  }
  public void setBinaryStream(byte[]  binaryStream) {
      this.binaryStream = binaryStream;
  }
  
  public String getDisplayname() {
      return displayname;
  }
  public void setDisplayname(String displayname) {
      this.displayname = displayname;
  }
  public String getCategory() {
      return category;
  }
  public void setCategory(String category) {
      this.category = category;
  }
  public RetrievingPost(int userId,Timestamp timePosted, String privacyDesc, String status, String post, int friendId){
      this.userId=userId;
      this.timePosted=timePosted;
      this.privacyDesc=privacyDesc;
      this.status=status;
      this.description=description;
      this.friendId=friendId;
  
  }
  public RetrievingPost(){
      super();
  }
  
  
  
  public int getUserId() {
      return userId;
  }
  public void setUserId(int userId) {
      this.userId = userId;
  }
  public int getFriendId() {
      return friendId;
  }
  public void setFriendId(int friendId) {
      this.friendId = friendId;
  }
  public String getPrivacyDesc() {
      return privacyDesc;
  }
  public void setPrivacyDesc(String privacyDesc) {
      this.privacyDesc = privacyDesc;
  }
  public String getStatus() {
      return status;
  }
  public void setStatus(String status) {
      this.status = status;
  }
  public String getDescription() {
      return description;
  }
  public void setDescription(String description) {
      this.description = description;
  }
  public Timestamp getTimePosted() {
      return timePosted;
  }
  public void setTimePosted(Timestamp timePosted) {
      this.timePosted = timePosted;
  }
  }
  </code></pre>