java - I want to retrieve image from database and display it in JSP using <img> tag. I am using my sql -
i want retrieve image database , display in jsp using img
tag. using mysql.
public class displayimage extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //printwriter pw = response.getwriter(); string connectionurl = "jdbc:mysql://localhost:3306/demodb";; java.sql.connection con=null; try{ class.forname("com.mysql.jdbc.driver").newinstance(); con=drivermanager.getconnection(connectionurl,"root","0321"); statement st1=con.createstatement(); resultset rs1 = st1.executequery("select photo contacts contact_id='2'"); string imglen=""; if(rs1.next()){ imglen = rs1.getstring(1); system.out.println(imglen.length()); } rs1 = st1.executequery("select photo contacts contact_id='2'"); if(rs1.next()){ int len = imglen.length(); byte [] rb = new byte[len]; inputstream readimg = rs1.getbinarystream(1); int index=readimg.read(rb, 0, len); system.out.println("index"+index); st1.close(); response.reset(); response.setcontenttype("image/jpg"); response.getoutputstream().write(rb,0,len); response.getoutputstream().flush(); } } catch (exception e){ e.printstacktrace(); } } }
this code display image directly. want path. can display wherever want. there other ways display image in dynamic web page?
the path depends on how servlet mapped in web.xml (as don't see annotations in code). other - rather discouraged - way use data uri. see i want retrieve image mysql database using servlet , show image along user details in table
Comments
Post a Comment