java web中获取各种路径

一、获取项目路径:

1)

String path = request.getContextPath() :// /TestMyEclipse
String basePath = request.getScheme()+"://" +request.getServerName()+ ":" +request.getServerPort()+ path+"/";
http://127.0.0.1:8080/TestMyEclipse/

2)

路径是:<c:out value="${pageContext.request.contextPath }"></c:out>

打印结果是:/springMVC  是项目根路径

 

二、获取url地址:

获取页面的url地址:
String url = request.getScheme()+"://" +request.getServerName()+ ":" +request.getServerPort();
url += request.getContextPath();
url += request.getServletPath() + "?" + request.getQueryString();
打印出来是:
http://localhost:8080/BBS/articleFlat.jsp?pageNo=2

还有两个:
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL()):
分别打印出来是:
/BBS/articleFlat.jsp
http://localhost:8080/BBS/articleFlat.jsp

所以上面的获取url地址,包括参数可以这么写了:
String url = request.getRequestURL() + "?" + request.getQueryString();

----------------------------------------------------------------------------------------------------------------------------------------------------

 

posted on 2017-01-21 23:36  有点懒惰的大青年  阅读(265)  评论(0编辑  收藏  举报