Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Showing image from a JAR on a JSP page

 
Markus Källander
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do I show an image stored in a jar file on a JSP page? I am using the tango project's icons.

Thanks
Markus
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Let's say we have a JAR file containing a bunch of .gif image files that we want to use in our application. Here's how we could access an image file from the JAR using the JARRESOURCES



That code snippet shows that we can create a JarResources object initialized to the JAR file containing the resource that we're interested in using -- Images.jar. We then use the JarResources' getResource() method to provide the raw data from the ujjwal_photos.jpg file for the AWT toolkit's createImage() method
 
Markus Källander
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjwal soni wrote:Hi,

Let's say we have a JAR file containing a bunch of .gif image files that we want to use in our application. Here's how we could access an image file from the JAR using the JARRESOURCES



That code snippet shows that we can create a JarResources object initialized to the JAR file containing the resource that we're interested in using -- Images.jar. We then use the JarResources' getResource() method to provide the raw data from the ujjwal_photos.jpg file for the AWT toolkit's createImage() method


And then, do I add it to the JSP like this?



/Markus>
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You cannot refer to image object directly, you need to save that first.

Try this functions




then on jsp page......

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No; you'll still need to get the image from the server--recall that an image tag makes a request for an image.

This is usually handled with a image-serving servlet (or whatever back-end technology you're using).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to save it as a file on disk -- simply write a servlet that streams the image read from the jar and address that servlet in the image tag,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic