aspose file tools*
The moose likes Servlets and the fly likes Retreiving a image from a database using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retreiving a image from a database using servlet" Watch "Retreiving a image from a database using servlet" New topic
Author

Retreiving a image from a database using servlet

maher dabbas
Greenhorn

Joined: Dec 02, 2001
Posts: 26
Hi all
i'm trying to fetch an image from a database using servlet and then display it in a jsp page.
The servlet code is the following
package image;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class imageServlet extends HttpServlet {
private PreparedStatement pstmt;
ResultSet res=null;
Blob a1=null;

InputStream in=null;
Connection conn;

OutputStream os ;
public void init() {
try{
Driver Dr1=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(Dr1);
conn=DriverManager.getConnection("jdbc racle:thin:samer/samer@193.188.66.105:1521:a8i");

}catch(Exception ex){System.out.println("init ERROR");

}
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
{


try{
pstmt=conn.prepareStatement("select item_pic from e_jitcc where item_num=?");
pstmt.setInt(1,2);
res=pstmt.executeQuery();
response.setContentType("image/jpeg");
os=response.getOutputStream();
if(res.next())
{
a1=res.getBlob("item_pic");
in=a1.getBinaryStream();
System.out.println("IMAGE FOUND");

}



int c;
while ((c = in.read()) != -1) {
os.write(c);

}
}catch(Exception h)
{ h.printStackTrace();
System.out.println("Cannot count1");

}
finally{
try{
res.close();
in.close();
os.flush();
os.close();
}catch(Exception h){}
}

}
}
the JSP is

<html>
<head>
<title>
</title>
</head>
<body bgcolor="#EEEFFF">
<br>
<br>
<br>
<table border=1 align="center">
<tr>

<td>
<a href="damn">kjgkjg</a>
</td>
<td>
<img src="damn">
</td>
</tr>
<%


%>
</table>
</body>
</html>
the web.xml is
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>image.imageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/damn/*</url-pattern>
</servlet-mapping>
</web-app>
The problem is that the output is an empty block "which is generated by the <img> tag" contains no image, no exceptions throwed ,so i can't figure out where is the error
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
For debugging servlets you have to proceed methodically.
1. Have you verified that the servlet doGet is even being called when the JSP is served?
2. If the servlet is called, and the image is found, how many bytes is it writing?
Bill
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
u can check this code

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetBlob extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
try
{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con =DriverManager.getConnection("jdbc racle:thin:@oracle:1521 RCL","scott","tiger");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT picture from emp_photo where empno = 'red' and photo_format='jpg' ");
if(rs.next())
{
InputStream is = rs.getBinaryStream(1);
FileOutputStream fos = new FileOutputStream("C:/bea/user_projects/mydomain/applications/DefaultWebApp/reddy1.gif");
int i;
while ((i = is.read()) != -1)
{
fos.write (i);
}
fos.close();

}

out.println("<html><body><img src=/reddy1.gif> </body><html>");
out.println("<html><body></body><html>");
out.println("<a href=http://subra:7001/img.jsp><i>Home</i></a>");
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Note:
Here
c:/bea/user_projects/mydomain/applications/DefaultWebApp/reddy1.gif
Is the applicatuion server root path.I using weblogic server7.0.So as per server u can put the root drectory
maher dabbas
Greenhorn

Joined: Dec 02, 2001
Posts: 26
Thanks fillas, but , i've tried the code that reddy suggested, and what i got is that the image is saved in the root of the server, but the servlet could not refer to it!!!
i got another empty block contains no image, and again , no exceptions thrown, the image path is resolved correctly in the html page that is genarated by the servlet.
And about making sure that the init() method is excuting, i cheked this , and it is excuting.
I realy,realy don't know what is the problem
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
What happens if you create a static page that refers to the image with exactly the same tag that your JSP uses? (Assuming that the image file exists of course)
Bill
maher dabbas
Greenhorn

Joined: Dec 02, 2001
Posts: 26
Hello all.
Well i've made a static HTML page that refer to the image using<IMG >TAG, the surprise was that the image did not appear also!!!
any suggestions ,'cuz this thing is drivin' me crazy!!!
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retreiving a image from a database using servlet