问题:
使用getServletContext().getRealPath("/")获取地址得到\eclipseworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps,而不是对应项目目录
分析:
该目录为eclipse中发布工程的临时目录。
解决:
Click on > Servers Tab. Right Click on Servlet Container (Tomcat 7 in your case) and Open (F3 is shortcut for eclipse).
Specify the server path (i.e. catalina.base) and deploy path.
You can specify the deploy location here.
如果,修改项为灰色disable。则先进行以下步骤:
- Right click on my tomcat server in "Servers" view, select "Properties…"
- In the "General" panel, click on the "Switch Location" button
- The "Location: [workspace metadata]" bit should have been replaced by something else.
- Open (or close and reopen) the Overview screen for the server.
Right click on the server, then Add/Remove, then remove any projects that are in the Configured panel. Then right click on the server and choose "Clean..." from the context menu. Then the Server Locations option will be enabled.
附:
getRealPath
String getRealPath(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
eg. getServletContext().getRealPath("/WebContent/template");