request.getRequestURL()和request.getRequestURI()
先在拦截器里执行方法看一看结果
System.out.println("getRequestURI:"+request.getRequestURI()); System.out.println("getRequestURL:"+request.getRequestURL()); System.out.println("getContextPath:"+request.getContextPath()); System.out.println("getServletPath:"+request.getServletPath());
结果:
getRequestURI:/user/index getRequestURL:http://localhost/user/index getContextPath: getServletPath:/user/index
总结:
-
request.getRequestURI() 返回除去host(域名或者ip)部分的路径
-
request.getRequestURL() 返回全路径
-
request.getContextPath() 返回工程名部分,如果工程映射为/,此处返回则为空
-
request.getServletPath() 返回除去host和工程名部分的路径