HttpServletRequest
中方法getRequestURI
和getRequestURI
等的区别
项目在tomcat中的目录结构是这样子的: webapps/testApp/项目内容
webapps
为tomcat容器的目录
testApp
即为项目部署的名称
项目的代码片段是这样子的:
@RestController
@RequestMapping("/testController")
public class TestController {
@RequestMapping("/testAPI.htm")
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> map = Maps.newHashMap();
map.put("getContextPath", request.getContextPath());
map.put("getServletPath", request.getServletPath());
map.put("getRequestURL", request.getRequestURL());
map.put("getRequestURI", request.getRequestURI());
map.put("getQueryString", request.getQueryString());
return new ModelAndView(new MappingJackson2JsonView(), map);
}
}
请求的URL为: http://localhost:8080/testApp/testController/testAPI.htm?a=1&b=2
得到的结果是:
{
"getContextPath":"/testApp",
"getServletPath":"/testController/testAPI.htm",
"getRequestURL":"http://localhost:8080/testApp/testController/testAPI.htm",
"getRequestURI":"/testApp/testController/testAPI.htm",
"getQueryString":"a=1&b=2"
}
变量名称 | 变量值 |
---|---|
ContextPath | 项目部署路径,没有名称(如直接部署在在ROOT里)则为空串 |
ServletPath | 请求地址(不包括项目名称) |
RequestURL | 整个URL(不包括查询参数) |
RequestURI | 请求地址(包括项目名称) |
QueryString | ?后面的那一堆查询串 |