[英]Get context path in Java web application
我正在嘗試獲取 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。
我按照本教程創建了那個
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.