getContextPath、getServletPath、getRequestURI的区别

来源:互联网 发布:mathcad软件 编辑:程序博客网 时间:2024/05/21 07:03

假定你的工程名称为projects,你在浏览器中输入请求路径:

http://127.0.0.1:8080/projects/pages/newForm.jsp

则执行下面向行代码后打印出如下结果:
1、 System.out.println(request.getContextPath());
打印结果:/projects
 2、System.out.println(request.getServletPath());
打印结果:/pages/newForm.jsp
 3、 System.out.println(request.getRequestURI());
打印结果:/projects/pages/newForm.jsp
 4、 System.out.println(request.getRealPath("/")); 
 JSP servlet API提供了getRealPath(path)方法,返回给定虚拟路径的真实路径,如果转换错误,则返回null。

打印结果:D:\apache-tomcat-5.5.33s\webapps\rsms\,就是字符串“D:\\apache-tomcat-5.5.33s\\webapps\\rsms\\”


原创粉丝点击