0

I am able to find all the data through this code

while(it.hasNext())
        {
        Object objnew=it.next();
        PartnerRegistrationIndividual PartRegIndv =(PartnerRegistrationIndividual) objnew;

        pid=PartRegIndv.getId();
        firstname=PartRegIndv.getFname();
        lastname=PartRegIndv.getLname();
        email=PartRegIndv.getEmail();
        mobile=PartRegIndv.getMobile();
        foe=PartRegIndv.getSpeciality();
        expSalPerDay =PartRegIndv.getExpectedSalaryPerDay();
        expSalPerMonth=PartRegIndv.getExpectedSalaryPerMonth();
        current_status=PartRegIndv.getApproval_status();

I am using following code to get the data from database...but my webpage goes blank and i get some exception in console..

        Blob imgdata=PartRegIndv.getImage();
        imgdata.getBinaryStream();
        OutputStream output = response.getOutputStream();
        response.setContentType("image/jpeg");
        response.getOutputStream().flush();
        response.getOutputStream().close();

Exception which comes in my console...

SEVERE: Servlet.service() for servlet emen threw exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:604) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125) at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118) at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:326) at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342) at org.apache.jsp.allpartners_jsp._jspService(allpartners_jsp.java:318) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)

2 Answers2

1

The response.setContentType() shouldn't be called after response has already started to be written back to the caller.

Try to invoke setContentType before you invoke getOutputStream.

If that doesn't help, could you check in your code where response or response.getOuputStream might be getting called? That way you will know what piece of code started writing back to the browser.

UPDATE

Once you start writing to the response. You are now allowed to render a JSP. If this was a servlet code, you could just "return" without having to forward to a JSP.

Mecon
  • 977
  • 1
  • 6
  • 17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78338/discussion-between-abhishek-shandilya-and-mecon). – Abhishek Shandilya May 20 '15 at 15:01
  • 1
    Images can't be returned in that manner. In the JSP page, create an HTML IMG element, and make the "src" attribute of that element invoke a URL which will fetch the Image from the server. So, you will need a special Controller method just to retrieve Image. – Mecon May 20 '15 at 15:59
  • ........................I have this one how to put src of that image fetched from database – Abhishek Shandilya May 20 '15 at 16:02
  • 1
    JSP can have image tag like this: – Mecon May 20 '15 at 16:02
  • One simple question is that....how can i show an image on web page. which is "Blob" type. – Abhishek Shandilya May 20 '15 at 16:03
  • 1
    You have to implement a method that accepts URL like I have in my example. And in that method, you can execute the same code you had that wrote image data into `response.getOutputStream()` – Mecon May 20 '15 at 16:06
  • ok...got that..can write that method on same page (jsp page). – Abhishek Shandilya May 20 '15 at 16:08
  • In your URL...can you tell me..what do the myapplication partreg and getImage represent? – Abhishek Shandilya May 20 '15 at 16:10
  • they are dummy pace holders. You would need put a URL that makes sense for your application. – Mecon May 20 '15 at 16:22
  • when i put the "output.write(bdata);" it shows the single image on the same page but also throws the Exception "getOutputStream() has already been called for this response" – Abhishek Shandilya May 21 '15 at 08:14
  • are you still forwarding the request to a JSP? When you write such binary data to response.outputStream, then you really Can't (and Should not need to) forward to a JSP. JSP is meant for rendering data in HTML page. But what you what is to output binary data to the Browser. – Mecon May 21 '15 at 13:57
  • can you send me some link where i could understand these things or i could able to show images on jsp page. – Abhishek Shandilya May 21 '15 at 18:59
  • http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page/2341322#2341322 – Mecon May 21 '15 at 19:02
  • any luck with the example? – Mecon May 22 '15 at 16:51
  • i would like to show you ...whole code once which i tried?? may be you can help me there – Abhishek Shandilya May 22 '15 at 19:10
0
OutputStream output = response.getOutputStream();  

response.setContentType("image/jpeg");

As you can see, you are fetching response first and setting it's type later, which might be causing the issue.

Try correcting this & if things are still nasty, post stacktrace also.

Raúl
  • 1,542
  • 4
  • 24
  • 37