PDA

View Full Version : Fail to display String and image in JSP


pcisec
09-04-02, 07:23
I have written a JSP to display Blob image and String but the page can display image only. I have no idea what happen. Can anybody point out what is the problem? Please kindly provide sample page for my reference.

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="oracle.jdbc.driver.OracleConnection,
java.sql.DriverManager,
java.sql.SQLException,
java.sql.*,
javax.sql.*,
java.io.*,
java.text.*,
java.util.*;"%>
<html>
<head>
</head>
<body>
<%
response.setContentType("text/html;charset=gb2312");

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbConnection2="jdbc: oracle:thin:@eai-dev:1523:VIS";
Connection con2=DriverManager.getConnection(dbConnection2, "apps", "apps");

Statement stmt2=con2.createStatement();
ResultSet rs2=stmt2.executeQuery("select * from fnd_lobs where file_id=75258");
if (rs2.next())
{%><%=rs2.getString(1)%><br><%}
}
catch (SQLException e)
{
System.err.println (e) ;
%>
<%=e%>
<%
}


// response.setContentType("image/jpeg");

char c;
byte [] b;
int i=0;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbConnection="jdbc: oracle:thin:@eai-dev:1523:VIS";
Connection con=DriverManager.getConnection(dbConnection, user, pwd);

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from fnd_lobs where file_id=75258");
if (rs.next())
{
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream("FILE_DATA");
b = new byte[0x7a120];
for(i = in.read(b); i != -1; )
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
}
}
catch (SQLException e)
{
System.err.println (e) ;
%>
<%=e%>
<%
}
%>
</body>
</html>

HouZhitao
09-29-02, 05:54
I think it can't display image and Strings in one page, because the page's content type must be one of "image/jpeg" or "text/html", can't be both.

You can use 2 pages :

showBoth.jsp
<%
// content type is "text/html"
out.println("your strings");
out.println("<img src='showImage.jsp?id=x'>");
%>

showImage.jsp
<%
// content type is "image/jpeg"
// show image here
%>