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)

3
  • 1
    please provide Exception which comes in my console... Commented May 20, 2015 at 14:36
  • java.lang.IllegalStateException: getOutputStream() has already been called for this response Commented May 20, 2015 at 14:37
  • @TAsk Please help me out Commented May 20, 2015 at 14:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

15 Comments

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.
<img src="?" class="img-circle" width="150" >........................I have this one how to put src of that image fetched from database
JSP can have image tag like this: <img src="/myapplication/partreg/getImage?id=<%= var.getId() %>" height="200" width="200">
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()
|
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.

1 Comment

Right, looks like I haven't refreshed page after opening question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.