我正在尝试获取 Java Web 应用程序中的项目上下文路径。我的项目位于 D:\GED\WSysGED
目录中,我想获取该路径。在我的研究中,我发现了两种方法:第一种使用 System.getProperty
,如下所示:
String path= System.getProperty("user.dir");
System.out.println(path);
但该代码返回 D:\eclipse-jee-luna-R-win32\eclipse
,Eclipse 可执行文件所在的位置。
第二种方法是使用 servlet。
我在这之后创建了那个 tutorial
public class ContextPathServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletContext servletContext = getServletContext();
String contextPath = servletContext.getRealPath("/");
PrintWriter out = response.getWriter();
out.println("<br/>File system context path (in TestServlet): " + contextPath);
}
}
但它正在显示 C:\Users\Juan\SysGED\.metadata\.plugins\org.eclipse.wst.server.core\tmp6\wtpwebapps\WSysGED
获取项目路径的正确方法是什么?
最佳答案
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String contextPath = request.getContextPath();
System.out.println(contextpath);
}
关于java - 在 Java Web 应用程序中获取上下文路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26018657/