AndreaNobili
AndreaNobili

Reputation: 42957

How exactly Spring MVC handle cookie?

I am studying how Spring handle cookie on a tutorial and I have some doubts.

In this example there is this CookieControllerExample that perform some cookie operation when are performed.

@Controller
public class CookieControllerExample {

    @RequestMapping(value = "/readcookie", method=RequestMethod.GET)
    public ModelAndView readCookie(@CookieValue(value = "URL") String URL, HttpServletRequest request,
        HttpServletResponse response) {     
        System.out.println("CookieControllerExample readcookie is called");
        return new ModelAndView("/cookie/cookieView", "cookieValue", URL);

    }
    @RequestMapping(value = "/writecookie", method=RequestMethod.GET)
    public String writeCookie(HttpServletRequest request,
        HttpServletResponse response) {     

        System.out.println("CookieControllerExample writeCookie is called");
        Cookie cookie = new Cookie("URL", request.getRequestURL().toString());
        response.addCookie(cookie);
        return "/cookie/cookieView";

    }


    @RequestMapping(value = "/readAllCookies", method=RequestMethod.GET)
    public ModelAndView readAllCookies(HttpServletRequest request) {        
        System.out.println("CookieControllerExample readAllCookies is called");

        Cookie[] cookies = request.getCookies();
        System.out.println("All Cookies in your browsers");
        String cookiesStr = "";
        for(Cookie cookie : cookies){
            System.out.println(cookie.getName() + " : " + cookie.getValue());
            cookiesStr += cookie.getName() + " : " + cookie.getValue() + "<br/>";
        }


        return new ModelAndView("/cookie/cookieView", "cookieValue", cookiesStr);

    }

}

From what I have understand the first method (readcookie()) read the content of a coockie named URL stored inside my computer.

The second method (writecookie()) create a cookie named URL and store it on my computer.

And the third method read the contet of all the cookies stored on my computer.

I have 2 doubts:

1) I know that cookies are textual file. Where exactly are stored?

2) Why the **writeCookie() method, after create a new cookie, add it to the response? How can I examinate the cookie stored on my system?

response.addCookie(cookie);

I think that it could depend by the fact that the response come back to the user browser and it retrieve the cookie from this response and create a textual file somewhere on my system. Is it true or am I missing something?

Upvotes: 0

Views: 654

Answers (1)

Ralph
Ralph

Reputation: 120771

You asked:

1) I know that cookies are textual file. Where exactly are stored?

The cookies are stored by the clients browser, somewhere at the clients machine. - The exact location depends on the browser

2) Why the **writeCookie() method, after create a new cookie, add it to the response? How can I examinate the cookie stored on my system?

As I told in answer for question 1), the cookie is stored at client side. So it's values need to be send to the client (in the header of the http-response). And that is the reason why the cookie (object) is added to the http response.


I strongly recommend you to read the wikipedia article about Http Cookies. And do not get confused by mixing cookies and sessions (sessions are often implement with a session-tracking-cookie, but its data resist on the server side.)

Upvotes: 1

Related Questions