getServletContext().getRealPath("/"):

来源:互联网 发布:java转码utf-8转gbk 编辑:程序博客网 时间:2024/05/22 02:05

http://zhidao.baidu.com/question/408005631.html

getrealpath()

http://stackoverflow.com/questions/11398513/eclipse-problems-with-getrealpath

disable server location

http://stackoverflow.com/questions/1012378/eclipse-server-locations-section-disabled-and-need-to-change-to-use-tomcat-ins/1012459#1012459


问题:

使用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。则先进行以下步骤:

  1. Right click on my tomcat server in "Servers" view, select "Properties…"
  2. In the "General" panel, click on the "Switch Location" button
  3. The "Location: [workspace metadata]" bit should have been replaced by something else.
  4. 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 returnsnull 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");


0 0