0

I am unable to display my image from the database onto my jsp page and I still cannot get where the problem is please help.

My servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        DBConnection db = new DBConnection();
        Connection conn;
         try {
             conn =  db.getDbConnection();
            PreparedStatement ps = conn.prepareStatement("select stu_image from student_images_table where STU_REG_NO = ?");
            String id = request.getParameter("studentregno");
            ps.setString(1,id);
            ResultSet rs = ps.executeQuery();
            rs.next();
            Blob  b = rs.getBlob("stu_image");            
            response.setContentType("image/jpeg");
            response.setContentLength( (int) b.length());
           // response.setContentLength(10);
            InputStream is = b.getBinaryStream();
            OutputStream os = response.getOutputStream();
            byte buf[] = new byte[(int) b.length()];
            is.read(buf);
            os.write(buf);
            os.close();
        }
        catch(Exception ex) {
             System.out.println(ex.getMessage());
        }
    }

My JSP:

<div id="bv_Image1" style="margin:0;padding:0;position:absolute;left:572px;top:31px;width:194px;height:162px;text-align:left;z-index:1;">
                        <img src="ProfileInquiryServlet" id="Image1" alt="" align="top" border="0" style="width:194px;height:162px;"></div>

My Display:

My Display has no Image

What could I be doing Wrong?

EDIT: My Web.xml is huge but maybe this might be relevant;

<servlet>
        <servlet-name>ProfileInquiryServlet</servlet-name>
        <servlet-class>com.kollega.controller.ProfileInquiryServlet</servlet-class>
    </servlet>
     <servlet-mapping>
        <servlet-name>ProfileInquiryServlet</servlet-name>
        <url-pattern>/ProfileInquiryServlet</url-pattern>
    </servlet-mapping>
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168

1 Answers1

0

Use one servlet for the image, and pass it the studentregno:

<img src="ProfileInquiryServlet?studentregno=${param.studentregno}" ...

This is a separate request, and hence need to have that parameter.


The first servlet can do request.setAttribute("regno", "..."); and then in the JSP you can use ${regno}. The request.getParameter("studentregno") is available as ${param.studentregno}.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138