• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retreiving a image from a database using servlet

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic