JSP路径问题
完整获取路径
如:http://localhost:8080/test/test.jsp
"http://" + request.getServerName() + ":" + request.getServerPort() +
request.getContextPath()+request.getServletPath().substring
(0,request.getServletPath().lastIndexOf("/")+1);
返回: http://localhost:8080/test/
getContextPath()
返回:/test
getServletPath()
返回:/test.jsp
getRealPath("")
返回:物理路径,例如webapp目录放在D盘,返回d:/webapp/test
getHeader("host")
返回:localhost:8080
getRequestURL()
返回:http://localhost:8080/test/test.jsp,返回类型未StringBuffer
getRequestURI()
返回:/test/test.jsp
访问服务器路径时最好使用相对路径
"/"
对应http://localhost:8080/test
本文详细介绍了JSP中各种路径获取方法的区别与应用,包括getContextPath()、getServletPath()等函数的具体返回值,并给出了实际场景中的例子。
277

被折叠的 条评论
为什么被折叠?



