943,882 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 1627
  • JSP RSS
Jul 20th, 2009
0

Displaying an image and its attributes in JSP

Expand Post »
Hello to all. I'm using the following jsp to display an image from a mysql database.

JSP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jul 21st, 2009
0

Re: Displaying an image and its attributes in JSP

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.
jsp Syntax (Toggle Plain Text)
  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:
jsp Syntax (Toggle Plain Text)
  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 ) .
Reputation Points: 10
Solved Threads: 3
Junior Poster
Alex_ is offline Offline
175 posts
since Jun 2008
Jul 21st, 2009
0

Re: Displaying an image and its attributes in JSP

Alex_,
out - out is an intrinsic page object.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

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: Code to compare system date and date from database
Next Thread in JSP Forum Timeline: i need source code for online examination application in jsp





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


Follow us on Twitter


© 2011 DaniWeb® LLC