B4J Question Combine static html text with server handler response

gezueb

Active Member
Licensed User
Longtime User
How do I combine a html page with some title and styles etc. with contents sent from a server? Can I call a html page from the handler or should I do it the other way round e.g. invoke a handler response from the page?
 

Quandalle

Member
Licensed User
Example below

In the main
B4X:
Sub Process_Globals
    Public srvr As Server
    ...
End Sub


Sub AppStart (Args() As String)
    ...
    srvr.AddHandler("/pages/*", "GetPage", False)
    ...
    srvr.Start
    StartMessageLoop
End Sub

The handler GetPage
B4X:
'Handler class
Sub Class_Globals
End Sub

Public Sub Initialize 
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)

    resp.ContentType = "text/html"

    Try
        Dim pagename As String = req.RequestURI.SubString(1)
        dim page  as string = File.ReadString(Main.srvr.StaticFilesFolder,pagename)
      
        ' process file loaded in string page
        page = page.replace("$$marker1$$", "<style> h1   {color: blue;} </style>")  ' example  replacing a marker in html by a inline style
      
        ' send result
        resp.Write(page)
    Catch
        resp.Write("error : " & req.FullRequestURI)
        Log("ERROR : invalid page " & req.FullRequestURI)
    End Try

End Sub
 
Last edited:
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I have some questions about this example. Forgive me the questions, its my first server program.
The call servr.Addhandler (line 9) seems to pass a pointer to all files located in the subdirectory /pages/ ?
The handler retrieves this information on line 13 in Getpage?
On line 14, the contents of this file is read into the string variable page. What I do not understand is "Main.srvr.StaticFilesFolder"
Where would that be?
Thanks, Georg
 
Upvote 0
Top