-1

I am retrieving image bytes from mysql database through the following Servlet method:

and then display the image bytes in a jsp page.

Please tell me how do I use the img tag in my jsp page.

    public byte[] getProfilePicture( int id ) {

    byte[] bytes = null;

    try {

        connection = dataSource.getConnection();
        pst = connection.prepareStatement("SELECT profile_picture FROM users WHERE id = '"+id+"' ");
        resultSet = pst.executeQuery();

        while( resultSet.next() ) {

            bytes = resultSet.getBytes("profile_picture");

        }

    } catch(Exception e) {
        e.printStackTrace();
    }

    return bytes;

}

Thanks, I am waiting for your kind response.

Sikandar Sahab
  • 638
  • 3
  • 10
  • 27

1 Answers1

0

Simply make it a resource within your web App that can be accessed using JSP/Servlet.

Example 1 Fixed Loaction:

Servlet Location: http://www.cvss.online/captcha

JSP Code:

<div class="six columns">
     <label>Captcha image</label>
     <img src="captcha"> 
</div>

Example 2 Dynamic Location by ID:

Each image has a unique ID

    ...
    int imgId = Integer.parseInt(request.getParameter("imgId"));
    // Your Logic
    ...

Your Example:

Within your resultSet.next() block add:

byte imageArray[] = rs.getBytes(1);
response.setContentType("image/type"); //type png jif etc
outputStream=response.getOutputStream();
outputStream.write(imageArray);
outputStream.flush();
outputStream.close();

Cheers

alkathirikhalid
  • 887
  • 12
  • 18