aspose file tools*
The moose likes Servlets and the fly likes directory structure questions... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "directory structure questions..." Watch "directory structure questions..." New topic
Author

directory structure questions...

Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Hi all,
I'm going to finally convert my free-form directory structure to something that is seemingly j2ee compliant (more for deployment-ease than anything else, but sell me on additional bonuses too if you feel the urge).
I have read literature on the web-inf structure, but I haven't found something comprehensive yet (links?).
it seems as if I
a) dump javascript and html files in the app-root
b) put my servlets in the web-inf/classes directory
c) put my jar files in the web-inf/lib directory.
Questions:
1) Do I need to pay attention to case-sensitivity in the directory structure?
2) can I put my existing com.yadda.yadda. etc. into the web-inf/classes directory?
3) Do you guys develop from the web-inf/classes directory or do you just build to this directory?
4) I take it that I can put my property files beneath the web-inf directory in just a random 'resource' directory?
Thanks in advance. I knew that I would get all grown up eventually.
Nikki Aniban
Greenhorn

Joined: Oct 10, 2002
Posts: 14
Hi, Matt! To answer you questions:
1. Yes, you need to pay attention to case sensitivity. I tried using "web-inf" and "CLASSES" as my directory names. Both didn't work.
2. Yes, you can put your com.yadda.yadda in your "classes" directory. In fact you can put them anywhere as long as you put their corresponding class files in the "classes" directory.
3. Personally, I make a "src" directory under "WEB-INF" where I put all my .java files. Then I specify in my IDE (Eclipse) that my build output path is the "classes" directory.
4. You have to put your properties file directly under the WEB-INF directory. This is according to the specifications defined by Sun.
Hope this helps! Goodluck!

[ October 21, 2002: Message edited by: Nikki Aniban ]
rastin purr
Ranch Hand

Joined: Jul 20, 2002
Posts: 73
Dear Nikki,
Can you please enplains what you mean by the property files? If it is under the web-inf directory how can it be accessed through the servlet?
Thanks
Ras
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

Just some refinements:
1. In Windows, filename lookup isn't case sensitive. However, in Java code it is, so pay attention to filename case, even under Windows or you'll see strange problems.
2. The proper placement for com.javaranch.saloon.SavePosting.java's class file is "WEB-INF/classes/com/javaranch/saloon/SavePosting.class"
just so there's no confusion.
3. Actually properties files are considered to be equivalent to class files, so if you asked for the "com.javaranch.saloon" properties object, it should be located in "WEB-INF/classes/com/javaranch/saloon.properties".
4. This is for loose classes. Hopefully you can guess how jars work.
BTW, I use the Tomcat recommended directory structure as a model - it's based on Sun recommendations. I'm typically using Ant to compile java from a "src" directory into build/WEB-INF/classes then I JAR the build into a WAR and/or EAR. And, of course, I copy etc/*.* and web/*.* into their proper places in build.


Customer surveys are for companies who didn't pay proper attention to begin with.
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
First: admins, can we have a view of the preexisting posts during our reply? I always lose focus when I reply )
Thanks Nikki, Tim... I believe that I'm starting to see the basic layout. I actually mimic much of this layout I'm beginning to realize, just w/o the ability to deploy through industry utilities.
BTW, one additional note/question: I've seen samples that both placed the .class files 'flat' in the WEB-INF/classes directory and samples that built them in their respective package/directories. I take it that the sample that had the .class files 'flat' in the WEB-INF/classes was just an odd sample, correct?
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Additional question:
must the app-root be SO MESSY
javascript,images & static html files???
Can I pretty please organize this directory into respective /image/* /javascript/* directories? Pretty please?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi Matt, when you reply, scroll down to the bottom , and it gives you a large scrollable text area that the 'previous' page inhabits.

as for an example that had class files 'flat'... they must not have been packaged. Can't see how it would work otherwise.

and for sure, you can treat your web-app directory as the webserver root. so make a bunch of directories like /images /javascript.. but just refer to them this way in your HTML (obviously).
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Thanks Mike, my eyes must have instantly glazed over as soon as I saw the ad at the top of the iframe.
Also, my initial intuition must have been simply that "war directories are messy" (self-fulfilling prophecy from a hold-out?), b/c upon finding a great reference:
http://www.servlets.com/soapbox/servlet22.html
I learned that I could in fact place things in their respective app-root/images/* and web-inf/classes/com/yadda/ directory structures.
Upon that epiphany I looked back at the sample, and sure enough, the samples whose classes were at the WEB-INF/classes/* level were simply not in packages.
Thanks again.
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Nikki, rastin,
I wonder if the confusion about the 'properties file' concerns... the web.xml file? Is this what you are referring to Nikki?
Nikki Aniban
Greenhorn

Joined: Oct 10, 2002
Posts: 14
Yup! I was refering to web.xml when I said properties file. It should have been descriptor file. Sorry for the confusion.
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Okay, much of the conversion was seamless... but of course, I ran into classpath issues.
http://www.coderanch.com/t/348124/Servlets/java/Why-servlets-access-file-relative
In the aforementioned link, as well as many others, posters suggest the following methodology to retrieve a file from the classpath:

My problem is... there are static classes that I utilize that simply do not have access to the ServletConfig object with which to retrieve the resource via some path (eg, "/WEB-INF/properties/alces.properties"). I cannot pass the config object outside of the servlet itself (let alone its descendants... different issue) because it's private.
Is there another way to retrieve a resource through a classpath? I feel as if I'm left w/ workarounds... ie, instructing clients to modify the classpath on their server (which is what I was trying to get away from to begin with) or creating a servlet that pre-loads and initializes my static classes to the desired state.
Thanks in advance.
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
aghaghahghaghhahg! There has to be a better way.
Previously, in 'free-form' mode, a test/rebuild consisted of me recompiling through my IDE... you know, and then TESTING.
Now I have to recompile the classes, delete the previous friggin' deployment. Recreate my .war, and then redeploy.
All this to recognize one error? Is this why we have ANT now? Was the J2ee directory structure a vehicle for makes/automatic builds? *arhghghg*.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Why recreate the WAR? I just have ANT put the revised classes etc. in the right place. As long as you are not adding a new servlet or changing what appears in web.xml, this works fine.
With respect to your properties problem, why not pass a Properties object?
Sounds to me like you could have avoided a lot of problems by downloading the servlet API from java.sun.com.
Bill
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Thanks for your time Bill...
With respect to your properties problem, why not pass a Properties object?

I am loading Properties in the following manner:

Perhaps I misunderstand your suggestion. I had hoped to be able to retrieve the properties files, w/o hardcoding paths, or passing init parameters to any given class.
However, it appears as if I will have difficulty determining exactly where my properties files are (only a slight hangup indeed). I have seen suggestions for retrieving the source through ServletConfig.getResourceAsStream(..)... which is inappropriate for me.
Is your suggestion to go ahead and pass the file's path in through an init parameter, and rewrite the location of the dependent rootdir via Ant?
Sounds to me like you could have avoided a lot of problems by downloading the servlet API from java.sun.com.

My application is actually quite robust, just not for the current spec.
If my biggest frustration concerns deployment, I'll take my lumps. However, as you hint, this is a lesson well learned (bushwhacking can be fun, if the plants don't eat you).
 
 
subject: directory structure questions...