Question

I have a webapplication for displaying images using imageio on a servlet. The webapp works fine when hosted in tomcat 7 using jre 1.6. But the same webapp when deployed on tomcat 5.5 with servlet 2.4 and Jre 1.5 it doesnt work. To access the image i pass an identifier as parameter and i get the image which is a blob column in DB. Initially i developed the application on tomcat 7 instance using eclipse and it worked fine. Compiler version compatibility is selected as 1.5 and Dynamic web module version is 2.4. In the below code i have checked the jdbc connectivity, it works fine and i can display textual information. Also i have checked separately whether the BufferedImage is having some data by checking for null and it seemed to have data. But the servlet simply fails to showup the image and i just end up getting a blank screen.

works on tomcat 7 [http://localhost:8081/testimage/ReturnImage?code=AUS]

doesnt work on tomcat 5.5 [http://localhost:8080/testimage/ReturnImage?code=AUS]

The servlet just displays a blank screen for the latter. Below is the code for my servlet.

package flags;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.awt.image.BufferedImage;

public class ReturnImage extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //PrintWriter out = response.getWriter();
        // OutputStream outimg = response.getOutputStream();

        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://192.168.2.2:3306/world", "root", "abcdef");

            //out.println("Connecting to database <br>");

            Statement statement = connection.createStatement();
            String param;
                param = request.getParameter("code");

            String sql = "Select Name,Flag,Code from world.Country where Code='"+ param + "'";
            ResultSet res = statement.executeQuery(sql);
            while (res.next()) {
                String Name = res.getString("Name");
                String Code = res.getString("Code");
                BufferedImage image = javax.imageio.ImageIO.read(res.getBlob("Flag").getBinaryStream());
                //out.println(System.getProperty("java.runtime.version"));
                //out.println(Code + " ");
                //out.println(Name + "<br>");
                if (image == null) {
                    //out.println("null image");
                }
                 ImageIO.write(image, "gif", outimg);
                 outimg.close();        
            }
            res.close();
            statement.close();
            connection.close();

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

Image shows the servlet output in two different tomcat instances. Tomcat 5.5 uses JDK1.5 and Tomcat 7 uses JDK 1.6

Was it helpful?

Solution

You probably need the setContentType set correctly (as you currently do).

However, have you tried using a different format, like PNG (and of course, use "image/png" as content type)? I'm not sure Java 1.5 has a GIF writer, due to the LZW licensing issues of the past. Note that the ImageIO.write methods have a boolean return type, to check if anything was written. In any case, PNG should always work.

PS: Unless you are planning to modify the image in the servlet, it is of course much faster to store the image in the right format in the blob, and just passing it down to the client without decoding/encoding it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top