• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the initial path of the servlet?

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64619
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try servletContext.getRealPath( "/" );
hth,
bear
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64619
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic