java - Unable to show images in JSP from database using servlet -


i trying fetch , display , images saved blob on jsp mysql database using servlet. wrote code referring many websites , still , not working. not getting type of errors. showing this

enter image description here

i have created table using

create table contacts(id int not null auto_increment, name varchar(40), second varchar(40), photo blob, primary key(id)); 

this servlet displayservlet.java

@webservlet("/displayservlet") public class displayservlet extends httpservlet {     private static final long serialversionuid = 1l;     connection conn = null;     preparedstatement stmt = null;     resultset rs = null;     public void init() throws servletexception {      }  public displayservlet() {     super(); }   protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     dopost(request, response); }  /**  * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)  */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {      string imageid = request.getparameter("id");     system.out.println(imageid);     inputstream simage;        // check if id supplied request.     if (imageid == null) {         // thing if id not supplied request.         // throw exception, or send 404, or show default/warning image, or ignore it.         response.senderror(httpservletresponse.sc_not_found); // 404.         return;     }      try{         class.forname("com.mysql.jdbc.driver");         conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/projectbuy", "root","root");         stmt = conn.preparestatement("select photo contacts id=" + imageid);         rs = stmt.executequery();         if(rs.next()){             system.out.println("inside rs");             byte[] bytearray = new byte[1048576];             int size=0;             simage = rs.getbinarystream(4);             response.reset();             response.setcontenttype("image/jpeg");             while((size = simage.read(bytearray)) != -1 ){                 response.getoutputstream().                 write(bytearray,0,size);             }         }      } catch (exception e){         e.printstacktrace();     } } 

}

here jsp page , imagedemo.jsp

<%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html>     <head>         <meta http-equiv="content-type" content="text/html; charset=utf-8">         <title>jsp page</title>     </head>     <body>          <h1>hello world!</h1>           <img src="displayservlet?id=1" height="150px" width="150px" alt="profilepic">      </body> </html> 

finally xml file

<servlet-mapping>         <servlet-name>action</servlet-name>         <url-pattern>*.do</url-pattern>     </servlet-mapping>  <servlet>     <servlet-name>displayservlet</servlet-name>     <servlet-class>displayservlet</servlet-class> </servlet>  <servlet-mapping>     <servlet-name>displayservlet</servlet-name>     <url-pattern>/displayservlet/*</url-pattern> </servlet-mapping> 

you're selecting 1 column:

stmt = conn.preparestatement("select photo contacts id=" + imageid); 

but later trying retrieve 4th:

simage = rs.getbinarystream(4); 

and have no flush() call afer writing loop. it's idea use connection pool instead of instantiating directly drivermanager, , close after work.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -