First i have a method 'insertimage' for inserting the image in the database and then I have used a Servlet class and called the method 'insertimage' for inserting operation.I have a bean class property 'image' of Part type and I have used a setter method of the bean class and set the image which i got from the index page. please help with the code for retrieving the image and displaying it in a jsp page
inserting the image the database
public boolean insertimage(FoodItems food)
{
boolean result=false;
try
{
InputStream inputstream=null;
image=food.getImage();// i have a bean class property of Part type
if(image !=null)
{
long fileSize=image.getSize();
String fileContent=image.getContentType();
inputstream=image.getInputStream();
}
PreparedStatement pst=con.prepareStatement("insert into AvailableItems values(?)");
pst.setBlob(1,inputstream);
pst.executeUpdate();
result=true;
}
catch(Exception e)
{
System.out.println("error st Available insert"+e);
}
return result;
}
//servlert class
@MultipartConfig(maxFileSize=169999999)
@WebServlet("/InsertFoods")
public class InsertFoods extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Part image=request.getPart("image");
DBOperations db=new DBOperations();
FoodItems food=new FoodItems();
food.setImage(image);
if(db.insertimage(food))
{
response.sendRedirect("AvailableItems.jsp");
}
else
{
pw.println("not inserted");
}
}
}