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

Don't understand reques.attribute :(

Hey!

I am doing a blog in Spark and I am having problems understanding the request.attribute

Craig Dennis does something similar to this:

    before((request, response) -> {
        if (request.cookie("password") != null) {
            request.attribute("password", request.cookie("password"));
        }
    });

Meaning: "if there is a cookie named "password", save the value of that cookie in a request attribute named "password" (Craig does the same but naming the attribute "username" instead). He does that so he can use this in any url, (is that right???).

This is supposed to apply before any method (get/post) is called. I have a couple of questions here:

1) Do GET method have attributes???? I though only POST had...

2) Where in Chrome can I see the attributes of a http reques? (I only see the headers!)

3) Why if I have done that, I lost the attribute when I change the url? I think it is because the attribute is only assigned when a cookie is present, and after that request, the attribute dies, but then...

what is the goal of doing this? just to change "cookie" by "attribute" ???

4) Wouldn't it better to use a session?

A well deep explanation would be veeeeeeery appreciated... I am having problems understanding this area!

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Ricardo!

Attributes in Spark are something special...it is a place that you can use to add values onto each request object. This is typically called middleware in most frameworks.

Here take a look at an example in the SparkJava source code. It stores a value in the attributes and later accesses that in the after.

This doesn't require a session to exist and is only valid for the life of the request through the application.....usually used in before and afters

That help clear things up a bit?

mmmm...

So for instance, if I do

            request.attribute("destinyUrl", "someUrl.com");
            response.redirect("/log-in");

1) I cant get "destinyUrl" in the controller the respons to that redirect, right?

2) The thing is, isn't that attribute stored somehow in the http request?

  • If the answer is no, how it works then?
  • If yes...where could I seed it? (see the request with the attribute that is being sended?) (I am just trying to really understand this! I am really sorry for insisting that much!)

3) The same with request.session().attribute("foo", "fooAtributeContent")

  • where can I see the content? I see the "session cookie", but it is somehow...encrypted.

thanks Craig Dennis !

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Yeah so imagine attributes on the request as a Map...just a key value store, that is only active for the single request.

On a session...attributes is also a a key value store like a Map, however it lives for the lifetime of the session. The session key that is sent down as a cookie does not contain the information, it is just a way to identify the session on the server. The web server uses it to find the proper session object for that "user".

That make more sense?