0
//download_image.jsp file
<%@page import="java.io.InputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.sql.ResultSet"%>
<%
             //   String username = session.getAttribute("userid").toString();

           try {

                OutputStream oImage;
                DB.Connect.openConnection();
                String query = "select image from tbluser where userid = '1'";
                ResultSet rs = DB.Connect.stat.executeQuery(query);
                if (rs.next()) {
                    byte barray[] = rs.getBytes(1);
                    response.reset();                   
                    response.setContentType("image/jpeg");
                    oImage = response.getOutputStream();
                    oImage.write(barray);
                    oImage.flush();
                    oImage.close();
                }
               DB.Connect.CloseConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
%

Blockquote

This is my code to display the image from mysql db. I am getting error when I call this from another jsp file from this tag

<img border="3" src="download_image.jsp" height="145" width="145" />

as :

WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:707)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:224)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180)
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:237)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:181)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:132)
        at org.apache.jsp.download_005fimage_jsp._jspService(download_005fimage_jsp.java from :99)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)

What should I do please suggest me a method.


I have written my code as below in displayImage servlet & I have mapped in web.xml also but still I am getting blank on image :(

public class displayImage extends  HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //PrintWriter pw = response.getWriter();
        String connectionURL = "jdbc:mysql://localhost/medico";
        java.sql.Connection con=null;
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection(connectionURL,"root","");
            Statement st1=con.createStatement();
            ResultSet rs1 = st1.executeQuery("select image from tbluser where userid='1'");
            String imgLen="";
            if(rs1.next()){
                imgLen = rs1.getString(1);

            }
            rs1 = st1.executeQuery("select image from tbluser where userid='1'");
            if(rs1.next()){
                int len = imgLen.length();
                byte [] rb = new byte[len];
                InputStream readImg = rs1.getBinaryStream(1);
                int index=readImg.read(rb, 0, len);             
                st1.close();
                response.reset();
                response.setContentType("image/jpg");               response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

}

I am calling it in html file as . so where is the error :( here

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Durgesh Kumar
  • 935
  • 10
  • 17
  • duplicate of http://stackoverflow.com/questions/4332907/how-to-retrieve-images-from-database-and-place-on-jsp – Sanjeev Mar 11 '14 at 10:31
  • I have already used that fourm but I am still getting this error i.e. getOutputStream() has been already defined :( – Durgesh Kumar Mar 11 '14 at 10:36
  • I don't think you have used the answer of that question. It suggests to use a servlet, same thing I am suggesting in my answer below. It provides a link to a complete example of the `DisplayImage` servlet too! – jbx Mar 11 '14 at 10:41
  • I have done as it is shown in the tut but still its not working? please refer to my answer below. :( – Durgesh Kumar Mar 11 '14 at 11:13

2 Answers2

2

I wouldn't display an image from a JSP. A JSP is designed to serve an HTML page, so what it will do is that it will start outputing the HTTP headers saying that it is an HTML page to the output stream.

Then in the middle of it you are saying 'oh no, I want to send an image', but by that time its already too late, which is why you are getting that error getOutputStream() has already been called for this response.

There is no benefit of using a JSP to output an image, the point of JSPs is to make it easy to output HTML pages.

Use a normal servlet instead. Override the doGet() method, and just put the code that outputs the image in it. Don't forget to add the servlet mapping to the web.xml so that you give it a path.

So if your servlet mapping will map to path /downloadImg, you just put <img border="3" src="downloadImg" height="145" width="145" /> instead in the other JSP.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • I have done as it is shown in the tut but still its not working? please refer to my answer below. :( – Durgesh Kumar Mar 11 '14 at 11:14
  • one more thing I have saved the image in db as MEDIUMBLOB data type. – Durgesh Kumar Mar 11 '14 at 11:43
  • If you don't give us the exception you are getting in the logs its difficult to know what you are seeing. A blank image could be caused by a hundred causes. Also don't post answers! Update your question with the additional information, you're creating a whole confusion. – jbx Mar 11 '14 at 18:00
1

they error is here:

String connectionURL = "jdbc:mysql://localhost/medico";

you should must mention the port no: you should change in your code as

String connectionURL = "jdbc:mysql://localhost:3306/medico";
jmail
  • 5,944
  • 3
  • 21
  • 35