Java equivalent of C#'s HttpContext.Current.Items.set/get

1.4k views Asked by At

I am attempting to translate the functionality of a C# API, and I have arrived at some code that I cannot find a confident solution for.

It concerns storing data in the correct web-contexted scope. For now I could have fixed the issue simply by making a HashMap, but as I do not know the extended need for the data stored at this point, I want to go the length in trying to do it right. In my attempt to find a java solution that does the same, I have come across a scope I didn't know about before, namely the Flash-scope. It seems like it is the same as HttpContext.Current.Items, but I would definitely appreciate a second opinion on this. The flashScope is something i have discovered in the playframework.

Also, HttpContext.Current.Items seems to be accessed in a static way, while java usually makes object instances of a scope class, like HttpSession.

Can I store objects in scoped dictionaries staticly, like in the C#-code below? Is the Flash scope equivalent to HttpContext.Current.Items? Can I access the Flash Scope without the Play Framework?

As always, I also very much would appreciate to know if I have made any wrong assumptions, or other misunderstandings.

Thank you :)

C# method

    public void setItem(String itemName, Object item) {

        HttpContext.Current.Items.set(itemName, item);      
    }
2

There are 2 answers

4
ferchoman09 On

HttpServletRequest has the setattribute() method.

0
jumps4fun On

This page provides a lot of useful information on the subject: http://odetocode.com/articles/111.aspx

Among the most important lines are:

An HttpContext object will encapsulate specific details of a single HTTP request. Properties of this class include the Request object, the Response object, the Session object, and an AllErrors property which keeps an array of Exception objects accrued during the current request.

...

Current is a static property which will return the HttpContext object for the current HTTP request. You can use Current from any object in the logical thread of execution for the request

The data in Items, though accessed in a static way, are scoped to a single request. In other words, it would be logical to assume that using javas HttpServletRequest, and its methods getAttribute() and setAttribute(), is sufficient for however the translated framework is intended to be used in applications.