从request中获取项目的一些路径

来源:互联网 发布:矩阵防御1 编辑:程序博客网 时间:2024/06/07 19:55

无论是jsp还是项目进程要获取一些项目部署的实际路径或者项目的访问路径的,那么如何获取呢?

在web后端:

例如访问地址是:http://localhost:8080/test

request.getContextPath(); 获取到工程名称:    /test

request.getServletContext().getRealPath("/"); 获取到实际路径例如:E:\java\tomcat7\apache-tomcat-7.0.55\wtpwebapps\test\

request.getRequestURI(); 获取到包含工程名的当前页面全路径:/test/action.do


jsp中获取路径:

String path=application.getRealPath(request.getRequestURI());  

String realPath1 = "http://" + request.getServerName() + ":" + request.getServerPort() +request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);


String projectPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();




0 0
原创粉丝点击