Displaying an image in JSP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amardeep Verma

    #1

    Displaying an image in JSP

    Hi,
    I have an image, in JPEG format, stored inside a byte array.
    I have to render that image. I will not be able to open a file
    connection and write that image to HDD, either on client side or on
    server side. So my query is this, how do I render an image which is
    stored as a series of bytes? I have tried out.println and IMG tag, but
    they do not work.

    Any help would be appreciated

    Bye
    Amardeep Verma
  • Andy Fish

    #2
    Re: Displaying an image in JSP

    presumably your JSP page is currently producing HTML. HTML cannot contain
    image data in the file itself, only through an external href.

    so the best option would be to generate an <img> tag in your JSP which
    contains a URL to a different JSP (or the same jsp with different
    parameters). When the browser requests the image, you can send the binary
    data as content type image/jpeg.

    Andy


    "Amardeep Verma" <addverma@email .com> wrote in message
    news:1a9170e5.0 307312310.1cb96 d01@posting.goo gle.com...[color=blue]
    > Hi,
    > I have an image, in JPEG format, stored inside a byte array.
    > I have to render that image. I will not be able to open a file
    > connection and write that image to HDD, either on client side or on
    > server side. So my query is this, how do I render an image which is
    > stored as a series of bytes? I have tried out.println and IMG tag, but
    > they do not work.
    >
    > Any help would be appreciated
    >
    > Bye
    > Amardeep Verma[/color]


    Comment

    • Chris

      #3
      Re: Displaying an image in JSP

      "Amardeep Verma" <addverma@email .com> wrote in message
      news:1a9170e5.0 307312310.1cb96 d01@posting.goo gle.com...[color=blue]
      > Hi,
      > I have an image, in JPEG format, stored inside a byte array.
      > I have to render that image. I will not be able to open a file
      > connection and write that image to HDD, either on client side or on
      > server side. So my query is this, how do I render an image which is
      > stored as a series of bytes? I have tried out.println and IMG tag, but
      > they do not work.[/color]

      I haven't tried this, but my guess is that you could include a tag like this
      in your html: <img src="myimage.js p">. Then have myimage.jsp return the byte
      data. Be sure to set the http response header to indicate that it's a jpeg.




      Comment

      • Roedy Green

        #4
        Re: Displaying an image in JSP

        On 1 Aug 2003 00:10:55 -0700, addverma@email. com (Amardeep Verma)
        wrote or quoted :
        [color=blue]
        >So my query is this, how do I render an image which is
        >stored as a series of bytes?[/color]

        see http://mindprod.com/jgloss/image.html

        --
        Canadian Mind Products, Roedy Green.
        Coaching, problem solving, economical contract programming.
        See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

        Comment

        • Raymond DeCampo

          #5
          Re: Displaying an image in JSP

          Chris wrote:[color=blue]
          > "Amardeep Verma" <addverma@email .com> wrote in message
          > news:1a9170e5.0 307312310.1cb96 d01@posting.goo gle.com...
          >[color=green]
          >>Hi,
          >> I have an image, in JPEG format, stored inside a byte array.
          >>I have to render that image. I will not be able to open a file
          >>connection and write that image to HDD, either on client side or on
          >>server side. So my query is this, how do I render an image which is
          >>stored as a series of bytes? I have tried out.println and IMG tag, but
          >>they do not work.[/color]
          >
          >
          > I haven't tried this, but my guess is that you could include a tag like this
          > in your html: <img src="myimage.js p">. Then have myimage.jsp return the byte
          > data. Be sure to set the http response header to indicate that it's a jpeg.
          >[/color]
          FYI, since JSPs implicitly open the output stream as a character stream
          and the specification prohibits one from re-opening it as a byte stream,
          this is best done from a servlet instead of a JSP.

          Ray

          Comment

          • Raymond DeCampo

            #6
            Re: Displaying an image in JSP

            Allen E. Scharfenberg wrote:[color=blue]
            > I think you can set the MIME type you are returning in both servlets and
            > JSP. SO, you could set the MIME type being returned to image\jpeg or
            > whatever, convert the byte[] to a character[], and then just slam it
            > through.
            >[/color]

            No; you can set the MIME type if you like, but a JSP is only allowed to
            return character data. There was a tech tip from Sun a while back that
            dealt with this very issue. The original tip suggested using JSPs, but
            the next tip made the correction. In fact, I just found the tip in my
            email library; it wasd the tech tip for 8/21/2001.

            Here are the links:



            Ray

            Comment

            • devatha
              New Member
              • Jul 2006
              • 1

              #7
              Originally posted by Raymond DeCampo
              Allen E. Scharfenberg wrote:[color=blue]
              > I think you can set the MIME type you are returning in both servlets and
              > JSP. SO, you could set the MIME type being returned to image\jpeg or
              > whatever, convert the byte[] to a character[], and then just slam it
              > through.
              >[/color]

              No; you can set the MIME type if you like, but a JSP is only allowed to
              return character data. There was a tech tip from Sun a while back that
              dealt with this very issue. The original tip suggested using JSPs, but
              the next tip made the correction. In fact, I just found the tip in my
              email library; it wasd the tech tip for 8/21/2001.

              Here are the links:



              Ray
              Hi Ray

              Thanks a lot. You solved my problem. I was thinking along the same lines on setting the mime type on jsp response. These URLs showed how to do it exactly.

              Comment

              Working...