DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JSP (http://www.daniweb.com/forums/forum24.html)
-   -   View Image in jsp (http://www.daniweb.com/forums/thread114311.html)

narayanabhilash Mar 17th, 2008 4:39 am
View Image in jsp
 
Dearest One,
I have created a web application in JSP & Servlet using Netbeans IDE,
i am collecting blob image into a byte[] array and displaying in a table data "<td></td>".

when i try to display the image, i am getting an exception getWriter() already called.
but when i try in a simple jsp page. it is working properly, and again i try this, this is displaying image without any table, and if i am trying to print something before or after it, it is not displaying that data.

The Code is given Below



This Code is Working Properly..

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="com.abc.DBCon"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.io.File"%>
<%@page import="java.io.OutputStream"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello JSP Upload</title>
    </head>
    <body>
<%
int myid=Integer.parseInt(request.getParameter("id"));

    Connection con=null;
  try
  {
    DBCon db= new DBCon();
    String sql=" select photo from photo where username='"+<%session.getAttribute("user")%>+"' and id="+myid;
    con=db.getCon(); 
    con.setAutoCommit(false);
    Statement st=con.createStatement();
    ResultSet rs= st.executeQuery(sql);

    while(rs.next())
    {
      byte[] imgData =rs.getBytes("photo");
      response.setContentType("image/gif");
      OutputStream o = response.getOutputStream();
      o.write(imgData);
      o.flush();
      o.close();
      }
  }catch(Exception ex){ex.printStackTrace();}
  finally
  {
      try
      {
          con.close();
      }
      catch(Exception ex){ex.printStackTrace();}
  }
%>

    </body>
</html>



This Code is not working Properly, when i am running this code it is displaying same as previous.
but i want this image data in a fix size column.



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="com.svg.DBCon"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.io.File"%>
<%@page import="java.io.OutputStream"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>

<body>

<table border="1" width="100%" height="836">
  <tr>
    <td width="33%" height="157"></td>
    <td width="23%" height="157"></td>
    <td width="44%" height="157"></td>
  </tr>
  <tr>
    <td width="33%" height="216"></td>
    <td width="23%" height="216" nowrap scope="col">
        <%
int myid=Integer.parseInt(request.getParameter("id"));
    Connection con=null;
  try
  {
    DBCon db= new DBCon();
    String sql=" select photo from photo where username='"+<%session.getAttribute("user")%>+"' and resumeid="+myid;
    con=db.getCon(); 
    con.setAutoCommit(false);
    Statement st=con.createStatement();
    ResultSet rs= st.executeQuery(sql);

    while(rs.next())
    {
      byte[] imgData =rs.getBytes("photo");
      response.setContentType("image/gif");
      OutputStream o = response.getOutputStream();
      o.write(imgData);
      o.flush();
      o.close();
      }
  }catch(Exception ex){ex.printStackTrace();}
  finally
  {
      try
      {
          con.close();
      }
      catch(Exception ex){ex.printStackTrace();}
  }
%>
        </td>
    <td width="44%" height="216"></td>
  </tr>
  <tr>
    <td width="33%" height="445"></td>
    <td width="23%" height="445"></td>
    <td width="44%" height="445"></td>
  </tr>
</table>

</body>

</html>


Thanks in Advance..

jwenting Mar 17th, 2008 2:04 pm
Re: View Image in jsp
 
NEVER use Java code in JSP.
NEVER try to mix binary and text data in a ServletOutputStream.


All times are GMT -4. The time now is 5:18 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC