# Grabbing variables stored in the session

**URL:** <https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224>\
**Category:** Gatling (Open-Source)\
**Created:** [January 11, 2013, 12:03pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224 "2013-01-11T12:03:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bayo\_Erinle](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Bayo\_Erinle](https://community.gatling.io/u/Bayo_Erinle)\
**Post date:** [January 11, 2013, 12:03pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/1 "2013-01-11T12:03:02Z")

</div>

Hi -  
From the docs ([https://github.com/excilys/gatling/wiki/Session](https://github.com/excilys/gatling/wiki/Session)), I understand I can grab the variables stored in a session like so…

${mySessionVariable}

– or –

.exec(session =\> {  
val mySessionVariable = session.getAttribute(“mySessionVariable”)  
session  
})

For clarity, is the first expression meant to be wrapped in an .exec(session =\> { } ) to work? It’s not working for me.

More importantly, when I dump the session

.exec(session =\> {  
println(session)  
session  
})

I don’t see values in there that I know are there. Once logged in, some variables are stored in the session. I’d expect those to be printed when I print the session info.  
All I see when I print

a map containing gatling.http.referrer, gatling.http.cookies, and gatling.core.timeShift

none of which contain the actual variables stored in the session.

Please advice.

Thanks.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 11, 2013, 12:10pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/2 "2013-01-11T12:10:15Z")

</div>

> Hi -  
> From the docs ([GitHub - gatling/gatling: Modern Load Testing as Code](https://github.com/excilys/gatling/wiki/Session)), I  
> understand I can grab the variables stored in a session like so...
> 
> ${mySessionVariable}
> 
> -- or --
> 
> .exec(session =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val mySessionVariable = session.getAttribute("mySessionVariable")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session  
> &nbsp;&nbsp;&nbsp;&nbsp;})
> 
> For clarity, is the first expression meant to be wrapped in an  
> .exec(session =\> { } ) to work? It's not working for me.

No. It's not intended to be used everywhere, but only where the doc says it  
can, as it's parsed and transformed into the second form.

> More importantly, when I dump the session
> 
> .exec(session =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;println(session)  
> &nbsp;&nbsp;&nbsp;&nbsp;session  
> &nbsp;&nbsp;&nbsp;})
> 
> I don't see values in there that I know are there.

So that means that you think they are here, but they actually don't 🙂

> Once logged in, some variables are stored in the session. I'd expect those  
> to be printed when I print the session info.  
> All I see when I print
> 
> a map containing gatling.http.referrer, gatling.http.cookies, and  
> gatling.core.timeShift
> 
> none of which contain the actual variables stored in the session.
> 
> Please advice.
> 
> Thanks.

I'm willing to help, but you have to show more code. Can you post a gist?

---

<div class="post-metadata">

**Author:** ![Bayo\_Erinle](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Bayo\_Erinle](https://community.gatling.io/u/Bayo_Erinle)\
**Post date:** [January 11, 2013, 5:28pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/3 "2013-01-11T17:28:46Z")

</div>

Hi Stephane - Thanks again for your quick response. See my responses and the info you asked for

> > _No. It’s not intended to be used everywhere, but only where the doc says it can, as it’s parsed and transformed into the second form._  
> > I am still not clear on this. If i expect values in the session, then when I am executing a request, should I be able to grab that value from the session to use as parameters?

i.e.

exec(http(“request\_1”)  
.post(“/post-some-data”)  
.headers(headers\_7)  
**.param(“age”, “${ageFromSession}”)**  
**.param(“height”, “${heightFromSession}”)**

provided I have stored age and height in the session before hitting this request under those keys respectively.

> > _So that means that you think they are here, but they actually don’t 🙂_  
> > Yes, they should be there 🙂

I recorded a very simple script, logging into an application using Spring Security which stores it’s object in session once the user has been authenticated. After logging in, I dump the contents of the session on a page. I am able to verify the values are there. For good measure, I also put a value (current time stamp of the request), into the session. Then I logout.

So there steps of the scenario I recorded are:

1. Request a secure page
2. Redirects to login page
3. User logs in
4. Sees the secure page (which just dumps the session info)
5. Logout

Here is the scenario:  
[https://gist.github.com/4512357](https://gist.github.com/4512357)

Expected Output:  
[https://gist.github.com/4512470](https://gist.github.com/4512470)

Here is what Gatling console reports (when I dump the session after logging in. NOTE: there is no **SPRING\_SECURITY\_CONTEXT** or the **time\_now** i manually added)  
[https://gist.github.com/4512367](https://gist.github.com/4512367)

Please let me know if you need additional info.

Thanks.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 11, 2013, 7:21pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/4 "2013-01-11T19:21:37Z")

</div>

Huh? I think there’s a big misunderstanding here: the Gatling user’s session has nothing to do with the java HttpSession!  
The first one lives on the client side in Gatling, while the second one lives on the server side in your webapp.

In order to inject data into Gatling’s user session, you either have to use a Feeder, use a check with saveAs, or hack the session with a function.  
See respective Feeder and Check documentation.

Cheers,

Stéphane

---

<div class="post-metadata">

**Author:** ![Bayo\_Erinle](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Bayo\_Erinle](https://community.gatling.io/u/Bayo_Erinle)\
**Post date:** [January 11, 2013, 7:41pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/5 "2013-01-11T19:41:25Z")

</div>

Oh I see. That was really a misunderstanding on my part. For whatever reason, I thought that was referring to the http session on the server.

Using feeders, is it at all possible to get hold of the http session on the server? I briefly taken a look, and I don’t think that can be done, but I still wanted to ask 🙂

Also, does using jdbc feeders have an negetive impact on performance, compared to the other feeders? i.e. is it preferable to feed in a file if you can than trying to hit the database?

Cheers

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 11, 2013, 7:44pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/6 "2013-01-11T19:44:48Z")

</div>

> Using feeders, is it at all possible to get hold of the http session on  
> the server? I briefly taken a look, and I don't think that can be done, but  
> I still wanted to ask 🙂

Nope, no way

> Also, does using jdbc feeders have an negetive impact on performance,  
> compared to the other feeders? i.e. is it preferable to feed in a file if  
> you can than trying to hit the database?

Not really as the whole feeder content is pull from the database on start  
up, not record per record during the run.

---

<div class="post-metadata">

**Author:** ![Bayo\_Erinle](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Bayo\_Erinle](https://community.gatling.io/u/Bayo_Erinle)\
**Post date:** [January 11, 2013, 7:49pm UTC](https://community.gatling.io/t/grabbing-variables-stored-in-the-session/224/7 "2013-01-11T19:49:32Z")

</div>

Ok, makes sense. Thanks for clearing that up.

Now on to those feeders!
