Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting out of WEB-INF  RSS feed

 
S Gibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,
By a series of events I've ended up working on a big site made out of JSP and the like. I'm pretty happy coding java, I've done a few years at university, but obviously putting it all in a web context is a bit of a learning curve... So treat me as a newbie.

This question may seem ridiculously simple to some but I've searched all over and tried endless tests and bits of code with no joy. The site is JSP pages with some servelets and classes. Now I have some classes that are particularly useful that play with files. These originally had the path to the files hard coded in, strangely enough hardcoded paths do not work anymore after a server change (yes I did change the path to reflect the new server, still no luck ). But to be honest, why should we have hardcoded when we can have relative?

Now the classes that play with the files are not servlets are they? They're only servlets if you define them as that? (is that right?) Now after endless testing I managed to get a relative path working...


As you can probably guess though this is inside the WEB-INF/classes folder. I've tried everything to get out of the classes folder but all attempts have resulted in a null value being returned. Other methods of getting a relative path have failed (using getContext() couldn't be resolved due to not being able to extend some of the classes because they're already extended by other things :S). So feel free to give other suggestions.

Sorry if these questions are too simple, as I'm new to the language I maybe missing some vital bits of info!
Thanks for your time!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the getClass.getResource method you can't access anything outside of the classes directory. It uses the classloader, which -no surprise there- only knows about things in the classpath.

Is there a particular reason you want to use Image.getInstance? The customary way to load images these days is javax.imageio.ImageIO.read (which can load images from everywhere, because it doesn't rely on classloaders).
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. The reason you are not able to move it out of WEB-INF/classes is because you use:

this.getClass().getResource

This obvioulsy looks for the resource in the context of the current class, which is the WEB-INF/classes folder.

2. The ideal way to read these resources (like images) in a web application is to use getServletContext().getResource() method. That way, you can place the images under your webapp context instead of WEB-INF/classes folder.

You might want to check out if there is a way of making the ServletContext object available in this class where you are loading images or if there is a way to get the image in a servlet class and making it available here.
 
S Gibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Using the getClass.getResource method you can't access anything outside of the classes directory. It uses the classloader, which -no surprise there- only knows about things in the classpath.

Is there a particular reason you want to use Image.getInstance? The customary way to load images these days is javax.imageio.ImageIO.read (which can load images from everywhere, because it doesn't rely on classloaders).


That makes a lot of sense yeah >_<

The reason for the older code currently existing on the site is down to how long it's been in development. It's been a while and obviously things need updating. I'm getting it back in working order and fixing the few incomplete systems it has. The path stuff has been throwing me though! Will have a try at using the suggested method. I am concerned about resolving the file though.

Originally posted by Sravan Kumar:
1. The reason you are not able to move it out of WEB-INF/classes is because you use:

this.getClass().getResource

This obvioulsy looks for the resource in the context of the current class, which is the WEB-INF/classes folder.

2. The ideal way to read these resources (like images) in a web application is to use getServletContext().getResource() method. That way, you can place the images under your webapp context instead of WEB-INF/classes folder.

You might want to check out if there is a way of making the ServletContext object available in this class where you are loading images or if there is a way to get the image in a servlet class and making it available here.


I did have an attempt at trying to get the ServletContext object available, but it said that I was trying to use it in a non-static context or something similar. Any ideas?

Thanks for your time and help!
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide some code so we can look through? The method which is loading this file, how the class is defined and how the variables are defined in it? I would be surprised if the ServletContext object is held within this class as a static object ..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!