JSP's are designed to display textual data only.  The out object is a
PrintWriter, which deals only with character data.  For binary data, such as
images, you need to use a ServletOutputStream instead.  A recent JDC
Newsletter presented an example in which getOutputStream() was called from
within a JSP scriptlet.  This works with some web servers, but it is
illegal.  The Servlet API explicitly states that if getWriter() and
getOutputStream() are called on the same response, an IllegalStateException
is thrown (and the servlet code generated from the JSP calls getWriter()).
Therefore, the correct solution is to write a servlet instead of a JSP.  (I
presume that the Struts ActionForward mechanism supports forwarding to a
servlet.)

-----Original Message-----
From: SUPRIYA MISRA [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 28, 2001 9:31 AM
To: [EMAIL PROTECTED]
Subject: How do deal with JPEG in JSP's


Hi,

I am storing jpeg images in oracle as BLOB. I am using the 
connection.getBlob() method to retrieve the images. When I write this back 
as a jpeg file it works great. How do I display the image in JSP's? Here is 
the code I am using.


<%@ page import="java.sql.* ,java.util.*" %>
<%@ page errorPage="error.jsp"%>
<%@ page buffer="50kb"%>
<jsp:useBean id="connection" class="ITTPDB" />


<%
response.setContentType("image/jpeg");
String sql="select *  from IMAGETEST";

ResultSet result=connection.executeQuery(sql);
char c;
byte [] b;
int i=0;
if (result.next()) {
Blob blob=result.getBlob("PHOTO");
b = blob.getBytes(1,(int)blob.length()+1);

//writing to file works

java.io.FileOutputStream fos = new java.io.FileOutputStream("c:\\test.jpg");
fos.write(b);
fos.close();


// Displaying in JSP does Not

while (i<blob.length()) {
c=(char)b[i++];
%>
<%=c%>
<%
  i++;
  }
}
%>


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

Reply via email to