How to display an image that is stored in a table in Mysql , on a JSP page
pavan manthan
Greenhorn
Joined: Dec 20, 2008
Posts: 6
posted
0
Sorry for reposting this question. I put it in the wrong section at first. I am sure this belongs to the JSP section :)
Hi,
As the sub says .. I have been out of luck today trying to display an image that is inserted into a mysql table on a JSP page. The JSp code to do this is given below. the output of this page when i run it : displays the URL of the page i.e
http://localhost:8080/abc/jsp/helloworld.jsp as an image instead of the actual image from the table. Please help me before I get crazy trying to get this thing working :)
<%@ page language="java"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@page import="com.pav.DAO.*" %>
<%@page import="com.pav.forms.*" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Connection dbconn = null;
PreparedStatement psmt = null;
ResultSet res=null;
InputStream sImage = null;
UserRequirementsForm frm = new UserRequirementsForm();
try
{
dbconn= dBInteraction.getConn();
psmt=dbconn.prepareStatement("SELECT images from photos WHERE userFirstName = ?");
psmt.setString(1,"rrr");
res=psmt.executeQuery();
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
res.next();
Blob b = res.getBlob(1);
byte[] bytearray = new byte[(int)b.length()];
int size=0;
sImage = res.getBinaryStream(1);
while((size=sImage.read(bytearray))!= -1)
{
o.write(bytearray);
}
response.flushBuffer();
sImage.close();
Please do not post the same question more than once. If you incorrectly post a question in the wrong section, please ask a moderator to move the post for you.