B4J Question [Jserver ] How to make a session persistent and avoid the user relogging with each refresh?

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Hello guys, I'm using websocket to manage the sessions on my page...
However, every F5, the session is closed and I need to log in again... how do I solve this??
 

jahswant

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You need to implement this:
If you then want sessions survive server reboots, you can also implement post #2
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
How do you create the session and read it?

From B4J Server template:

GuessMyNumber:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Try
        If req.GetSession.HasAttribute("myNumber") = False Then
            req.GetSession.SetAttribute("myNumber", Rnd(0, 101))
        End If
        Dim myNumber As Int = req.GetSession.GetAttribute("myNumber")
        Dim n As String = req.GetParameter("number")
        If IsNumber(n) = False Then
            resp.Write("Please enter a valid number.")
        Else
            If n > myNumber Then
                resp.Write("My number is smaller.")
            Else If n < myNumber Then
                resp.Write("My number is larger.")
            Else
                resp.Write("Well done!!!")
            End If
        End If
    Catch
        resp.SendError(500, "error....")
    End Try
End Sub
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
how can i use it ?
have some example ?
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Do you mean F5 on B4J (Debug), not F5 on web browser (Refresh)?

You may try using Cookie or LocalStorage.
i mean : F5 on web browser (Refresh), Even using session, whenever I refresh the page, the session is closed.and I have to log in again... I wanted to leave the session with an expiration time
cookies works on websocket handler ?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
i mean : F5 on web browser (Refresh),
Have you checked out the link in post #3? Yes, WebSockets can work with sessions (which are tracked via a session cookie)
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Have you checked out the link in post #3? Yes, WebSockets can work with sessions (which are tracked via a session cookie)

I'm using this to create sessions, and I still have the problem mentioned above.
B4X:
'main server class'
srvr.AddFilter("/b4j_ws.js", "SessionCreator", False)


'Session Filter class
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    Log("sessão do crl")
    req.GetSession
    Return True
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I'm using this to create sessions, and I still have the problem mentioned above.
You followed the instructions of the link by including the additional JavaScript file (mentioned and included in the link) and you did the code changes as shown in the link and you are still having issues?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…