Java Web项目开发中常见路径获取方法

时间:2024-01-03 23:28:32

项目绝对路径

String serverPath = request.getSession().getServletContext().getRealPath("/");

E:\JAVA\server\tomcat\apache-tomcat-7.0.77\webapps\lawreg\

项目根目录名称
String contextPath = request.getContextPath();

/lawreg

服务器地址
String basePath = request.getScheme()+"://"+request.getServerName()+":"+
                request.getServerPort();


http://127.0.0.1:8080/

项目服务器地址路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+
                request.getServerPort()+contextPath+"/";

http://127.0.0.1:8080/lawreg/