4 Replies Latest reply on Oct 9, 2002 12:30 PM by Zarni Worgl

    displaying image from a database in jsp

    Zarni Worgl Newbie

      Hello, does someone have an example how to
      display an image that i get from a bean in a variable?

      How do i get it into the <img src> ...
      and i don't want to load images from disk, i won't to
      load them directly from the bean...

      thanks, zarni

        • 1. Re: displaying image from a database in jsp
          Mikael Eriksson Novice

          In the src part of the tag, refer to a servlet that take a parameter telling what image you want.

          The servlet should then get the image from the db and stream it to its output.

          Regards.

          • 2. Re: displaying image from a database in jsp
            Zarni Worgl Newbie

            thanks for the reply,
            i figured this would be the case,

            >In the src part of the tag, refer to a servlet that take >a parameter telling what image you want.

            >The servlet should then get the image from the db and >stream it to its output.

            do you have such a servlet, or a simple example, i just don't won't to write it my self...:-)
            please...

            regards, zarni

            • 3. Re: displaying image from a database in jsp
              Dmitry Namiot Newbie

              check out DB taglib from Coldtags suite
              http://www.servletsuite.com/jsp.htm
              You can do it right with JSP.

              • 4. Re: displaying image from a database in jsp
                Zarni Worgl Newbie

                I did it my self, becouse nobody helped me ...

                :-)

                here's my servlet for displaying images of any kind:
                u just supply it a filename on the server...

                package galaktimat.ekatalog.servlet;

                import java.io.*;
                import javax.servlet.*;
                import javax.servlet.http.*;

                public class ImgServlet extends HttpServlet
                {

                public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
                // Get the absolute path of the image
                ServletContext sc = getServletContext();
                String filename = req.getParameter("fajl");
                //String fajl = sc.getAttribute("itemImage");
                //String filename = sc.getRealPath("fajl");

                if (filename!=null) {
                System.out.println("[ImgServlet] procesiram rekvest!");
                // Get the MIME type of the image
                /*String mimeType = sc.getMimeType(filename);
                if (mimeType == null) {
                sc.log("Could not get MIME type of "+ filename);
                System.out.println("[ImgServlet] Could not get MIME type of "+filename);
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
                }*/

                // Set content type
                String extension = null;
                extension = filename.substring(filename.lastIndexOf("."));

                if ( extension!=null ){
                resp.setContentType("image/" + extension);

                // Set content size
                File file = new File(filename);
                resp.setContentLength((int)file.length());

                // Open the file and output streams
                FileInputStream in = new FileInputStream(file);
                OutputStream out = resp.getOutputStream();

                // Copy the contents of the file to the output stream
                byte[] buf = new byte[1024];
                int count = 0;
                while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
                }
                in.close();
                out.close();
                System.out.println("[ImgServlet] slika je bila izpisana!");
                }}
                }
                }


                to use it write this in your jsp:

                <img src="/imgservlet?fajl=name-of-file" border=0 alt="picture" name="image">

                of course after u set up your servlet mapping in web.xml:


                <servlet-name>imgservlet</servlet-name> <servlet-class>galaktimat.ekatalog.servlet.ImgServlet</servlet-class>
                <load-on-startup>2</load-on-startup>




                <servlet-mapping>
                <servlet-name>imgservlet</servlet-name>
                <url-pattern>/imgservlet</url-pattern>
                </servlet-mapping>


                thanks for help anyway-...

                zarni