B4J Question [ABMaterial] default page without specification

ToolboxZX

Member
Licensed User
Longtime User
This may be either an ABMaterial or Jetty question.

Is there a way to specify, or is there a "default" page that ABMaterial / Jetty looks for if a user just specifies the URL and not a specific page?

Example:
You ABM webapp is called "hello" and you can start it with:

http://localhost:51042/hello
or
http://someurl/hello

is there a way to just use: http://someurl

I know I could set up a catch all error handler to just redirect it back to the correct place, but wondered if this was possible without the catch all or a dns redirect. Thank you.
 

ToolboxZX

Member
Licensed User
Longtime User
For those just wanting to do this using the error catch method add the following to your ABMaterial app:
In the Main module add a map global such as this in the Process_Globals sub:
B4X:
    'error handler stuff
    Dim err As Map

Also in the Main module, in the AppStart SUB, add the following code before the call to start the server:

B4X:
    err.Initialize
    err.Put(404, "/hello") 'page not found catch   "hello" is the name of your ABM app whatever it is, as defined in: ABMApplication, Sub Initialize, ABMShared.AppName =
    err.Put("org.eclipse.jetty.server.error_page.global", "/hello")  'all errors catch    "hello" should be replaced with the name of your app.
       
    srvr.SetCustomErrorPages(err)


That's it. Now you can use http://localhost:51042 or http://someurl and your webapp will start.

This information was derived from Erel's post about the error handling Jetty has now. I know you probably don't need both put lines of code above but thought I would keep them for reference to all as it doesn't impact function.

If there is another way to do this without the error catch described feel free to still post to answer the original question.
 
Upvote 0

mindful

Active Member
Licensed User
You specified [ABMaterial] in your question so I am going to answer to it related to ABMaterial framework. index.html is the default page served by jetty. So if you have your app named hello and in you ABMApplication the PageHTMLName = "index.html" then when you point your browser to http://localhost:51042/hello it will display the ABMApplication page.

Also if you wish to redirect your users if they type only http://localhost:51042 then you need to setup a Filter which needs to be added to the server in which you redirect your users to /hello.

You create a Server Filter Class named RootRedirectFilter
B4X:
'Server Filter: RootRedirectFilter
Sub Class_Globals
  
End Sub

Public Sub Initialize
  
End Sub

'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    If ABMShared.AppVersion.Length > 0 Then
        resp.SendRedirect("/hello/?" & ABMShared.AppVersion.Version)
    Else
        resp.SendRedirect("/hello/")
    End If
    Return False
End Sub
And before the srvr.Start from ABMApplication in StatServer/StartServerHTTP2 you need to add:
B4X:
' register the RootRedirect Filter on the server - it redirects users that do not add the app name in the address bar of the browser
srvr.AddFilter("/", "RootRedirectFilter", False)

Note that 51042 is the port you setup for jetty to listen on you can change that to 80 so your users can access your url without specifying the port number.
 
Upvote 0
Top