1. 程式人生 > >getContextPath、getServletPath、getRequestURI的區別

getContextPath、getServletPath、getRequestURI的區別

request 執行 result web print 名稱 ati 輸入 pri

假定你的web application名稱為example,你在瀏覽器中輸入請求路徑:

https://localhost:8080/example/main/edit.jsp

則執行下面代碼後打印結果如下:

System.out.println(request.getContextPath());
//result:/example

System.out.println(request.getServletPath());
//result:/main/edit.jsp
System.out.println(request.getRequestURI()); //result:/example/main/edit.jsp
System.out.println(request.getRealPath("/")); //result:D:\Tomcat 7\webapps\example\test

getContextPath、getServletPath、getRequestURI的區別