1. 程式人生 > >SpringMVC常用註解2獲取http請求中的cookie,header和ServletAPI

SpringMVC常用註解2獲取http請求中的cookie,header和ServletAPI

[email protected]註解獲取cookie中的值

    @RequestMapping("/ShowCookie")
    public String showCookieValue(@CookieValue(value="JSESSIONID") String cook) {
        System.out.println("從cookie中取出的cook對應的值為---->" + cook);
        return "welcome";
    }

2.使用@RequestHeader註解獲取請求頭的相關資訊:

@RequestMapping("/showHeader"
) public String showRequestHeader(@RequestHeader("Accept") String accpet) { System.out.println("請求頭accept中的值為" + accpet); return "welcome"; }

3.通過SpringMVC直接獲取ServletAPI:

@RequestMapping("/showByRequest")
    public String getHeaderByRequest(HttpServletRequest request) {
        System.out
.println(request.getHeader("Accept")); return "welcome"; }