Current Position:Home > How to dynamically display images in JSPs

How to dynamically display images in JSPs

Update:10-11Source: network consolidation
Advertisement
This took a little while to figure out so I thought I'd share. After
          doing some research I was led to the following approach on how to load
          images from an Oracle database into a JSP:
          The "main" JSP:
          <HTML>
          <head>
          <title>Image Test</title>
          </head>
          <body>
          <center>
          hello
          <P>
          <img border=0 src="getImage.jsp?filename=2cents.GIF">
          <P>
          <img border=0 src="getImage.jsp?filename=dollar.gif">
          <P>
          world
          </body>
          </HTML>
          And this is the image getter:
          <% try {
          response.setContentType("image/gif");
          String filename = (String) request.getParameter("filename");
          java.sql.Connection conn =
          java.sql.DriverManager.getConnection("jdbc:weblogic:pool:orapool"); //
          connect to db
          java.sql.Statement stmt = conn.createStatement();
          String sql = "select image from testimage where filename = '" +
          filename + "'";
          java.sql.ResultSet rs = stmt.executeQuery(sql);
          if (rs.next()) {
          byte [] image = rs.getBytes(1);
          java.io.OutputStream os = response.getOutputStream();
          os.write(image);
          os.flush();
          os.close();
          conn.close();
          catch (Exception x) { System.out.println(x); }
          %>
          The thing to note is that there are no <%@ page import="..." %> or <%@
          page contentType="..." %> tags - just the single scriptlet. It
          seems that for every "<%@" the weblogic compiler sees it puts
          out.print("\r\n"); statements in the generated java source.(???) I
          don't know much about how browsers work but I think that once it sees
          flat ascii come at it it treats everything that follows as text/plain
          which is incorrect for the binary stream that's being sent. Another
          work around was to set out = null; but that's kind of ugly and might
          produce server errors. The real fix is to write a bean to handle images
          which I'll work on next (does anybody have any hints on how to do
          that?)
          -Erik.
          PS, thank you to the people that posted info in this group that helped
          me track down this problem. -
          

The Best Answer

Advertisement
Erik,
          No need to write a bean, just write a serverlet...
          you can use this function (however you might want to include some
          improved error handeling)
          Oh, FYI to prevent caching by the browser you might want to pass a parameter
          into the creation of the image like timeseconds or (in my case) variable
          text which
          was included into the image produced. (You need not even use the variable in
          the code, the server is not bright enough to tell)
          <IMG SRC=".....MyImageProducer?file=my.jpeg&timesecs=12345134">
          Jay L. Toops
          public boolean httpJpegStreamWriter(BufferedImage ib, HttpServletResponse
          response) {
          try {
          javax.servlet.ServletOutputStream myout = response.getOutputStream();
          byte b[] = new byte[1024];
          response.setContentType("image/jpeg");
          //File file = new File("./myserver/public_html", "testjlt.jpg");
          //FileOutputStream fos = new FileOutputStream(file);
          ByteArrayOutputStream fos = new ByteArrayOutputStream();
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
          JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(ib);
          param.setQuality(1.0f,false);
          encoder.setJPEGEncodeParam(param);
          encoder.encode(ib);
          b = fos.toByteArray();
          response.setContentLength(b.length);
          myout.write(b, 0, b.length);
          myout.flush();
          myout.close();
          } catch (Exception e) {
          System.out.println("MY httpJpegStreamWriter Exception\n is :"+
          e.toString());
          return(false);
          return(true);
          Erik Lindquist wrote:
          > This took a little while to figure out so I thought I'd share. After
          > doing some research I was led to the following approach on how to load
          > images from an Oracle database into a JSP:
          >
          > The "main" JSP:
          >
          > <HTML>
          > <head>
          > <title>Image Test</title>
          > </head>
          > <body>
          > <center>
          > hello
          > <P>
          > <img border=0 src="getImage.jsp?filename=2cents.GIF">
          > <P>
          > <img border=0 src="getImage.jsp?filename=dollar.gif">
          > <P>
          > world
          > </body>
          > </HTML>
          >
          > And this is the image getter:
          >
          > <% try {
          > response.setContentType("image/gif");
          > String filename = (String) request.getParameter("filename");
          > java.sql.Connection conn =
          > java.sql.DriverManager.getConnection("jdbc:weblogic:pool:orapool"); //
          > connect to db
          > java.sql.Statement stmt = conn.createStatement();
          > String sql = "select image from testimage where filename = '" +
          > filename + "'";
          > java.sql.ResultSet rs = stmt.executeQuery(sql);
          > if (rs.next()) {
          > byte [] image = rs.getBytes(1);
          > java.io.OutputStream os = response.getOutputStream();
          > os.write(image);
          > os.flush();
          > os.close();
          > }
          > conn.close();
          > }
          > catch (Exception x) { System.out.println(x); }
          > %>
          >
          > The thing to note is that there are no <%@ page import="..." %> or <%@
          > page contentType="..." %> tags - just the single scriptlet. It
          > seems that for every "<%@" the weblogic compiler sees it puts
          > out.print("\r\n"); statements in the generated java source.(???) I
          > don't know much about how browsers work but I think that once it sees
          > flat ascii come at it it treats everything that follows as text/plain
          > which is incorrect for the binary stream that's being sent. Another
          > work around was to set out = null; but that's kind of ugly and might
          > produce server errors. The real fix is to write a bean to handle images
          > which I'll work on next (does anybody have any hints on how to do
          > that?)
          >
          > -Erik.
          >
          > PS, thank you to the people that posted info in this group that helped
          > me track down this problem. -
          
  • Dynamic display in a jsp Update:10-11

    Hi all, My requirement is I need to display a calender on the jsp and when the user clicks on a date I need to display a set of radio buttons underneath the calender on the same page. The date field on the calendar is an anchor tag. So what I need to

  • How to dynamically display images in JSPs Update:10-11

    This took a little while to figure out so I thought I'd share. After           doing some research I was led to the following approach on how to load           images from an Oracle database into a JSP:           The "main" JSP:           <HT

  • JSP : Dynamic display Update:11-30

    Hi, I need to display 4 html tables which are constructed and populated with data from java. So i make 4 method calls and pass the out object to the methods, These methods populate the table with data and then display them using the out parameter. Bu

  • Reg dynamically displaying records from database into a jsp page Update:10-11

    I am working in a project which is using struts and hibernate.In a class I gave code for retrieving data from a table and I put it in a list. List retrieveList; public List getRetrieveList() { String SQL_QUERY ="from student s"; Query query = se

  • How to dynamically display .flv files in website Update:10-11

    I'm using a JSP for my interface.?? In the webpage, I want to pass a java variable, which holds the url to the video that was retrieved from the database, to the flash player script to dynamically determine which video to play.?? For example, this co

  • Dynamic Themes in mapinit.jsp Update:10-11

    Hi , How can I pass dynamic themes using mapinit.jsp , For example alongwith all the parameters like map size , basemap etc.. I want to give a new date parameter . based on the given date I need to put a where clause in my themes like where date=para

  • Display image on jsp page Update:10-11

    I have to display image on jsp page with some text output. This image is already saved at a location parallel to web-inf and is generated dynamically using a servlet. I have used img tag html to display the image. Other outputs are taking their value

  • Display image in JSP Portlet Update:10-11

    I create a JSP portlet. But The portlet can't display image(gif file, jpg file). I have modified the provider.xml and the following line is added: <imageURL>URL_Path</imageURL> But, the image still cannot be displayed. How can I display image

  • Dynamically displaying a new region (row?) based on immediate user input Update:10-11

    Whew, figuring out a title was almost as hard as trying to explain what I want to do! Okay, a little background first. My app has 178 main data fields, spread across about 35 tables. The users want to be able to search any and all data fields. So, I

  • Junk characters like" � � "displayed in the jsp page please help. Update:10-11

    Hi, I am getting junk characters like � � displayed in the jsp page. In the JSP page i used javascript "& nbsp" for appending spaces to a string "CCR" to get "CCR " . Now the Resultant string "CCR " has three sp