Hello, my name is peggie.
Currently I am doing a project on online store whereby I am required to display my product images dynamically in jsp page.
I would like to call the image from servlet and display it.
However I do not know the correct codes to be display in jsp.

Here are my complete codes:
Code:
package sg.nyp.edu.sit;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Blob;

/**
 * Servlet implementation class RetreiveImage
 */
public class RetreiveImage extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RetreiveImage() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String connectionURL = "jdbc:mysql://localhost:3306/images";
		java.sql.Connection con=null;
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con=DriverManager.getConnection(connectionURL,"root","");
			Statement st1=con.createStatement();
			ResultSet rs1 = st1.executeQuery("select image from picture where pictureid='1'");
			String imgLen="";
			if(rs1.next()){
				int len = imgLen.length();
				byte [] rb = new byte[len];
				InputStream readImg = rs1.getBinaryStream(1);
				int index=readImg.read(rb, 0, len); 
				System.out.println("index"+index);
				st1.close();
				response.reset();
				response.setContentType("image/jpg");
				response.getOutputStream().write(rb,0,len);
				response.getOutputStream().flush(); 

			}
} catch (Exception e){
	e.printStackTrace();
	
	
	
	
}
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
Code:
package sg.nyp.edu.sit;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Blob;

/**
 * Servlet implementation class InsertImage
 */
public class InsertImage extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public InsertImage() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();
		String connectionURL = "jdbc:mysql://localhost:3306/images";
		Connection con=null;
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(connectionURL, "root", "");
			PreparedStatement ps = con.prepareStatement("INSERT INTO picture VALUES(?,?)");
			File file = new File("C:/Beannie/1102.jpg");
			FileInputStream fs = new FileInputStream(file);
			ps.setInt(1,1);
			ps.setBinaryStream(2,fs,fs.available());
			int i = ps.executeUpdate();
			if(i!=0){
				pw.println("image inserted successfully");
			}
			else{
				pw.println("problem in image insertion");
			}
			} catch (Exception e){
				System.out.println(e);
				
			}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
And my java class that have getter as well as setter
Code:
package sg.nyp.edu.sit.model;

import com.mysql.jdbc.Blob;

public class Image {
	int pictureid;
	Blob image;
	public int getPictureid() {
		return pictureid;
	}
	public void setPictureid(int pictureid) {
		this.pictureid = pictureid;
	}
	public Blob getImage() {
		return image;
	}
	public void setImage(Blob image) {
		this.image = image;
	}

}
Lastly, this is my jsp page that displays out nothing.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example</title>
</head>
<body> 
<form name="form1" method="get" action="./RetrieveImage">


<img src="DisplayImage.jsp?pictureid=1" width="115" border="0" >


          

</body>
</html>
My database is called picture
Attributes are:
pictureid(int)
image(blob)
Thanks
Hope to receive ur reply soon!