Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext.getRealPath()

 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
May I know whether the following is true?
ServletContext.getRealPath(anyPath) is equivalent to ServletContext.getRealPath("/") + anyPath?
Thanks!
 
sridhar satuloori
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getRealPath returns the "real path" for a given virtual path. Real path is system dependent
Ro answer your question. mostly No. ie, if "/" and all ur "anyPath(s)" maps to the same physical component you will get the same real Path. but in usage we map different paths to difeerent components on the system, in this type you never gets the same value for "/" and anypath.
Sridhar

Originally posted by Meng Tan:
Hi,
May I know whether the following is true?
ServletContext.getRealPath(anyPath) is equivalent to ServletContext.getRealPath("/") + anyPath?
Thanks!

 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sridhar,
Why ServletContext.getRealPath("/servlet/MyServlet") does not give me the actual file system path i.e. "c:\tomcat\webapps\MyContext\WEB-INF\classes\MyServlet.class" instead it gives "c:\tomcat\webapps\MyContext\servlet\MyServlet"?
Thanks.
 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m using Tomcat as my web server and I also have a servlet called MyServlet in "c:\tomcat\webapps\MyContext\WEB-INF\classes.
But when i call getServletContext().getRealPath("/servlet/MyServlet"), it does not return me the actual file system path which is "c:\tomcat\webapps\MyContext\WEB-INF\classes\MyServlet.class". Instead it gives me "c:\tomcat\webapps\MyContext\servlet\MyServlet".
Can someone help me?
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic