B4J Question B4J Server, return page with DBRequest result

cheveguerra

Member
Licensed User
Longtime User
Hi everybody,

I am playing with the ServerHelloWorld and I am trying to return a page generated with the result of a DBRequest (JRDC):

So I have the "/hello" handler in Main:
B4J - Main:
Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 8888
    srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")

    srvr.AddHandler("/hello", "HelloPage", False)

    srvr.Start
    Log("Server started")
    StartMessageLoop
End Sub

And in the HelloPage class:

Class - HelloPage:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    reqManager.Initialize(Me, DBRServer)
    cmd.Initialize
    cmd.Name = "select_something"
    reqManager.ExecuteQuery(cmd , 0, "selectSomething")
    mreq = req
    mresp = resp
    Dim start As Long = DateTime.Now
    resp.ContentType = "text/html"
    resp.Write("<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/>")
    resp.Write("Your ip address is: " & req.RemoteAddress & "<br/>")
    resp.Write("The time here is: " & DateTime.Time(DateTime.Now)).Write("<br/>")
    resp.Write("It took: ").Write(DateTime.Now - start).Write(" ms to create this page.<br/>")
    resp.Write("<a href='/'>Back</a>")
End Sub

Sub JobDone(Job As HttpJob)
    Log("jobDone: " & Job.Tag)
    If Job.Success = False Then
    Else
        If Job.JobName = "DBRequest" Then
            Dim result As DBResult = reqManager.HandleJob(Job)
            If result.Tag = "selectSomething" Then 'query tag
                For Each records() As Object In result.Rows
                    For Each k As String In result.Columns.Keys
                        Log(result.Tag & ": " & k & ": " & records(result.Columns.Get(k)))
                    Next
                Next
            End If
        End If
        Job.Release
    End If
End Sub

But the DBRequest or the JobDone does NOT run, the JobDone event never gets fired, I tried putting the DBRequest in the Initialize sub, but it does not work either... so how would I go about generating the page with the result of the DBRequest?

I don´t want a jquery/javascript generated page, I need a dynamically generated html page.

My final goal is to have a URL that I can feed with a config.properties query name and some parameters, and get back the result of that query, whether it returns json, coma separated values or whatever, I can decide it later!
Best regards

José Alberto Guerra
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 1
Top