B4J Question Handler Class & Globals

tchart

Well-Known Member
Licensed User
Longtime User
I'd just like to confirm something.

I'd like to set a global variable in a handler class that I can access in other subs in the handler (to avoid passing the variable to every single sub).

This variable (a time zone offset) is added to the session when the user signs in using OAuth. Thus the value could be different between users. Although I have client side code to calculate timezone offsets I do occasionally require the offset on the server side.

If I define a class global that is set in the Handle sub, is the global defined for just that handler call or the handler instance?

My testing seems to indicate that the variable tzo set for each handler call (even if multi-threaded is on).

Is that a safe assumption?

B4X:
'Handler class
Sub Class_Globals
    Dim tzo As int = 0
End Sub

Public Sub Initialize
    Log(tzo) 'Prints zero
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
   Log(tzo) 'Prints zero
    tzo = req.GetSession.GetAttribute2("tzo", 0)    
    DoSomething
End Sub

Sub DoSomething()
   Log(tzo) 'Prints session value
End Sub
 
Top