Displaying an image and its attributes in JSP

Reply

Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Displaying an image and its attributes in JSP

 
0
  #1
Jul 20th, 2009
Hello to all. I'm using the following jsp to display an image from a mysql database.

  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="java.io.*" %>
  3.  
  4. <%
  5. Connection connection = null;
  6. String connectionURL = "jdbc:mysql://localhost:3306/people";
  7.  
  8. ResultSet rs = null;
  9. PreparedStatement psmnt = null;
  10. InputStream sImage;
  11.  
  12. try {
  13.  
  14. Class.forName("com.mysql.jdbc.Driver").newInstance();
  15. connection = DriverManager.getConnection(connectionURL, "root", "123");
  16.  
  17. psmnt = connection.prepareStatement("SELECT pic FROM person WHERE id = ?");
  18. psmnt.setString(1, "9");
  19. rs = psmnt.executeQuery();
  20.  
  21. %>
  22.  
  23.  
  24.  
  25. <html>
  26. <head>
  27. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  28. <title>JSP Page</title>
  29. </head>
  30. <body>
  31. <h1>Hello World!</h1>
  32.  
  33. <%
  34. if(rs.next()) {
  35. byte[] bytearray = new byte[1048576];
  36. int size=0;
  37. sImage = rs.getBinaryStream(1);
  38. response.reset();
  39. response.setContentType("image/jpeg");
  40. while((size=sImage.read(bytearray))!= -1 ){
  41. response.getOutputStream().write(bytearray,0,size);
  42. }//End of while
  43. }//End of if
  44.  
  45. %>
  46. <p>
  47. This is some text.
  48. </p>
  49.  
  50. <%}
  51. catch(Exception ex){
  52. out.println("error :"+ex);
  53. }
  54. finally {
  55. // close all the connections.
  56. rs.close();
  57. psmnt.close();
  58. connection.close();
  59. }
  60. %>
  61. </body>
  62. </html>

And thus far it seems to work, well sort of. The problem here is that I can't get the text I put in the h1 and p tags to show. Another problem I'm having is that whenever I try to display another attribute of the the table (like the name of the image) I get an error which says "getOutputStream() has already been called for this response ". Is there a way I can display the image and the html? as well as the other attributes of the image?

Any suggestions will be greatly appreciated. Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 97
Reputation: Alex_ is an unknown quantity at this point 
Solved Threads: 2
Alex_'s Avatar
Alex_ Alex_ is offline Offline
Junior Poster in Training

Re: Displaying an image and its attributes in JSP

 
0
  #2
Jul 21st, 2009
Hello CodeBoy101,
1. I don't think this code compiles because i don't see where you declared a "out" object for the following code.
  1. catch(Exception ex){
  2. out.println("error :"+ex);
  3. }
2. This is just a gues, but maybe you need to create an object of the output stream, like this:
  1. outputStream.getOutputStream();
  2. while((size=sImage.read(bytearray))!= -1 ){
  3. response.outputStream.write(bytearray,0,size);
  4. }//End of while
But i'm not too sure about any of these. Hope it helps ) .
Fundamental law of life:
do{ ThingsToDo+=me.CompleteTask(ThingsToDo); }while(ThingsToDo); Die(me);
Law of the Spirit:
do{ Rebuke(me); }while(!me.Repented); LiveEternal(me);
PM me to know more why i wrote this or what it means.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Displaying an image and its attributes in JSP

 
0
  #3
Jul 21st, 2009
Alex_,
out - out is an intrinsic page object.
Failure is not fatal, but failure to change might be. - John Wooden
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