转载自:http://blog.163.com/jackswu@yeah/blog/static/14062912320110111000234/
如果请求的URL为:http://localhost:8080/webapp/login
那么分别调用 request.getContextPath( ) 、 request.getServletPath( ) 、 request.getURI( ) 、 request.getURL( ) 分别返回什么路径?
这几个方法都是在HttpServletRequest接口中定义的。
1.request.getURI( )或者request.getRequestURI()返回 HTTP请求行中请求URI的部分 。上例中该方法将返回 /webapp/login。
2.request.getContextPath( )返回 web应用程序的路径 ,上例中该方法将返回 /webapp。
3.request.getServletPath( ) 返回 Servlet的路径 。上例中该方法将返回 /login。
4.request.getURL( )或者request.getRequestURL()返回 请求的URL ,上例中即为 http://localhost:8080/webapp/login。