B4J Question ServletResponse SendRedirect

micro

Well-Known Member
Licensed User
Longtime User
If I have a server with this connection:
B4X:
srv.AddHandler("/conn1", "Conn_1", False)
srv.AddHandler("/conn2", "Conn_2", False)
srv.AddHandler("/conn3", "Conn_3", False)
srv.AddHandler("/connUi", "Conn_Ui", False)
and i would like in hanlde (for example Conn_1) redirect the resp.OutputStream
to Conn_Ui (and viceversa) i must use resp.SendRedirect?
If it's yes, in what way?
I used resp.SendRedirect("connUi") and after SendObject(map, resp.OutputStream) and raise event in handle of Conn_Ui but not
always and the object received is empty.

Thanks

Example:
<device> to conn1 to connUi to B4jUi and B4j to connUi to conn1 to <device>
 

Attachments

  • example.jpg
    example.jpg
    21.2 KB · Views: 305
Last edited:

billzhan

Active Member
Licensed User
Longtime User
Note that resp.SendRedirect will cause url change if you are using a web browser.

It will be more efficient to combine these handlers if possible:
B4X:
srv.AddHandler("/common", "Conn_common", False)

' action in the parameter
/common?action=conn1
/common?action=conn2
/common?action=conn3
/common?action=connUi

' Conn_common
Dim action As String = req.GetParameter("action")
select action
    case conn1
...


Information can be shared with session

B4X:
' Conn_1
req.GetSession.SetAttribute("object2share",object2share)
resp.SendRedirect("connUi")

'connUi
dim object2share as Object = req.GetSession.GetAttribute("object2share")
req.GetSession.RemoveAttribute("object2share")
...
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
It will be more efficient to combine these handlers if possible:
Thanks billzhan but I do not understand where to place the various segments of code.
I'm not an expert on web server connections.

This code is to be entered in the Sub Handle of "Conn_common"?
' Conn_common
Dim action As String = req.GetParameter("action")
select action
case conn1
...

and always in the same Handle..
B4X:
Select action
    Case conn1
       req.GetSession.SetAttribute("object2share",object2share)
       resp.SendRedirect("connUi")
    Case connUi
        dim object2share as Object = req.GetSession.GetAttribute("object2share")
        req.GetSession.RemoveAttribute("object2share")
    .......

and after creating the various code modules conn1, conn2 .... with their sub Handle, SendObject and ReadObject?

Best Regards
 
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
Thanks billzhan but I do not understand where to place the various segments of code.
I'm not an expert on web server connections.

This code is to be entered in the Sub Handle of "Conn_common"?

Yes, in the Sub Handle of "Conn_common". If you can't figure it out , try to share information with session (see previous post)
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Yes, in the Sub Handle of "Conn_common". If you can't figure it out , try to share information with session (see previous post)
Not work

This code in Conn_common
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim read As Map
    read.Initialize
    read = ReadObject(req.InputStream)
    Dim action As String = req.GetParameter("action")
    Select action
        Case "conn1"
            req.GetSession.SetAttribute("object2share",read)
            resp.SendRedirect("connUi")
            SendObject(read, resp.OutputStream)
            .......
        Case "conn2"
       
        Case "conn3"
       
        Case "conn4"
   
    End Select
End Sub

and this in connUi code
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim object2share As Object = req.GetSession.GetAttribute("object2share")
    req.GetSession.RemoveAttribute("object2share")
    Dim read As Map
    read.Initialize
    read = ReadObject(req.InputStream)
    ...........
End Sub

The event in Sub Handle of connUi module it's never raised.
In Conn_common is allright.
Thanks
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Excuse billzhan but i have a little confusion.
Ok on how to handle different connections in web server, but how i send the object (map) received in non ui to ui application and viceversa?
Of course not ui and ui are on the same Pc.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
If non-ui and ui apps are two seperate jars, ui apps can communicate with non-ui app(server) with http post(s).
it can not be,
the ui application must first receive from the device passing for non ui server and then retransmit the device passing for non ui.
Then i think
device <>http post<>non ui>TcpIp >ui
and non ui<http post<ui
It's right?
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Not sure how you can make it work : non ui>TcpIp >ui
:(
trying to make me understand please.....
Device send a object map to non ui server web run on the desktop, in handle non ui connection it read the object map and.......
Now this object must be send to ui application (always on the same desktop) and is this step that confuses me.
How i can activate jobdone in ui application to read a object posted by non ui app?
Excuse me...thanks.
 
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
ui app can't handle http request (no http server ), but this should work : ui app ->send http request(every 1 second etc.) -> non-ui app return a map object( map is empty when no information to share) -> ui app jobdone

A simply way is to use shared folders for ui and non-ui app.example:
folder1 : non-ui app write the map object here , ui app check this folder with a timer
folder2 : ui app write map object , non-ui app check this folder with a timer
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Checking folders with a timer is an ugly hack and more resource-intensive than it needs to be. Someone wrote the jFileWatcher library precisely for this purpose.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks billzhan and Roycefer, I want to try first con http request
and if it does not work well I try with shared folder and jFileWatcher lib.
A little patience billzhan, you can give me an example of htt request?
Just to be sure, i not expert in http communication.

Best regards.
 
Upvote 0
Top