0

am doing this task for the first time so i google this code and try to understand what's in it . what i fond after implementing this code is that it unable to fullfilling my requirement , it only display one image insted of retreving all the images from the database as well as it threw the exception -

java.lang.IllegalStateException: getOutputStream() has already been called for this response.

after i google this exception what i'd read on this particular site about this is in this liink -

java.lang.IllegalStateException: getOutputStream() has already been called for this response

again i try to do it that way but the not get the result. whatever i did before google this exception and after i'll show you

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="index.jsp">click to display images</a>
</body>
</html>

this is my index.jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.io.*"%>

<%@ page import="java.sql.*,java.util.*"%>
<%!ServletOutputStream o; %>
<%!ArrayList<Blob> bl; %>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>men's trousers</title>
</head>
<body>


<%
try {

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306      /imgdb","root","annie1993");
PreparedStatement stmt = con.prepareStatement("select IMAGE from myimages ");
ResultSet rs = stmt.executeQuery();
//ResultSet rs1=stmt.executeQuery("select count(id) from student_profile");
bl=new ArrayList<Blob>();
while(rs.next()) {
System.out.println("st-1");
bl.add(rs.getBlob(1));
System.out.println("st-2");
}
//if(rs1.next())
//System.out.println(bl.size()+"size="+rs1.getInt(1));
for(int i=0;i<bl.size();i++){
System.out.println("st-3");
Blob b=bl.get(i);
System.out.println("st-4 "+b);
byte brr[]=new byte[(int)b.length()];
System.out.println("st-6");
brr=b.getBytes(1,(int)b.length());
System.out.println("st-7 "+brr);
// display the image
response.setContentType("image/jpg");
o = response.getOutputStream();
System.out.println("st-8 "+o);
%><table><tr><td align='left'>
<img src=<%o.write(brr); %> width="117" height="160"
>
</td></tr></table>
<%
out.println("<br>");
System.out.println("st-9 ");
}
} catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} 
%>
</div></td></tr></table></td></tr>
</table>


</body>
</html>

the result which i get is

INFO: Server startup in 6916 ms
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-3
st-4 com.mysql.jdbc.Blob@1a368ee
st-6
st-7 [B@886ce4
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@1259b5d
st-6
st-7 [B@1c9704f
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@7fc51e
st-6
st-7 [B@1d06ac2
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@bcf156
st-6
st-7 [B@bb7c6
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@4ccfac
st-6
st-7 [B@f42b5d
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
Aug 20, 2015 6:28:43 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path   [/display_multiple_images] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:578)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:183)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:184)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:43    1)
at  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

Aug 20, 2015 6:28:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path  [/display_multiple_images] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:578)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:183)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:184)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-1
st-2
st-3
st-4 com.mysql.jdbc.Blob@1280329
st-6
st-7 [B@1dc1aa
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@5de911
st-6
st-7 [B@47eb95
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@197937f
st-6
st-7 [B@1190cf6
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@1375e5
st-6
st-7 [B@12323b1
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 
st-3
st-4 com.mysql.jdbc.Blob@664f41
st-6
st-7 [B@1bb8fc7
st-8 org.apache.catalina.connector.CoyoteOutputStream@cea65d
st-9 

and after i google this exception what i did am unable to get a single image but this time it not threw any exception here is the code

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="Displayimage">click to display images</a>
</body>
</html>

here is my Displayimage servlet

package com.mulimgdis;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Displayimage
*/
public class Displayimage extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
 public Displayimage() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request,  HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub


    PrintWriter out = response.getWriter();

    ServletOutputStream o; 
    ArrayList<Blob> bl;
    try {




         Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con  =DriverManager.getConnection("jdbc:mysql://localhost:3306/imgdb","root","annie1993");
    PreparedStatement stmt = con.prepareStatement("select IMAGE from myimages ");
    ResultSet rs = stmt.executeQuery();
    //ResultSet rs1=stmt.executeQuery("select count(id) from student_profile");
     bl=new ArrayList<Blob>();
    while(rs.next()) {
    System.out.println("st-1");
    bl.add(rs.getBlob(1));
    System.out.println("st-2");
    }
    //if(rs1.next())
    //System.out.println(bl.size()+"size="+rs1.getInt(1));
    for(int i=0;i<bl.size();i++){
    System.out.println("st-3");
    Blob b=bl.get(i);
    System.out.println("st-4 "+b);
    byte brr[]=new byte[(int)b.length()];
    System.out.println("st-6");
    brr=b.getBytes(1,(int)b.length());
    System.out.println("st-7 "+brr);
    // display the image
    response.reset();
    response.setContentType("image/jpg");
     o = response.getOutputStream();
     System.out.println("st-8 "+o);

     /* 
      * 
      *   %><table><tr><td align='left'>
 <img src=<%o.write(brr); %> width="117" height="160"
 >
 </td></tr></table>
 <%
 out.println("<br>");
 System.out.println("st-9 ");
 }
 } catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} 
%>
</div></td></tr></table></td></tr>
</table>
*/

     o.write(brr);
     response.getOutputStream().flush();  

     out.println("<br>");
     System.out.println("st-9 ");
     }
    } catch (Exception e) {
    out.println("Unable To Display image");
    out.println("Image Display Error=" + e.getMessage());
    return;
    }  


}
Community
  • 1
  • 1

1 Answers1

0

Look this documentation: httpServletResponse.getOutputStream()

You see that

Either this method or getWriter() may be called to write the body, not both.

It should be the problem

c3R1cGFy
  • 505
  • 4
  • 12
  • ok i got it but after removing all the getWriter() from my Displayimage servlet class i have not getting that exception but eventually i only get the single image not all the images from the database. after omited this getWriter() pls look to my servlet class and tell me that what should i write into this to get all the images and how – Akash Srivastava Aug 20 '15 at 14:52
  • Try to change your sql query: select IMAGE from myimages ORDER BY image_name. So look which image is displayed, if this is last one in the query or first one it might be that browser can one receive and show single image at the time. – c3R1cGFy Aug 20 '15 at 17:40