Display image in jsp by retrieving it from DB2

I insert image in to db2 table using the code given below
PreparedStatement preparedStatement =
connection.prepareStatement("INSERT INTO BOOKCOVERS VALUES(?,?)");
File imageFile = new File("c:\\redbookcover.jpg");
InputStream inputStream = new FileInputStream(imageFile);
preparedStatement.setString(1," 0738425826");
preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length()));
preparedStatement.executeUpdate();
Now i need to retrieve the image and display in jsp page!!

One way would be to write a Servlet... http://yourpage/ImageServlet/?id=0738425826
Servlet:
- get url parameter id
- get the inputstream of the object from the db
- read and directly write the data to the servletoutputstream
regards
slowfly

Similar Messages

  • Display image on jsp page

    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 values from database.
    First problem is that image will be taking time to display in comparision of other outouts from database. I have to refresh the page to get imageon my page.
    Second is that if I save image in a folder parallel to web-inf in my project then it will not be displaying the image.
    Can I use any jsp functionality to display image with other outputs from database. I have used "*include*". but it shows only that image and other outputs.

    Best way is to use a servlet for this.
    <img src="path/to/imageservlet?id=someidentifier">
    <!-- or -->
    <img src="path/to/imageservlet/someidentifier">In the servlet's doGet() just write code which gets an InputStream of the image (either directly generated, or just read from the local disk file system, if necessary with help of ServletContext#getRealPath(), or even from the DB by ResultSet#getBinaryStream()) and writes it to the OutputStream of the response. That's basically all. Don't forget to buffer the streams properly to speedup performance and to save memory.
    You can find here a basic example: [http://balusc.blogspot.com/2007/04/imageservlet.html].

  • Display image in JSP Portlet

    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 in JSP portlet?

    Leo Cheung,
    You could try the following :
    1. Add a virtual directory path Alias 'imgf' in the Apache configuration file httpd.conf to load the image file. Add the following line under the alias section :
    Alias /imgf/ "<your directory>\images/"
    2. Place your gif/jpg files (eg., work.gif) in the images directory.
    3. Use the IMG tag of HTML :
    <IMG src="/imgf/work.gif" border=0 width=80 height=80> in the JSP file at the location where you need to display the image.
    Hope this helps
    Pushkala

  • Error retrieving data from DB2 NoSuchMethodEror:sun.io.ByteToCharSingleByte

    Hi All,
    I am trying retrieve data from DB2 database using select command from java code,
    able to establish connection but not getting the data, the following error was occurred,
    can any one help out me...
    Exception in thread "main" java.lang.NoSuchMethodError: sun.io.ByteToCharSingleByte.createCharMap(Ljava/lang/String;Z)[C
    at sun.io.ByteToCharCp870.<clinit>(ByteToCharCp870.java:86)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sun.io.Converters.getConverterClass(Unknown Source)
    at sun.io.Converters.newConverter(Unknown Source)
    at sun.io.ByteToCharConverter.getConverter(Unknown Source)
    at com.ibm.db2.jcc.b.j.a(j.java:426)
    at com.ibm.db2.jcc.c.kc.a(kc.java:2366)
    at com.ibm.db2.jcc.c.jb.a(jb.java:3167)
    at com.ibm.db2.jcc.c.jb.a(jb.java:1720)
    at com.ibm.db2.jcc.c.jb.a(jb.java:1647)
    at com.ibm.db2.jcc.c.jb.n(jb.java:562)
    at com.ibm.db2.jcc.c.jb.j(jb.java:247)
    at com.ibm.db2.jcc.c.jb.c(jb.java:57)
    at com.ibm.db2.jcc.c.w.c(w.java:42)
    at com.ibm.db2.jcc.c.cc.h(cc.java:178)
    at com.ibm.db2.jcc.b.sf.p(sf.java:1256)
    at com.ibm.db2.jcc.b.sf.a(sf.java:1954)
    at com.ibm.db2.jcc.b.sf.a(sf.java:523)
    at com.ibm.db2.jcc.b.sf.executeQuery(sf.java:507)
    Edited by: siva512 on Dec 11, 2008 5:37 PM

    siva512 wrote:
    Exception in thread "main" java.lang.NoSuchMethodError: sun.io.ByteToCharSingleByte.createCharMap(Ljava/lang/String;Z)[CThe mentioned method is missing in the mentioned class during runtime, while it was available during compiletime.
    This generally indicate a version conflict. Your classpath is dirty. The older versioned class without that method apparently got precedence in classloading above the newer versioned class with that method. Cleanup your classpath. Check all classes/JAR´s and remove all duplicated older versions.

  • Problem for displaying image in jsp

    I like to retrieve and display image from database in jsp file. Now, image has been retrieved and store in session. But it cannot be displayed in jsp. Who can help me? Thanks a lot!
    Codes like below:
    <%
    byte[] b = (byte[])session.getAttribute("sessionName");
    System.out.println(b.length); //the length can be printed at console successfully
    if(b != null) {
    response.setContentType("image/*");
    OutputStream toClient=response.getOutputStream();
    toClient.write(b);
    toClient.flush();
    toClient.close();
    %>

    JSP is designed to return text/html documents.
    As such it automatically calls getWriter() to return a character stream. (places it in the implicit variable "out"
    Seeing as you want to return bytes, instead of character data, the JSP technology is not applicable
    Calling getOutputStream after getWriter has been called results in an IllegalStateException
    So calling getOutputStream in a JSP will ALWAYS throw an exception.
    Cheers,
    evnafets

  • ADF BC:Displaying Image On JSP page

    Hai All
    I'm using ADF BC and JDev 10.1.3.
    I want to show an image on my jsp page from database.
    For this I have written an servlet with the following code
    response.setContentType("image/gif");
    OutputStream os = response.getOutputStream();
    String amDef = "package.AppModuleName";
    String config = "Configuration Name";
    ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
    ViewObjectImpl vo =
    (ViewObjectImpl)am.findViewObject("ViewObject"); // get view object (the same as used in the table)
    System.out.println("vo:" + vo);
    vo.executeQuery();
    Row row = vo.first();
    BlobDomain image = (BlobDomain)row.getAttribute("field");
    //System.out.println("image:" + image);
    InputStream is = image.getInputStream();
    // copy blob to output
    byte[] buffer = new byte[10 * 1024];
    int nread;
    while ((nread = is.read(buffer)) != -1)
    os.write(buffer, 0, nread);
    os.close();
    Configuration.releaseRootApplicationModule(am, false);
    This servlet working perfectly.The image is displayed on the page while running
    the servlet alone.
    My problem is the image is not got displyed on the jsp page
    I tried following code to call the servlet
    <af:objectImage source="ImageServlet"/>
    <img src="ImageServlet" width="140px" height="50px" align="right">
    Where ImageServlet is the url-pattern in the web.xml for the servlet
    Both method are not working
    Any body please help me.......
    what I'm missing..............
    Is there any other way to display an image on JSP page using backing bean
    method.
    Thanks
    Ans

    HI,
    See: http://kuba.zilp.pl/?id=241
    Kuba

  • Problem in displaying image on jsp page

    I want to display an image on jsp page.I copied the image the image in WebContent folder.
    I am able to see the image on the design pane when using the following code:-
    <img src="/image.gif" height="50" width="50"> but when i run it in the browser nothing gets diplsyed.
    also when i use the image container and select the source option that image is not displayed in the WebContents file..plz help.

    Hi,
    Seems that the Problem is there with the way how you specifuied the Image relative Path:
    You have specified the below: The opath of image starts with (/) forward slash:
    *<img src="/image.gif" height="50" width="50">*
    It means to access this Image WebLogic Container will form a Path like this:
    http://localhost:7001/image.gif
    BUT may be the image is available inside your Context root: So Change the <Img> tag like following in your JSP:
    *<img src="image.gif" height="50" width="50">*
    (*NOTE:* Never Start your src path with a Preceesing /)
    Now WLE will consider the path like below ..if your Applications Context root is "TestApp"
    http://localhost:7001/TestApp/image.gif
    I am Assuming that Inside the TestApp Application "image.gif" And "your.jsp" jSP pages are Co-Located (Means available in the Same Directory).
    Thanks
    Jay SenSharma
    http://jaysensharma.wordpress.com (WebLogic Magical Stuff)

  • Displaying Image In JSP for Mozilla Browser

    Hai all
    How to display the .gif image in JSP file using Mozilla browser

    do you really think you give enougth information???
    the answer to your question is using the html tag <img src='image_url'>

  • Display image in JSP

    Hi,
    I'm having problems allowing users to upload images within my j2ee application and then displaying them again. Does anybody have any tips?
    I don't really want to store the image in the db (just the image name), but I can't figure out where to put the image once it has been uploaded so that the JSP page can find it again.
    All of my current images are inside my .war file so I can't simply add the uploaded image to these. I'm using JBoss 4.0 if it matters....
    Your help is greatly appreciated,
    Stu.

    Hi.
    Probably not the best solution, but I've solved this by creating one war file that only holds images. It only has an empty jsp index file.
    I use a Unix workstation, so I've creted a symlink, (shortcut) from the folder I uppload images to, to the empty project. Then I can call the images from the jsp, and deploy new versions of my main application without the images....

  • Displaying Image in JSP page

    Hi there.. I have a problem with displaying an image from table with blob data type.
    Here's what have I done:
    I have this page to display an image from requestScope.test (I've set this value in data action).
    But the problem is, the image is only displayed once. Seems that after it read the eof of stream, It'll not move the pointer to the beginning. The image will show up again if view object retrieve new data from the database.
    Here's my code:
    ------------------------------------------------------begin of code----------------------------------------------------
    <%@ page import="java.io.InputStream,oracle.jbo.client.Configuration,oracle.jbo.ApplicationModule"%>
    <%
    response.setContentType("image/jpeg");
    response.addHeader("Content-Disposition","filename=img.jpg");
    InputStream sImage = ((oracle.jbo.domain.BlobDomain)request.getAttribute("test")).getInputStream();
    byte[] bytearray = new byte[4096];
    int size = 0;
    while((size=sImage.read(bytearray))!= -1 ){
    response.getOutputStream().write(bytearray,0,size);
    sImage.close();
    %>
    ------------------------------------------------------end of code----------------------------------------------------
    Any suggestion?
    Thx,
    Andre

    Sorry, I've just read the java doc for oracle.jbo.domain.BlobDomain and find method toByteArray(), and this method can solve my problem. Here's the new code:
    ----------------------------------------begin of code-----------------------------------------------
    <%
    response.setContentType("image/jpeg");
    response.addHeader("Content-Disposition","filename=img.jpg");
    byte[] bytearray = ((oracle.jbo.domain.BlobDomain)request.getAttribute("test")).toByteArray();
    response.getOutputStream().write(bytearray, 0, bytearray.length);
    %>
    ----------------------------------------begin of code-----------------------------------------------
    But I still intereset with the first solution, why it always find eof after it displayed. I've already closed the stream.
    Thx,
    Andre

  • How to dynamically display images in JSPs

    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. -
              

    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. -
              

  • How to display images in JSP

    Hi all,
    I created a Jsp page and I stored my images in the dist-->images folder and by using the following code
    <% String image = componentRequest.getWebResourcePath() + "/images/image.gif"; %>
    <img src="<%=image%>">
    I try to display the images in that page.
    Yes I am successful in displaying one image.
    But my problem here is to display more than two images which are present in that page.
    Can any body help me regarding this.
    Your help will be highly rewarded with points.
    Regards,
    Ramana.

    Hi Ramana,
    Set the image border to 1 and check atleast your borders get displayed. If it displays the border, then the problem may be with your image path.
    Before deploying for each modification in your JSP, check the preview of the page using "Preview" tab.
    Are you getting any exception?
    Check your whole JSP page once again (check all the scriplets).
    Regards,
    Uma
    Message was edited by: Uma Maheswari

  • Displaying image on jsp

    Hi,
    I am setting image byte array to servlet output stream after setting content type as "image/jpg".
    Problem 1:
    if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.
    Problem 2 :
    For "image/jpg", File Download window pops up and asks me for saving the file. I don't want to save it.
    Below is the sequence of code in my action class.
    response.resetBuffer();
    response.setContentType("image/jpeg");
    servletOutputStream.write(barray);
    servletOutputStream.flush();
    servletOutputStream.close();
    this image should be displayed on new opened window from where i have called above action.

    Problem 1:
    if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.Well, what is the correct contentType? You can't just set it on whim, it needs to be the correct type. Which is probably why the image that are actually that type show up, while others don't.
    Take a look at these to understand how to improve your code:
    - [http://balusc.blogspot.com/2007/04/imageservlet.html]
    - [http://www.exampledepot.com/egs/javax.servlet/GetImage.html]
    For example, you should set your content length.

  • How to display Image/Logo in Html file generated from Webdynpro App

    Dear All,
      I have developed a webdynpro application and onClick of the Button I am opening  a new browser by generating HTML file.The file is opened successfully.
    My requirement is to display the company logo on that HTML file.  Where to place that image file and how to call that image file in my HTML.
    Regards
    Ponnusamy

    Hi,
    Ex. 1 - If the Index file is in the same directory, called "test"
    tales/numbers.jpg
    Ex. 2 - If the image is in a subdirectory, called "test1" within "test"
    test/test1/numbers.jpg
    img src="test/test1/numbers.jpg"
    Regards
    Ayyapparaj

  • Displaying images along with data on downloading from excel

    Hi All,
    I need to show images along with data in the excel sheet when user clicks downld to excel button.  Is there possible way to show the images in excel sheet if, kindly help.
    Thanks & Regards
    Jaspreet Kaur

    Hi Vinod
    Basically I have a report which needs to be downloaded to excel sheet. For each record there is a particular image in the first column for ex say my colums are img, desc.  Now image column contains an image corresponding to each description.  If its a file, a folder image is there; if its a document, a page image is there etc.  Now i am can display the data of  text fields  in the following manner ie label =new Label(0,RowCount, wdContext.nodeVn_PortalExcel().getVn_PortalExcelElementAt(i).getVa_Role(), PortalRoleDataFormat);
    PortalReportsheet.addCell(label);
    but how do i display the image in the excel sheet?
    Is it possible to do so, if so please provide assistance.
    Thanks & Regards
    Jaspreet Kaur

Maybe you are looking for