假定你的web application 項目名稱為news,你在瀏覽器中輸入請求路徑:?http://localhost:8080/news/main/list.jsp?
則執(zhí)行下面向行代碼后打印出如下結(jié)果:?
1训裆、 System.out.println(request.getContextPath()); //可返回站點的根路徑鹰贵。也就是項目的名字?
打印結(jié)果:/news?
2杜漠、System.out.println(request.getServletPath());?
打印結(jié)果:/main/list.jsp?
3、 System.out.println(request.getRequestURI());?
打印結(jié)果:/news/main/list.jsp?
注:
URI=contextPath+servletPath