This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Context Listener  RSS feed

 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have defined a servlet Context Listener in my web app which will be loaded on server startup

From that servlet i want to read a properties file. The challenge is that the file is located in config folder.

WEB-INF
|
|____ classes
|
|___________config/env.properties

Also while creating my web application i am creating a jar of my classes and placing it in the lib foolder in WEB-INF

I am not able to load the properties file in servlet context listener.

For this I have 2 questions
1. I am doing. Is this correct ?
2. If yes then how will this vary across different application servers. This is working fine in tomcat but not in glassfish.

Thanks in advance for your help
 
Sudhir Ravindra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assuming that the property file exists in WEB-INF/classes or as a jar in WEB-INF/lib
loggingProperties.load(initializeEvent.getServletContext().getResourceAsStream("env.properties"))
should work.
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The property file is not under classes or lib folder but it is under config directory inside WEB-INF
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you can't access it as a resource. You'll need to use ServletContext.getRealPath and FileInputStream (or even better, FileReader).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Rob, but that's not correct.

ServletContext.getResourceAsStream() will open a resource anywhere in the web app. It doesn't use a class loader so the resource does not need to be in the class path.
 
Youssef Ben Kaddour El Wazzani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Anuj Batra wrote:
1. I am doing. Is this correct ?

It seems correct, but you have to remove WEB-INF, try with:
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So

This means that the implementations vary accross the application servers ?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Sorry Rob, but that's not correct.

ServletContext.getResourceAsStream() will open a resource anywhere in the web app. It doesn't use a class loader so the resource does not need to be in the class path.

Never knew that... I'd still go with getRealPath though. It doesn't add to the confusion where a resource will be found - relative to the web app, to the WEB-INF/classes folder or inside a JAR file inside the WEB-INF/lib folder.
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear is correct. The getResourceAsStream() method can access any resource ("file") in a WAR, whether it's in the classpath or not and whether it's in a URL-accessible directory or (WEB-INF) not.

getRealPath() does not work on servers/configurations where the WAR has not been exploded and therefore the filesystem path ends at the location of the WAR file. Unless you have a magic filesystem installed on the affected machine that allows open/close/read access on entries within an archive file as though they were independent files in their own right, ordinary file operations will fail when applied to WAR resources.

The proper syntax for getResourceAsStream() is an absolute path (initial slash "/"). For example, "/WEB-INF/classes/com/javaranch/server/label.properties". As far as this particular function is concerned, anything outside the WAR does not exist (for security and portability reasons).
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Youssef,
No luck
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that you're trying to use this:


Since that should work, I'd recommend breaking it down into components and verifying them:


Some minor tweaking of this code will be required. I can never remember the names of the stream-handling components.
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Thanks a lot for your replies. Was a great help for me. However i realized that my approach and the values were correct that I am populating.
The problem was that I was using File.separator instead of "/" which on windows was not working because on windows the File.separator will return "\"
instead of "/". Such a silly thing.
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than use file.separator, I recommend always using "/" in Java code that needs to reference a file. It works under ALL OS's, not just Unix/Linux or Windows.

Even better where applicable:


Technically I'm not sure that web resources qualify as "files", in any event. As I said, in the strict J2EE implementation the contents of a WAR are ZIP file components, not actual files. So the OS filesystem path conventions may simply not apply. In fact, for web resources, java.io.File probably can't even be used unless the "getRealPath()" returns a non-null value (meaning that the WAR was exploded).
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim,

Will keep this in mind from next time
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!