B4J Code Snippet Additional jServer (web server) Setting Snippets

Just thought it would be good to keep additional jServer/Jetty/Web Server settings in one place.

I'll go first;

Set Max Post/Form Size (setMaxFormContentSize) (link)

B4X:
Dim jo As JavaObject = webserver  
jo.GetFieldJO("context").RunMethod("setMaxFormContentSize", Array(10000000)) 'Size is in bytes, this example is 10mb
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Set Specific Host IP (to listen on?) (link)

B4X:
Dim jo As JavaObject = webserver
jo.SetField("host", "ip.address")
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Set SetCookieJsessionidHttpOnly (setHttpOnly) (link)

B4X:
Dim jo As JavaObject = webserver
Dim sessionmanager As JavaObject = jo.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethod("getSessionManager", Null)
sessionmanager.RunMethod("setHttpOnly", Array(True))
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
RemoveServerVersionFromResponses (link)

B4X:
Private Sub RemoveServerVersionFromResponses (Server As Server)
   Dim jo As JavaObject = Server
   jo = jo.GetField("server")
   Dim connectors() As Object = jo.RunMethod("getConnectors", Null)
   For Each co As JavaObject In connectors
     Dim connections() As Object = co.RunMethodJO("getConnectionFactories", Null).RunMethod("toArray", Null)
     For Each connection As JavaObject In connections
       If GetType(connection) = "org.eclipse.jetty.server.HttpConnectionFactory" Then
         Dim configuration As JavaObject = connection.RunMethod("getHttpConfiguration", Null)
         configuration.RunMethod("setSendServerVersion", Array(False))
       End If
     Next
   Next
End Sub
 

tchart

Well-Known Member
Licensed User
Longtime User
Stop Running Server (link)

B4X:
Dim jo As JavaObject = server
jo.GetFieldJO("server").RunMethod("stop", null)
 

tchart

Well-Known Member
Licensed User
Longtime User
Set Max Idle Time (setIdleTimeout) (link)

B4X:
srvr.Start
Dim jo As JavaObject = srvr
Dim connectors() As Object = jo.GetFieldJO("server").RunMethod("getConnectors", Null)
Dim timeout As Long = 50000
For Each c As JavaObject In connectors
   c.RunMethod("setIdleTimeout", Array(timeout))
Next
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Set Max Text Message Size (setMaxTextMessageSize) (link)

B4X:
Sub SetMaxTextMessage(size As Int)
   Dim jo As JavaObject = ws
   jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
   jo.RunMethod("setMaxTextMessageSize", Array(size))
End Sub
 

tchart

Well-Known Member
Licensed User
Longtime User
jServer v2.80+ Enabling TLSv1.1 (setIncludeCipherSuites/setExcludeCipherSuites) (link)

B4X:
Dim jo As JavaObject = ssl
jo.RunMethod("setIncludeCipherSuites", Array(Array As String("TLS_RSA_WITH_AES_128_CBC_SHA")))
jo.RunMethod("setExcludeCipherSuites", Array(Array As String()))
 

tchart

Well-Known Member
Licensed User
Longtime User
Set Samesite=strict on session cookie

B4X:
Sub setSameSite()
    'Note 1 - This must be done after webserver.Start
    'Note 2 - This does stop/start the web server
    Dim jo As JavaObject = webserver
    jo.GetFieldJO("server").RunMethod("stop", Null)
    jo.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("getSessionCookieConfig", Null).RunMethod("setPath", Array("/;SameSite=strict")) 
    jo.GetFieldJO("server").RunMethod("start", Null)
End Sub
 

tchart

Well-Known Member
Licensed User
Longtime User
Get the request cipher suite and ssl version (only tested on Server 3)

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.Secure Then
        Try
            Dim jo As JavaObject = req
            Dim cipher_suite As String = jo.RunMethod("getAttribute",Array As String("jakarta.servlet.request.cipher_suite"))
            Dim ssl_session As JavaObject = jo.RunMethodJO("getAttribute",Array As String("org.eclipse.jetty.servlet.request.ssl_session"))
            Dim ssl_protocol As String = ssl_session.RunMethod("getProtocol",Null)
        
            Log(ssl_protocol)
            Log(cipher_suite)
        Catch
            Log(LastException)
        End Try       
    End If
    
    'rest of handler
end sub
 
Top