How do you use the @SessionAttributes and @CookieValue annotations in Spring MVC?

@SessionAttributes and @CookieValue are Spring Web MVC annotations for carrying state across requests without the controller manually reading and writing HttpSession or Cookie objects. @SessionAttributes stores model attributes in the HTTP session, while @CookieValue reads a value directly from an incoming cookie.

Key Points

Example

A multi-step checkout form could use @SessionAttributes("order") to keep the in-progress Order object available across the shipping, billing, and confirmation steps, while a @CookieValue(value = "theme", defaultValue = "light") parameter reads a previously saved UI preference.

@Controller
@SessionAttributes("order")
public class CheckoutController {

    @GetMapping("/checkout/confirm")
    public String confirm(@CookieValue(value = "theme", defaultValue = "light") String theme,
                           @ModelAttribute("order") Order order,
                           SessionStatus status) {
        status.setComplete();
        return "confirmation";
    }
}

Interview Tip

A concise interview answer is

@SessionAttributes keeps a model attribute alive in the HTTP session across multiple requests, which is handy for multi-step forms, and should be cleared with SessionStatus.setComplete() when the flow ends. @CookieValue binds a method parameter directly to a cookie's value, useful for reading lightweight preferences without touching the raw Cookie API.

Last updated: 11 November 2026

Previous question: How can you let users change the language dynamically in a Spring MVC application?

Next question: What are the security considerations when using @SessionAttributes and @CookieValue annotations?

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions