急:请问如何在程序中获得tomcat中docBase的路径(虚拟目录)?

horizon2003 2005-09-03 11:57:43
我希望获得系统的当前工作路径,可是用System.getProperty("user.dir")发现总是指向c:\windows\system32目录。我用JBuilder开发的时候没有这个问题,user.dir总是指向工程的根目录;而用eclipse+tomcat的时候则更离谱,user.dir指向eclipse的安装目录。
不得已,我想直接读取docBase的设置,请问如何读取?万分感谢!
不知是否我的解决思路有问题,还请大家指教!
...全文
982 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
horizon2003 2006-03-03
  • 打赏
  • 举报
回复
谢谢!
whodsow 2005-09-05
  • 打赏
  • 举报
回复
new File(".");可以获取当前路径。
ServletContext.getRealPath(java.lang.String path)提供了一个获取web文件路径在本地文件系统的绝对路径.

getRealPath
public java.lang.String getRealPath(java.lang.String path)Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).


Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed


比如JSP中:this.getServletConfig().getServletContext().getRealPath("/index.jsp")返回的可能是:D:\Tomcat 5.0\webapps\ROOT\index.jsp

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧