java - image not retrieving when submitting form using servlet -
i have image in sql , want retrieve in web page have developed servlet. when hit submit button calls servlet image not displayed in browser if if writing localhost:8080/y/testimageservlet image displayed.
imagetestservlet.java
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 } }
login
<form id="f1" name="f1" action="imagetest" method="post" 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>
your form sends post
request,
<form id="f1" name="f1" action="imagetest" method="post" onsubmit="return ccheck()">
but define doget method in servlet. resulting in page working if navigate in browser (get
) not when submitting form (post
).
you either need change form use get
or implement code in dopost
method
Comments
Post a Comment