web项目中的路径获取

1. Servlet
String path = getServletContext().getRealPath("/");
d:/workspace/project/

2. 
getClass.getClassLoader().getResource("/").getPath();
d:/workspace/project/WEB-INF/classes/

3.
request.getContextPath();
/project (web项目的root context)

String path = request.getContextPath();
String scheme = request.getScheme();
String serverName = request.getServerName();
int port = request.getServerPort();
String basePath = scheme+"://"+serverName+":"+port+path+"/";
http://localhost:8080/project/

你可能感兴趣的:(web项目中的路径获取)