This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Applet getResource() from Server or Client? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet getResource() from Server or Client?" Watch "Applet getResource() from Server or Client?" New topic
Author

Applet getResource() from Server or Client?

Fabricio Sanchez
Ranch Hand

Joined: Mar 11, 2003
Posts: 33
Hi there,

I have a textures.jar file on my server (specified with the archive parameter within the applet tag) with a class to use as an anchor and some images. When I run the applet, this is how I get to the images:

URL url = TextureAnchor.class.getResource("image.gif");

I then build an ImageIcon from the images and use as normal.

If I print out the url,
System.out.println( "url: " + url.toString() );
// I get: url: http://www.mysite.co.uk/textures.jar!/image.gif

My question:
How can I make sure that the applet is getting the resource from the local cached version (on the client) instead of going to the server for it?

Does this mean that every time I use a getResource() on an applet it goes to the server for it?

Thank you,

Regards,

Fabricio
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
Does this mean that every time I use a getResource() on an applet it goes to the server for it?

No. Generally the Java Plugin will cache jar files. When the applet is loaded the Plugin checks whether the file on the server is newer than the one in the cache; if it is, it will replace the existing cached copy, but otherwise the cached copy will be used.

Note that the user can turn off jar file caching in the Plugin control panel, but by default it's turned on (and most users probably never touch those settings).


Ping & DNS - my free Android networking tools app
Fabricio Sanchez
Ranch Hand

Joined: Mar 11, 2003
Posts: 33
Thank you for the quick reply, Ulf.

I do know that the jar files are cached by default.

I just want o know how to access the resources contained within them (the cached version, if available) as opposed to always connecting to the server version of the files.

I also want to know if this is the default behavior of applets (that is to access server resources instead of local cached resources with the getResource method) or if I'm doing something wrong.

Thank you,

regards,

Fabricio
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
I don't quite follow. Are you saying that the jar file is retrieved every time despite caching being turned on?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet getResource() from Server or Client?
 
Similar Threads
preloading images
Image as part of HTML email
custom DefaultListCellRenderer only works on one PC
Problem in Image Loading in Swing Applet
Displaying an image in a servlet