Rani_7 0 Newbie Poster

Hello everyone, I am having trouble in displaying the multiple images on jsp. I am using servlet to pull the image from the database. Following code, will always display single image multiple times on my jsp page. Can anyone please help me in rectifying the error in the code. I am new to jsp and servlet programming! Thanks for your anticipation. Fyi,
Inline Code Example Here

Table A:
    fieldname: personid(datatype: int, primary key)
    fieldname: person_name(datatype: varchar(20)
    Table B:
    fieldname: person_pic(datatype: BLOB)
    fieldname: personid(foreign key references Table A(id))
    fieldname: picid(datatype: int, primary key)
    Below is the jsp code:

     <%
        //uid will have the session value
        rs = st.executeQuery("select picid from Table B where personid = '" + uid + "' ");   
        int pid = 0;
        while(rs.next())
        {
        // Fetching person id from Table B a
        pid = rs.getInt(picid);
        %> <tr> <td><%= rs.getInt(picid);</td> <td><img src="http://localhost:8080/Displayimg?id=pid " height="" width="" /></td> </tr> <% 
        }
        %>
        Servlet code:
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        {
        int p_id= request.getParameter("pid");
         PreparedStatement ps = con.prepareStatement("Select Pro_pic from Table B where  picid =  '" + p_id + "' ");            
         ResultSet rs = ps.executeQuery();
         if(rs.next)
        {
                Blob  b = rs.getBlob("Pro_pic");            
                response.setContentType("image/jpg");
                response.setContentLength( (int) b.length());       
                InputStream is = b.getBinaryStream();
                OutputStream os = response.getOutputStream();
                byte buf[] = new byte[(int) b.length()];
                is.read(buf);
                os.write(buf);
                os.close();        
                }     
                }

Dani AI

Generated

Quick diagnosis for : the page shows the same image because the IMG tag is sending a literal token instead of each row's pic id, and the servlet is not reading that parameter correctly. There are also small mistakes in the resultset/servlet code (missing () on rs.next(), assigning request.getParameter(...) to an int, and ambiguous column/parameter names). Fix the parameter interpolation and make the servlet parse the incoming pid; that will make each IMG request fetch the correct blob.

Correct the JSP output so the actual pic id is put into the query string (example pattern shown here):

<% while (rs.next()) {
    int pid = rs.getInt("picid");
%>
<tr>
  <td><%= pid %></td>
  <td><img src="Displayimg?pid=<%= pid %>" alt="person pic" /></td>
</tr>
<% } %>

In the servlet, read and validate the pid parameter, use a prepared statement, and stream the blob without loading it all at once. Example pattern:

String pidParam = request.getParameter("pid");
if (pidParam == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; }
int pId = Integer.parseInt(pidParam);
PreparedStatement ps = con.prepareStatement("SELECT Pro_pic FROM TableB WHERE picid = ?");
ps.setInt(1, pId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
  Blob b = rs.getBlob("Pro_pic");
  response.setContentType("image/jpeg");
  try (InputStream is = b.getBinaryStream();
       OutputStream os = response.getOutputStream()) {
    byte[] buf = new byte[4096]; int n;
    while ((n = is.read(buf)) != -1) os.write(buf, 0, n);
  }
} else {
  response.sendError(HttpServletResponse.SC_NOT_FOUND);
}

Quick checklist while testing: view page source to confirm each IMG has a different numeric pid, call the image servlet directly in the browser (e.g., ?pid=3) to verify output, avoid building SQL in JSP (move DB logic to servlet/DAO), and use PreparedStatement to prevent SQL injection.

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.