943,831 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 9515
  • JSP RSS
Mar 17th, 2008
0

View Image in jsp

Expand Post »
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..

html Syntax (Toggle Plain Text)
  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.



html Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
narayanabhilash is offline Offline
6 posts
since Dec 2007
Mar 17th, 2008
0

Re: View Image in jsp

NEVER use Java code in JSP.
NEVER try to mix binary and text data in a ServletOutputStream.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: passing and retrieving arguments
Next Thread in JSP Forum Timeline: sending email





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC