Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:32:30+00:00 2026-06-09T11:32:30+00:00

I am trying to add a simple string to a play session, but it

  • 0

I am trying to add a simple string to a play session, but it is just not working. For testing I wrote a little controller function that first adds a string to the session and afterwards, it prints all elements the session contains, but it’s always empty.

def foo = Action { request =>
  request.session + ("token", "foobar")
  request.session.data.foreach{ keyVal => println("\tkey value pair: " + keyVal._1 + ", " + keyVal._2)}
  Ok("just a test")
}

What did I do wrong? Do I need to activate something via application.conf or something else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:32:31+00:00Added an answer on June 9, 2026 at 11:32 am

    Actually, a session is an immutable structure as well.

    It’s true that the session object that is stored in the request one has a + method, this latter is respecting the immutability paradigm by returning a new instance of Session. Keeping the request.session unchanged.

    Thinking a step further, we can assert on the fact that an updated session has only a sense when reused in another request-response transaction…

    So the way to update a session is to update it while building the response (Result in Play), like that:

    Ok("just a test").withSession(request.session + ("token", "foobar"))
    

    This will add the new session field in your cookie, that will be available in the next transaction (i.e. request-response).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a series of numbers to a string, but not
I am trying to add an attribute using javax.xml.bind.annotation to a simple type (String
This may be a simple answer, but I'm trying to add a dom-created element
I'm trying to add a generic simple function that shows/hides form field's associated hint
I have a simple class called Applicant. I'm trying to add a template controller
I'm trying just to get a simple ListView to update when clicking an add
I am trying to add a simple list of strings: string[] misc = {a,
I'm trying to add simple login page to ASP.NET MVC app. I actually use
I am trying to add a simple log in with Facebook button to my
I am trying to add a simple form to allow my users to edit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.