Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Intro to Java Web Development with Spark Diving into Web Development Cookies

purpose of model.put(“username”, req.cookie(“username”)) at 3min 38second of the video?

Instructor said we need to put into our model a value called “username” & will pull this cookie. Why? What is the purpose of the line model.put(“username”, req.cookie(“username”)) ?

Why is it that for post method, we did res.cookie("username",username) but for get method, we don't need req.cookie("username",username) and instead have req.cookie("username",usename) inside the parameter of model.put()?

Thank you!

1 Answer

Hi Ronnie.
Map , represents a mapping between a key and a value. More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value. Once stored in a Map , you can later look up the value using just the key.

In this case we are using "username" as key to locate the cookie.

Why is it that for post method, we did res.cookie("username",username) but for get method, we don't need req.cookie("username",username) and instead have req.cookie("username",username) inside the parameter of model.put()?

We call the "response.cookie("username", username);" on the post method to save the username in the cookies. if you go to the developer tool and comment out the //response.cookie("username", username); in the post method you will see what happen.