Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:46:17+00:00 2026-05-23T21:46:17+00:00

Possible Duplicate: How to retrieve and display images from a database in a JSP

  • 0

Possible Duplicate:
How to retrieve and display images from a database in a JSP page?

I want to display an image in a <td> tag of a table. The code is working fine but I am not able to retrieve the image in the <td> tag of the table

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:IMG");
        Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs=st.executeQuery("select image from img");

        %>
        <table width="100%" border="2">
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <%
            while(rs.next()) {
            byte[] bytearray = new byte[1048576];
            int size=0;
            InputStream sImage = rs.getBinaryStream(1);

            response.reset();
            response.setContentType("image/jpeg");
            while((size=sImage.read(bytearray))!= -1 ){
            %>
            <tr>
                <td>&nbsp;</td>
                <td><img src="<%= response.getOutputStream().write(bytearray,0,size)%>"width=50 height=50 /></td>
                <td>&nbsp;</td>
            </tr>
            <%
            }
            }     
            %>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:46:18+00:00Added an answer on May 23, 2026 at 9:46 pm

    Setting a different content type inside an HTML (jsp) page cannot work. Inside your JSP you must generate a link to an image. The browser will request the image in a separate request, for this you need something like a servlet:

    1. step: generate the JSP

        <%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:IMG");
        Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    
        /* you need an identifier for each image, like an ID */
        ResultSet rs=st.executeQuery("select name, image from img");
    
        %>
        <table width="100%" border="2">
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <%
            while(rs.next()) {
            %>
            <tr>
                <td>&nbsp;</td>
                <td><img src="${path_to_your_image_serving_resource}/<%=rs.getString("name")%>"width=50 height=50 /></td>
                <td>&nbsp;</td>
            </tr>
            <%
            }     
            %>
        </table>
    

    2. step: create a servlet

    Your servlet must listen to ${path_to_your_image_serving_resource}/*. If the browser requests this URL you have to retrieve the image based on its name, set the coorect content type for the response, and stream the bytes back to the browser.

    Remember that in HTML/HTTP an image is an independent resource.

    BTW: You should carefully look at your design:

    • You do not use a connection pool, but instead the DriverManager and create a new connection to the database per request. This will neither perform well nor is it scalable. Look at javax.sql.DataSource
    • defining your SQL directly inside the JSP is (in general) a bad practice
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to retrieve all case-insensitive duplicate entries from an array. Is this possible
Possible Duplicate: how to display data obtained from dao in jsp hi i able
Possible Duplicate: How to retrieve the values for the particular key from CFMutableDictionary In
Possible Duplicate: How can I retrieve recently used contacts in android? I want to
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: File creation time In PHP, how to retrieve the files contained into
Possible Duplicate: How do I get the current logged in Active Directory username from
Possible Duplicate: How to check if internet connection is present in java? I want
Possible Duplicate: Retrieving Android API version programmatically I am trying to retrieve the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.