B4J Question WebServer filtering

Discussion in 'B4J Questions' started by aaronk, May 18, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User


    I am using the web server sample (http://www.b4x.com/android/forum/threads/server-building-web-servers-with-b4j.37172/#content)

    What I am trying to do is load the web page which is working but I want to be able to add things in the webpage such as $test and then get the B4J app to replace $test with something else such as 'hello world' which my B4J app will do.

    In my Main Module I have added the following line of code in the AppStart sub:

    I then added a Class Module called 'HTMLFilter'.

    In my HTMLFilter module I have the following code:

    'Class module
    Sub Class_Globals
    Private mreq As ServletRequest 'ignore
        Private mresp As ServletResponse 'ignore
    Private templates As Map
    End Sub

    Public Sub Initialize
    End Sub

    Sub Handle(req As ServletRequest, resp As ServletResponse)
    Log(req.RequestURI) 'handle the request based on the URL
    If req.RequestURI.EndsWith("html"Then
                HandleMainPage (resp, req.RequestURI)
    End If
            resp.Status = 
            resp.ContentType = 
    Log("Error serving request: " & LastException)
    "<h1><b>Error serving request</b></h1><br>Error: " & LastException)
    End Try

    End Sub

    ' Will replace values on the page and will then send it to the user in the browser
    ' Response = resp
    ' Page = page name including file extension, For Example: index.html
    Sub HandleMainPage (Response As ServletResponsepage As String)
    Dim MainPage As String = GetTemplate(page'load the template from the assets folder - ** All web pages should be in the 'www' folder **
        MainPage = MainPage.Replace("$test$""Hello World")
        Response.ContentType = SetContentType(
    End Sub

    Sub GetTemplate(Name As StringAs String
    If templates.ContainsKey(Name) Then Return templates.Get(Name)
    Dim temp As String = File.ReadString(File.DirApp & "/www/", Name)
        templates.Put(Name, temp)
    Return temp
    End Sub

    Sub SetContentType(FileName As String)
    Dim extension, ContentType As String
    Dim m As Matcher = Regex.Matcher("\.([^\.]*)$", FileName) 'find the file extension
        If m.Find Then
            extension = m.Group(
    Select extension
    Case "html""htm"
                    ContentType = 
    Case "js"
                    ContentType = 
    Case "gif""png"
                    ContentType = 
    "image/" & extension
    Case "jpeg""jpg"
                    ContentType = 
    Case "css""xml"
                    ContentType = 
    "text/" & extension
    Case "ico"
                    ContentType = 
    Case "txt"
                    ContentType = 
    Case "logfile"
                    ContentType = 
    Case Else
                    ContentType = 
    End Select
    Return ContentType
    End If
    End Sub
    My HTML page I have in the www folder is:

    <img src="images/logo.png" width=100 height=100><br>
    <b>Hello this is a test!!!</b><br>
    The problem I have is the page loads and replaces the text on the screen (eg. $test is replaced with 'hello world') which is fine but the image on the screen doesn't display.

    If I then remove the following line from the main module

    It then works but the text is not replaced (eg. $test doesn't get replaced with 'Hello World').

    How can I make it replace the text on the page but also allow the rest of the page to load as well?
  2. billzhan

    billzhan Active Member Licensed User

    our code is similar to the demo of B4A (android) httpserver.

    Code of B4J server hello world demo (the link in your post):

    Sub Handle(req As ServletRequest, resp As ServletResponse)
        mreq = req
        mresp = resp
    Dim start As Long = DateTime.Now
        resp.ContentType = 
    "<img src='images/logo.png'/ width=100 height=100><br/>"'this file will be loaded from the www folder
        resp.Write("<b>Hello world!!!</b><br/>")
    "Your ip address is: " & req.RemoteAddress & "<br/>")  
    "The time here is: " & DateTime.Time(DateTime.Now)).Write("<br/>")
    "It took: ").Write(DateTime.Now - start).Write(" ms to create this page.<br/>")
    "<a href='/'>Back</a>")
    End Sub
    If your are using B4J server:
    1. You used a wildcard for every request to the server (including images/logo.png), the logo was not returned properly.
    2. You should start with B4J server demo.
    Use resp.Write("html contents") to return html contents.
    Dim htmlstring As String ="hello world $test$"
    Erel likes this.
  3. aaronk

    aaronk Well-Known Member Licensed User

    Thanks for your reply..

    I ended up putting a folder called 'pages' and another folder called 'img' in the 'www' folder. All my HTML files are stored in the 'pages'.
    I then put all my images in the 'img' folder.

    Then in my HTML page I am pointing the image to the img folder.

    I then changed:

    Now it works just like what I wanted. Well kind of like what I wanted. I wanted all the webpages in the root dir (www folder) but for now I will need to put all my pages in the 'pages' folder.
  4. Erel

    Erel Administrator Staff Member Licensed User

    All the "static" files should be under the www folder.
  5. aaronk

    aaronk Well-Known Member Licensed User

    Yeah that's where my files are kept.

    I have been playing around with it today and I now have it working the way I want it to.
    I am still learning and playing around with it, but so far everything seems to be working fine.