Abhishek Shandilya
Abhishek Shandilya

Reputation: 105

How to display image on Jsp page from database using Hibernate and spring MVC

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)

Upvotes: 0

Views: 1122

Answers (2)

Mecon
Mecon

Reputation: 997

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.

Upvotes: 1

Raúl
Raúl

Reputation: 1552

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.

Upvotes: 0

Related Questions