B4J Question local server display HTML from string variable

joseluis

Active Member
Licensed User
Longtime User
Hi,

Is there a way to serve a webpage without needing to create a static file for it first?
 

billzhan

Active Member
Licensed User
Longtime User
It's possible. But you still have to provide the html contents.

B4X:
srvr.AddHandler("/user/*", "user",false)


'handler
Sub Handle(req As ServletRequest, resp As ServletResponse)
    resp.ContentType = "text/html"
    resp.CharacterEncoding="UTF-8"  
    Dim sb As StringBuilder
    sb.Initialize
    'sb.Append("html contents")
    sb.Append("<html><body>")
    sb.Append("Hello world")
    sb.Append("</body></html>")  
  
    resp.Write(sb.ToString)
End Sub
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Thank you very much, now I finally understand how this works.

FWIW To run the example one needs to create a class module name "user" and put that Handle sub in there, and srvr.addHandler must be called before srvr.START
 
Upvote 0
Top