java - how to display an image from mysql database on a jsp page? -
this question has answer here:
i want display image stored in mysql database. retrieve image, using servlet. please tell me how display in jsp page. flow:
login.html ---> dologin.jsp
login.html , servlet ready. please tell me how finish dologin.jsp.
login.html
<html> <head> <style type="text/css"> html, body, div, h1, h2, h3, h4, h5, h6, p, img, dl, dt, dd, ol, ul, li, table, tr, td, form, object, embed, article, aside, command, details, fieldset, figcaption, figure, footer, group, header, hgroup, legend { margin: 0; padding: 0; border: 0; } html { font: 82.5% verdana, helvetica, sans-serif; background: #fff; color: #333; line-height: 1; direction: ltr; } html, body { position: absolute; height: 100%; min-width: 100%; } table { border-collapse: collapse; border-spacing: 0; } .signin-header { padding: 10px 12px 5px; background:#00aaff; border: 1px solid #e5e5e5; width: 362px; float: right; } .signin-box { padding: 20px 25px 15px; background:#dedede; border: 1px solid #e5e5e5; width: 335px; float: right; } .button { min-width: 46px; text-align: center; color: #444; font-size: 11px; font-weight: bold; height: 27px; padding: 0 8px; line-height: 27px; border-radius: 2px; transition: 0.218s; border: 1px solid #dcdcdc; background-color: #f5f5f5; cursor: default; } *+html .button { min-width: 70px; } button.button, input[type=submit].button { height: f1f1f1px; line-height: 29px; vertical-align: bottom; margin: 0; } .button:hover { border: 1px solid #c6c6c6; color: #333; text-decoration: none; transition: 0.0s; background-color: #f8f8f8; box-shadow: 0 1px 1px rgba(0,0,0,0.1); } .button:active { background-color: #f6f6f6; box-shadow: inset 0 1px 2px rgba(0,0,0,0.1); } .button:visited { color: #666; } .button-submit { border: 1px solid #3079ed; color: #fff; text-shadow: 0 1px rgba(0,0,0,0.1); background-color: #4d90fe; } .button-submit:hover { border: 1px solid #2f5bb7; color: #fff; text-shadow: 0 1px rgba(0,0,0,0.3); background-color: #357ae8; } button-submit:active { background-color: #357ae8; box-shadow: inset 0 1px 2px rgba(0,0,0,0.3); } .footer-bar { position: absolute; bottom: 0; height: 35px; width: 100%; border-top: 1px solid #ebebeb; overflow: hidden; } .footer { padding-top: 9px; font-size: .85em; white-space: nowrap; line-height: 0; } .footer ul { color: #999; float: left; max-width: 80%; } .footer ul li { display: inline; padding: 0 1.5em 0 0; } .footer { color: #333; } .footer .lang-chooser-wrap { float: right; max-width: 20%; } .footer .lang-chooser-wrap img { vertical-align: middle; } .footer .attribution { float: right; } .footer .attribution span { vertical-align: text-top; } .content { padding: 0 44px; } .table { padding: 0 55px } </style> <script type="text/javascript"> function ccheck() { uid=document.f1.uid.value; cpass=document.f1.cpass.value; if(uid=="" || uid==null) { alert("plz. enter user id"); document.f1.uid.focus(); return false; } if(cpass=="" || cpass==null) { alert("plz. enter password"); document.f1.cpass.focus(); return false; } return true; } </script> <title>login page</title> </head> <body onload="document.f1.uid.focus()" style="background-image:url('medical.jpg');background-position: center"> <form id="f1" name="f1" action="dodelete.jsp" method="get" onsubmit="return ccheck()"> <img src="header.png"><br><br><br><br><br> <table><tr><td> <table class="table"><tr><td> <table class="table"><tr><td> <div class="content"><div class="signin-header"><h3>welcome diaemr</h3></div></div> </td></tr> <tr><td> <div class="content"> <div class="signin-box"> <p class="one"> welcome @ <b>"workshop on ileal interposition".</b><br> <b>brazil</b> inaugurate & launch important data registry<br> key features of solution-<br> </div> </div> </td></tr></table> </td> <td>image</td> <td> <table class="table"><tr><td> <div class="content"> <div class="signin-header"> <h3>portal login</h3> </div> </div> </td></tr> <tr><td> <div class="content"> <div align="center" class="signin-box"> <table class="table"><tr><td> <b>user id</b></td> <td><input name="uid" type="text" /></td> </tr> <tr><td> <b>password</b></td> <td><input name="cpass" type="password" /></td> </tr> <tr> <td><input type="submit" class="button button-submit" value="submit" /></td> <td><input type="reset" class="button button-submit" value="reset" /></td> <tr>new user<a href ="userregistration.jsp">register</a></tr><br> </tr></table> </div> </div> </td></tr></table> </td></tr></table> </td></tr></table> <div class="footer-bar"> <img align="left" src="footer.png"> </div> </form> </body> </html>
servlet
import java.sql.*; import db.databaseconnection; import java.io.ioexception; import java.io.inputstream; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class imagetestservlet */ @webservlet("/imagetestservlet") public class imagetestservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public imagetestservlet() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ public void doget(httpservletrequest request, httpservletresponse response) throws ioexception,servletexception { blob image = null; connection con = null; statement stmt = null; resultset rs = null; databaseconnection db= new databaseconnection(); servletoutputstream out = response.getoutputstream(); try { class.forname("com.mysql.jdbc.driver"); con=db.connet(); stmt = con.createstatement(); rs = stmt.executequery("select img 1 id = '4'"); if (rs.next()) { image = rs.getblob(1); } else { response.setcontenttype("text/html"); out.println("<font color='red'>image not found given id</font>"); return; } response.setcontenttype("image/gif"); inputstream in = image.getbinarystream(); int length = (int) image.length(); int buffersize = 1024; byte[] buffer = new byte[buffersize]; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); out.flush(); } catch (exception e) { response.setcontenttype("text/html"); out.println("<html><head><title>unable display image</title></head>"); out.println("<body><h4><font color='red'>image display error=" + e.getmessage() + "</font></h4></body></html>"); return; } } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub } }
why dont try this:
i have getimagedetails.jsp retrive image data database based on user_id or name or other constraint follows (put code in jsp/servlet page)
int img_id = integer.parseint(request.getparameter("your_id")); connection con = //get connection object here ; resultset rs = null; preparedstatement pstmt = null; outputstream oimage; try { pstmt = con.preparestatement("your sql statement retriving image column data"); pstmt.setint(1, img_id); rs = pstmt.executequery(); if(rs.next()) { byte barray[] = rs.getbytes(1); response.setcontenttype("image/gif"); oimage=response.getoutputstream(); oimage.write(barray); oimage.flush(); oimage.close(); } } catch(exception ex){ //ex.printstacktrace(); }finally { try{ if(con!=null) con.close(); }catch(exception ex){ // ex.printstacktrace(); } }
then calling page html or jsp page follows
< img src="getimagedetails.jsp?your_id=12" width="50" height="50" />
please try answer
let me know status happy coding
Comments
Post a Comment