JSP 如何获取request对象中的路径信息呢?
下文笔者讲述使用示例的方式获取request对象中的绝对路径,虚拟路径及其它信息的方法分享,如下所示:
实现思路:
例1:工程名“maomao”
getContextPath():获取工程名
结果:/maomao
getServletPath() 得到当前页面所在目录下全名称
/maomao/jsp/test.jsp
getRequestURL() 得到IE地址栏地址
结果:http://localhost:8080/maomao/test
getServletPath() 得到相对地址
结果:/maomao/test
例2:jsp请求地址:http://127.0.0.1:8080/projectName/test/testSearch.jsp,各种路径获取如下:
request.getServletPath():/test/testSearch.jsp
request.getContextPath():/projectName
request.getRequestURL():http://127.0.0.1:8080/projectName/test/testSearch.jsp
request.getRemoteAddr():127.0.0.1
request.getServletContext():org.apache.catalina.core.ApplicationContextFacade@6db1123
request.getServerPort():8080
request.getScheme():http
request.getServerName():127.0.0.1
request.getProtocol():HTTP/1.1
request.getPathInfo():null
request.getQueryString():null
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


