I´m developed an web aplication in netbeans using html and jsp, and well I have to display a image from a databese in a index.
There is the method in the class that manager the database
public byte[] getImg(Connection connect, User user) throws SQLException {
byte[] blobAsBytes = null;
Statement statement = null;
ResultSet rs = null;
statement = connect.createStatement();
rs = statement.executeQuery("select * from user");
while (rs.next()) {
if (rs.getString("nickName").equals(user.getNickName())) {
Blob blob = rs.getBlob("img");
int blobLength = (int) blob.length();
blobAsBytes = blob.getBytes(1, blobLength);
blob.free();
}
}
return blobAsBytes;
}
Then I try to use it in a jsp page.
<jsp:useBean id="database" scope="session" type="Datos.ControlDatos"/>
<jsp:useBean id="access" scope="session" type="Datos.DataAccess"/>
<%
byte[] imgData = database.getImg(access.createConnection(),user);
response.setContentType("image/jpeg");
response.getOutputStream().write(imgData);
%>
The problem is that I get this exception:
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
So there is a way to display the image as a byte array in a jsp page?