aspose file tools*
The moose likes Servlets and the fly likes what is the initial path of the servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "what is the initial path of the servlet? " Watch "what is the initial path of the servlet? " New topic
Author

what is the initial path of the servlet?

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
i wrote a servlet which print a gif picture that is under the "c:\cart\images\",if i specify the absolute path,it works well,but i want to use the relative path,i don't know the initial path(or base path) of the servlet,"%tomcathome%/bin" or "%tomcathome%/webapps"?who can tell me.i think it should be "../../cart/images",but it don't work!why?
thank you!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Try servletContext.getRealPath( "/" );
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
You should never write code that relies on the "current" servlet engine path. I like to pass in a "work" directory as a servlet initialization parameter.
Bill
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks for all of your helps,i think bill's idea is better,how can i set work directory?modify the web.xml?
thanks
Meera Chandrasekaran
Greenhorn

Joined: Jan 08, 2002
Posts: 10
Hi,
Yes, you can pass init-params to a servlet by setting values in web.xml. check out <init-params> tag.
-Meera

Originally posted by zb cong:
thanks for all of your helps,i think bill's idea is better,how can i set work directory?modify the web.xml?
thanks
Daniel Walker
Greenhorn

Joined: Aug 09, 2001
Posts: 19
Hi there. William, I noticed your comment about not writing code which uses the current servlet path - why is this? I usually store this value in a class parameter in the init() method, and it works fine for me. Am I missing something?


<B>daniel.walker</B><BR><I>***bleed in your own light...<BR>...dream of your own life...***</I>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
The "current" directory of the JVM running the servlet engine depends on how it was started and the vendor's conventions. What works for Tomcat may not work for JRun, etc etc.
Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

William, but I believe the original poster was asking about the root directory of the web app (zb, correct me if that's not true -- that's what I was assuming with my answer re getRealPath()), rather than the cwd of the JVM. That path should be safe to use if you need the directory path to the current web app tree. No?
bear
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
yes,i want to know the web root directory,i find that the tomcat use "%tomcathome%/webapps" as its webroot,but the "%tomcathome%/bin" as the reference directory to classpath,but all of above come from my guess,i can't find any official document to verify it,in jchq website,bill have told me that the tomcat start from "%tomcathome%/bin",as follow link:
http://www.jchq.net/discus/messages/50028/50538.html?1011185788
my detailed scenario is :
my web directory is "%tomcathome%/webapps/cart",there is a servlet which access a gif file that is under the "../images" directory,in the servlet, i specify "/cart/images/xx.gif",the servlet works well,this fact seem like to tell me the "%tomcathome%/webapps" is the webroot reference.

but after i move my "cart" directory to "c:/",and modify "server.xml" to map the web to "c:/cart",the web still work,but the problem appear to the picture file,how to specify the path to the picture in the servlet? no doubt, the "/cart/images" don't work,because i believe the web root is "%tomcathome/webapps%",but what confused me is that the "../../cart/images.xx.gif" DON'T work too,why?it seem like that "webapps" is not the web root,it contradict to my belief. if i specify the absolute path as "c:/cart/images/xx.gif",it works.how can i specify the relative path?what's the reference path?"bin" or "webapps" or neither of them?the path problem in tomcat nearly drive me crazy!
so that the better way is to define the reference root by MYSELF as bill said!
thanks
 
wood burning stoves
 
subject: what is the initial path of the servlet?
 
Similar Threads
Stateful Session Bean
Where is logfile located ?
how to start servelets
images in database
Saving images in database from servlet