JSP/Servlet几种常见的容易混淆的路径函数

request.getRequestURI() 返回值类似:

/myContext/requestdemo.jsp

返回值是String

request.getRequestURL() 返回值类似:

http://localhost:8080/myContext/requestdemo.jsp

返回值是StringBuffer

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp     则执行下面向行代码后打印出如下结果:

System.out.println(request.getContextPath());     //打印结果:/news

System.out.println(request.getServletPath());      //打印结果:/main/list.jsp

System.out.println(request.getRequestURI());      //打印结果:/news/main/list.jsp

System.out.println(request.getRealPath("/"));      //打印结果: F:\Tomcat 6.0\webapps\news\test


你可能感兴趣的:(JSP/Servlet几种常见的容易混淆的路径函数)