View Image in jsp

Reply

Join Date: Dec 2007
Posts: 6
Reputation: narayanabhilash is an unknown quantity at this point 
Solved Threads: 0
narayanabhilash narayanabhilash is offline Offline
Newbie Poster

View Image in jsp

 
0
  #1
Mar 17th, 2008
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..

  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@page import="com.abc.DBCon"%>
  4. <%@page import="java.io.FileOutputStream"%>
  5. <%@page import="java.sql.Connection"%>
  6. <%@page import="java.sql.ResultSet"%>
  7. <%@page import="java.sql.Statement"%>
  8. <%@page import="java.io.File"%>
  9. <%@page import="java.io.OutputStream"%>
  10.  
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  14. <title>Hello JSP Upload</title>
  15. </head>
  16. <body>
  17. <%
  18. int myid=Integer.parseInt(request.getParameter("id"));
  19.  
  20. Connection con=null;
  21. try
  22. {
  23. DBCon db= new DBCon();
  24. String sql=" select photo from photo where username='"+<%session.getAttribute("user")%>+"' and id="+myid;
  25. con=db.getCon();
  26. con.setAutoCommit(false);
  27. Statement st=con.createStatement();
  28. ResultSet rs= st.executeQuery(sql);
  29.  
  30. while(rs.next())
  31. {
  32. byte[] imgData =rs.getBytes("photo");
  33. response.setContentType("image/gif");
  34. OutputStream o = response.getOutputStream();
  35. o.write(imgData);
  36. o.flush();
  37. o.close();
  38. }
  39. }catch(Exception ex){ex.printStackTrace();}
  40. finally
  41. {
  42. try
  43. {
  44. con.close();
  45. }
  46. catch(Exception ex){ex.printStackTrace();}
  47. }
  48. %>
  49.  
  50. </body>
  51. </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.



  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@page import="com.svg.DBCon"%>
  4. <%@page import="java.io.FileOutputStream"%>
  5. <%@page import="java.sql.Connection"%>
  6. <%@page import="java.sql.ResultSet"%>
  7. <%@page import="java.sql.Statement"%>
  8. <%@page import="java.io.File"%>
  9. <%@page import="java.io.OutputStream"%>
  10.  
  11. <html>
  12.  
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  15. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  16. <meta name="ProgId" content="FrontPage.Editor.Document">
  17. <title>New Page 1</title>
  18. </head>
  19.  
  20. <body>
  21.  
  22. <table border="1" width="100%" height="836">
  23. <tr>
  24. <td width="33%" height="157"></td>
  25. <td width="23%" height="157"></td>
  26. <td width="44%" height="157"></td>
  27. </tr>
  28. <tr>
  29. <td width="33%" height="216"></td>
  30. <td width="23%" height="216" nowrap scope="col">
  31. <%
  32. int myid=Integer.parseInt(request.getParameter("id"));
  33. Connection con=null;
  34. try
  35. {
  36. DBCon db= new DBCon();
  37. String sql=" select photo from photo where username='"+<%session.getAttribute("user")%>+"' and resumeid="+myid;
  38. con=db.getCon();
  39. con.setAutoCommit(false);
  40. Statement st=con.createStatement();
  41. ResultSet rs= st.executeQuery(sql);
  42.  
  43. while(rs.next())
  44. {
  45. byte[] imgData =rs.getBytes("photo");
  46. response.setContentType("image/gif");
  47. OutputStream o = response.getOutputStream();
  48. o.write(imgData);
  49. o.flush();
  50. o.close();
  51. }
  52. }catch(Exception ex){ex.printStackTrace();}
  53. finally
  54. {
  55. try
  56. {
  57. con.close();
  58. }
  59. catch(Exception ex){ex.printStackTrace();}
  60. }
  61. %>
  62. </td>
  63. <td width="44%" height="216"></td>
  64. </tr>
  65. <tr>
  66. <td width="33%" height="445"></td>
  67. <td width="23%" height="445"></td>
  68. <td width="44%" height="445"></td>
  69. </tr>
  70. </table>
  71.  
  72. </body>
  73.  
  74. </html>


Thanks in Advance..
Last edited by peter_budo; Mar 17th, 2008 at 10:48 pm. Reason: Keep It Organized - please use [code] tags
Abhilash Narayan
Java Programmer
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: View Image in jsp

 
0
  #2
Mar 17th, 2008
NEVER use Java code in JSP.
NEVER try to mix binary and text data in a ServletOutputStream.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC