Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.