B4J Question webapp help

billyrudi

Active Member
Licensed User
Longtime User
Hi i have this code to access to a particular webpage passing a paramiter but
i'm not able to set session attribute to make some activity in the rerirected page.
can you help me? there is another way to do a call to a specific page passing paramiters?
regards Paolo

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim idsolar As Int
    req.GetSession.setAttribute("IdSolar",Null)
    idsolar = req.GetParameter("idsolar")
    If idsolar = 100 Then
        Dim url As String
        url = "/impianto/index.html"
        resp.SendRedirect (url)
    End If
End Sub
 

billzhan

Active Member
Licensed User
Longtime User
B4X:
'Get parameters from URL : 

'in the original handler
url = "http://yourhost.com/impianto/index.html?key1=value1&key2=value2"
 resp.SendRedirect (url)


'impianto/index.html  handler to get parameters

value1_str= req.GetParameter("key1")
value2_str= req.GetParameter("key2")
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
hi tanks for reply but i think this is not a solution...

/impianto/index.html is a websocket page...

B4X:
rivate Sub WebSocket_Connected(WebSocket1 As WebSocket)
    Dim iic As Int
    WS = WebSocket1
' Log(WS.Session.Id)
    WC.Initialize(WS)
    RemoteIP = WS.UpgradeRequest.RemoteAddress
    iic =  WC.GetClientHeight.Value  - 512

so how i can retieve from a websocket page parameters?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
In situations like this, I have found it easiest to retrieve the parameters in JavaScript on the client side and, after the WebSocket connection is made, send the parameters' info to the server with a "b4j_raiseEvent()" call.

In JavaScript, you can get the full address of the page with "window.location.toString()". Then, you can extract the parameters with some straight forward substring() and String.split() work.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
@billyrudi I think in the latest ABMaterial demo I do something like this:

In WebSocket_Connected():

B4X:
' if you start the app as: http://localhost:51042/demo/index.html?login=demo&pwd=demo
        Dim params As Map = ws.UpgradeRequest.ParameterMap
        Dim login(0), pwd(0) As String
        If params.IsInitialized Then
            login = params.GetDefault("login", Array As String(""))
            pwd = params.GetDefault("pwd", Array As String(""))
        End If 
        Log(login(0) & " " & pwd(0))
 
Upvote 0

dar2o3

Active Member
Licensed User
Longtime User
I do not understand the problem, once the session variable set, this is available from any page until the session or the browser is closed.
 
Upvote 0

dar2o3

Active Member
Licensed User
Longtime User
If I want to pass a variable to a page will be to do something with that value, usually you use jquery to treat variable, we have the websocket method ws.eval() to do just that, you do not pass value to the page directly run the script on the server where we have the value of the variable, maybe I'm missing something?


Sorry for the language, google translator
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
Alain, yes you are rigth!
i have this url:
http://172.16.10.101/pilota?idsolar=100
and i have a WebSocket_Connected() sub in which i want know the parameter idsolar
i have try with your solution but i have not parameters in the params variable.
i think that with javascript it is possible but i would like to know if i can with bj4 directly!
 
Upvote 0

dar2o3

Active Member
Licensed User
Longtime User
from the page you want to send the parameter
B4X:
req.GetSession.SetAttribute("name", Main.idsolar)

or
B4X:
ws.Session.SetAttribute("name",main.idsolar)


The page that you want to receive the parameter

B4X:
Dim idsolar as string = ws.Session.GetAttribute("name")


I'm confused?
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Maybe you must add the html page for to to work, e.g in your case something like:

http://172.16.10.101/pilota/index.html?idsolar=100

And I use a ABMSessionCreator:

B4X:
'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
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
  
    Log("In filter: " & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.now))
      
     req.GetSession 'a new session will be created if a session doesn't exist.
    Return True
End Sub

in ABMApplication, start server:

B4X:
srvr.AddFilter("/js/b4j_ws.js", "ABMSessionCreator", False)

Tried it in my demo app, and I got both the login and pwd param.
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If you want to do it all in B4J, you can use the WebSocket's ws.EvalWithResult() method to run arbitrary JavaScript on the client and get a return value. You can use this to get the value of window.location.ToString(). Then you can parse it to extract the keys and values using some SubString() and Regex.Split() work. All this should be done inside the server-side WebSocket_Connected() event sub.

This code probably won't work on its own, but it will set you in the right direction:
B4X:
Dim f As Future = ws.EvalWithResult("return window.location.ToString();", Null)
Dim fullAddr As String = f.Value
Dim parametersString As String = fullAddr.SubString(fullAddr.IndexOf("?"))
Dim KeyValuePairs() As String = Regex.Split("&", parametersString)
Dim paramMap As Map
paramMap.Initialize
For Each kvp As String in KeyValuePairs
    Dim key As String = Regex.Split("=",kvp)(0)
    Dim value As String = Regex.Split("=",kvp)(1)
    paramMap.Put(key, value)
Next
'Now you have paramMap that has all the key-value pairs in the original address navigated to by the user.

This code could be executed inside the WebSocket_Connected() event sub.
 
Upvote 0
Top