Two Laptop Bag*
The moose likes JSP and the fly likes Showing image from a JAR on a JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Showing image from a JAR on a JSP page" Watch "Showing image from a JAR on a JSP page" New topic
Author

Showing image from a JAR on a JSP page

Markus Källander
Greenhorn

Joined: May 20, 2009
Posts: 15
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

Joined: Mar 28, 2007
Posts: 403
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


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Markus Källander
Greenhorn

Joined: May 20, 2009
Posts: 15
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

Joined: Mar 28, 2007
Posts: 403
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 10, 2002
Posts: 61082
    
  66

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,


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: Showing image from a JAR on a JSP page