B4J Question [b4j] ServletResponse.SendRedirect has error !

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I have setting a filter : srvr.AddFilter("*", "MemberFilter",False) to showing login form. In filter, I get error as following :

req.FullRequestURI -->> http://127.0.0.1:8001/param/mon/index.html -->> Redirect to /login/ is OK
req.FullRequestURI -->> http://127.0.0.1:8001/param/day/ws -->> Redirect to /login/ is NG

B4X:
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    Log("filter : " & req.FullRequestURI)
    Dim fp1 As Boolean=req.FullRequestURI.IndexOf("/param")>=0
    Dim fp2 As Boolean=req.FullRequestURI.IndexOf("logoutHelper")>=0
    Dim fp3 As Boolean=req.FullRequestURI.IndexOf("/ws")>=0
    If fp1 And Not(fp2) Then
        Dim registered As Boolean=req.GetSession.GetAttribute2("registered",False)
        Dim username As String=req.GetSession.GetAttribute2("name", "")
        Log("registered:"&b4jlib.IIF(registered,"true","false")&" , name:"&username)
        If registered=True Then
            'check that no more than 30 minutes passed since last activity
            If req.GetSession.LastAccessedTime + DateTime.TicksPerMinute * 30 > DateTime.Now Then
                Return True 'allow request to continue
            End If
            Log("user have login")
        End If
        resp.SendRedirect("/login/")  <<<--- problem in here    
        Return False
        Log("Redirect to /login/")          
    else If fp2 Then
        resp.ContentType = "application/json"
        Dim jg As JSONGenerator
        Dim responeMap As Map
        responeMap.Initialize
        responeMap.Put("success",True)
        responeMap.Put("goto","/monitor/index.html")
        responeMap.Put("errorMessage", "")
        jg.Initialize(responeMap)
        resp.Write(jg.ToString)
        req.GetSession.RemoveAttribute("registered")
        req.GetSession.RemoveAttribute("name")
        Log("=== logout ===")
        Return False
    Else
        Return True
    End If
End Sub
 

billzhan

Active Member
Licensed User
Longtime User
B4X:
http://127.0.0.1:8001/param/day/ws

Is this url for webscoket?
A websocket connection( ws://127.0.0.1:8001/param/day/ws) is sent as a http request( http://127.0.0.1:8001/param/day/ws ). Once the connection is successful, it turn to be the ws connection, and the connection is alive until disconnection.

It's not right to redirect ws connection to a html page ("/login/"). Try(not tested):

B4X:
If fp3=False then
    resp.SendRedirect("/login/")
End if
Return False
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…