| | |
Displaying an image and its attributes in JSP
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello to all. I'm using the following jsp to display an image from a mysql database.
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.
JSP Syntax (Toggle Plain Text)
<%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/people"; ResultSet rs = null; PreparedStatement psmnt = null; InputStream sImage; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "123"); psmnt = connection.prepareStatement("SELECT pic FROM person WHERE id = ?"); psmnt.setString(1, "9"); rs = psmnt.executeQuery(); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <% if(rs.next()) { byte[] bytearray = new byte[1048576]; int size=0; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType("image/jpeg"); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); }//End of while }//End of if %> <p> This is some text. </p> <%} catch(Exception ex){ out.println("error :"+ex); } finally { // close all the connections. rs.close(); psmnt.close(); connection.close(); } %> </body> </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.
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.
2. This is just a gues, but maybe you need to create an object of the output stream, like this:
But i'm not too sure about any of these. Hope it helps ) .
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)
catch(Exception ex){ out.println("error :"+ex); }
jsp Syntax (Toggle Plain Text)
outputStream.getOutputStream(); while((size=sImage.read(bytearray))!= -1 ){ response.outputStream.write(bytearray,0,size); }//End of while
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.
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.
![]() |
Similar Threads
- Proble displaying images dynamically in jsp (JSP)
- Store and retrieve image from mysql db with JSP (JSP)
- displaying image from database (PHP)
- Displaying an image in JSP using expressions tag (JSP)
- View Image in jsp (JSP)
- Two monitors displaying identical image? (Monitors, Displays and Video Cards)
- Displaying language other than English on JSP (JSP)
- how to display one image per day from an array? (JSP)
Other Threads in the JSP Forum
- Previous Thread: Code to compare system date and date from database
- Next Thread: i need source code for online examination application in jsp
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






