Hello Experts pls i want get all the form data for id card along with the passport image when correct id corresponding to the database row is inputed. the code only retrieves other form data but can't get the image.
--------------recovery.jsp---------

<HTML>
    <HEAD>
        <TITLE>Database Lookup</TITLE>
    </HEAD>
 
    <BODY>
    
        <FORM ACTION="recover1.jsp" METHOD="POST"><fieldset><legend>
        <font size="5" color="green">
            Please Enter id to Search </font></legend>
            <BR>
            <INPUT TYPE="TEXT" NAME="id" size="40">
            <BR>
            <INPUT TYPE="RESET" value="clear all" size="40"><INPUT TYPE="SUBMIT" value="Search Account"></fieldset>
</center>

    </BODY>        
<HTML>
<%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>

<HTML>
    <HEAD>
        <TITLE>Fetching Data From a Database</TITLE>
    </HEAD>

    <BODY>

    

        <% 
            Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/student", "root", "root");

            Statement statement = connection.createStatement();

            String id = request.getParameter("id");  

            ResultSet resultset = 
                statement.executeQuery("select * from job where id = '" + id + "'"); 

            if(!resultset.next()) {
                out.println("<font size=3 color=red>Sorry,we can't find your informations.</font>");
            } else {
           
        %>

        <TABLE BORDER="1" bordercolor=green>
            <TR>
               <TH>
        <font size="3" color="green">ID:</font></TH>
               <TH>
        <font size="3" color="green">USERNAME:</font></TH>
                <TH>
        <font size="3" color="green">PICTURE:</font></TH>
                          </TR>
           <TR>
              <TD> <%= resultset.getString(1) %> </TD> 
               <TD> <%= resultset.getString(2) %> </TD>  
               <TD> <%= resultset.getBlob(3) %> </TD> 
           </TR>
       </TABLE>
       <BR>
       <% 
           } 
       %>

<BODY>


    </BODY>
</HTML>

I said ok let me develop servlet or another jsp to retrive the image along with other id card data so i did this

<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*"%>
<%
 
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con=null;
 
ResultSet rs=null;
Statement stmt=null;
String id = request.getParameter("id");  

 
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root");
stmt=con.createStatement();
}
catch(Exception e)
{
out.println(e.getMessage());
}
 
rs=stmt.executeQuery("select * from new ");
rs.next();
 
response.setHeader("expires", "0");
response.setContentType("image/jpeg");
 
out.clear();
OutputStream os = response.getOutputStream();
os.write(rs.getBytes("fred3"));
out.flush();
 
%>

and for the display i used

<a href="shango3.jsp?id=1<<%=resultset.getInt(1)%>">
               <img src="shango3.jsp?id=1<<%=resultset.getInt(1)%>" width="100" height="100">

but i successed in getting the passport image and the id card data of the first row when id 1 is inputed but when i entered id 2 i can't get information at all unless i go to my code and set id=2. my servelet is still doing the same thing.
this code has stressed me for 2 months now.
Now my plea is this,is there any way you can help me even if there is any site where i can see example on how to get passport image along with other form data when correct id is inputed may be using jsp or even jsp/servlet.
thanks for your patience.

Dani AI

Generated

A concise diagnosis and a practical fix that ties back to comments by and .

The symptom (image always comes from row 1; only id=1 works) normally means the image endpoint is not using the id parameter when querying the database, or the page that serves binary data is polluted by JSP template output. Servlets are the right tool here: have one servlet stream the BLOB for a given id, and have the HTML/JSP page fetch other fields and reference the image with an <img> tag that points to the servlet. This follows the Model 2 idea mentioned by and the servlet advice from .

Example servlet pattern (adapt to connection management and column names):

@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String id = req.getParameter("id");
    if (id == null) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST); return; }

    try (Connection conn = /* get connection */;
         PreparedStatement ps = conn.prepareStatement(
             "SELECT photo_blob, mime_type FROM persons WHERE id = ?")) {
      ps.setInt(1, Integer.parseInt(id));
      try (ResultSet rs = ps.executeQuery()) {
        if (!rs.next()) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; }
        InputStream in = rs.getBinaryStream("photo_blob");
        String mime = rs.getString("mime_type");
        resp.setContentType(mime == null ? "image/jpeg" : mime);
        try (OutputStream out = resp.getOutputStream()) {
          byte[] buf = new byte[8192];
          int len;
          while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
        }
      }
    } catch (SQLException e) {
      throw new ServletException(e);
    }
  }
}

HTML/JSP snippet to show the image (avoid building a literal "id=1" in the href):

<img src="ImageServlet?id=${row.id}" width="100" height="100" alt="photo">

Short checklist and cautions

  • Ensure the SELECT uses a WHERE id = ? so rs.next() does not always return the first row.
  • Do not stream binary from a JSP that contains template text or whitespace; use a servlet and resp.getOutputStream().
  • Use PreparedStatement to avoid SQL injection and type mistakes.
  • Prefer streaming via InputStream rather than rs.getBytes(...) for large blobs.
  • Test the image servlet directly (e.g., /ImageServlet?id=2) and inspect response headers with browser devtools.

Following these steps normally resolves the behavior described by and keeps the display page separate from the binary-streaming servlet.

Recommended Answers

All 7 Replies

within a few pages, this code will become almost impossible to read/maintain.
you could familiarize yourself with the use of servlets, and use those to perform all the tasks you want to be done.

when reading further on the use of requests en responses, you'll figure out how to pass the data from one 'page' to another.

thank u for your response .I have tried it using servlet but am still having the same problem.the point is these ,is there any site you know where i can get an articles or tutorials that can help me achieve this goal.
thanks

Check this

Thank you i have already known these but it has no link with what am trying to do b/c no blob data retrival along with other database records where there thus may be i will send you another servlet i develope with some description so that u can assist me.
I am glad for ur help so far.

write good English first. Once you've managed that use that knowledge to read some good books and tutorials.
After that, you might be able to write decent code after some practice.

Thank you i have already known these but it has no link with what am trying to do b/c no blob data retrival along with other database records where there thus may be i will send you another servlet i develope with some description so that u can assist me.
I am glad for ur help so far.

The above tutorial is exactly what you need because you are going about database connectivity in wrong way. Adding blob data type to it is simple matter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.