B4J Question Creating B4J server and static web pages site - how to

Mark Stuart

Well-Known Member
Licensed User
Longtime User
Hi y'all,

I'm wanting to setup a web site and a web server. The web server will be a B4J app. It has nothing to do with the static web pages.
So 2 different entities.

I've had help with the setup of a Ubuntu on DigitalOcean.
Installed so far:
1. MS SQL server
2. JRE version 21.0.9
3. Docklet that supports the above?

The B4J app and files have not been installed as of yet, as I'm still developing and testing that.
What I need to know is has anyone done both on the same host so that the website and the B4J web server is running on the same VPS. Possibly 2 Docklets?

Some say to install NGINX and it will receive the incoming client data (JSON) and pass it to the appropriate B4J server port. eg: 8080
The website is listening to ports 80 and 443, right?

Internet
|
v
Nginx (80 / 443) --> status website
Java B4J (8080) --> backend only

Is this correct?

But how to do this has me puzzled as I do not know server stuff or Linux, etc. Just how to develop apps. (old cliche, right?)

I need help on this please.

Regards,
Mark Stuart
 

Mark Stuart

Well-Known Member
Licensed User
Longtime User
The proper way is not to put html file inside www folder. It is fine if you are doing a test.
The recommended way is to put the html files inside "Files" folder during development and load them from File.DirAssets so it will be compiled inside the jar file.
What about the css and image files? Where would you put them?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
What about the css and image files? Where would you put them?
css and images files can put inside www folder which is same path as the jar.
www folder will become your document root.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
The proper way is not to put html file inside www folder. It is fine if you are doing a test.
The recommended way is to put the html files inside "Files" folder during development and load them from File.DirAssets so it will be compiled inside the jar file.
If the html files are in the development Files folder, how does the runtime on the server access the html files?
I don't see how that works.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
css and images files can put inside www folder which is same path as the jar.
www folder will become your document root.
So all css and image files will be in the same folder together? That's a bit of a mess. That's why I created the css and images subfolders under www. But B4J and the html files are having an issue seeing them.
Yes, most likely the html files will see the css and image files if they are all together with the html files, but that's too disorganized for me.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If the html files are in the development Files folder, how does the runtime on the server access the html files?
I don't see how that works.
The compiled jar can access the resources inside itself which is containing the Files folder.

So all css and image files will be in the same folder together? That's a bit of a mess. That's why I created the css and images subfolders under www. But B4J and the html files are having an issue seeing them.
Yes, most likely the html files will see the css and image files if they are all together with the html files, but that's too disorganized for me.
No, you can use subfolders to organize the files.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
The compiled jar can access the resources inside itself which is containing the Files folder.


No, you can use subfolders to organize the files.
"The compiled jar can access the resources inside itself which is containing the Files folder."
My question relating to the resources is, how does the code do that? I don't understand that at all.
The html files inside the .jar file makes no sense. They need to reside in a server folder for each html file to access with a link or button.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
"No, you can use subfolders to organize the files."
That's how I have it now and the html files and B4J can't see them.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
The html files inside the .jar file makes no sense. They need to reside in a server folder for each html file to access with a link or button.
No. It doesn't necessary to be a static html file reside in a folder that you can see.
HTML "response" can be written and sent to the client browser dynamically with B4J server.

programmatically
assets
www
 

Attachments

  • testserver_2026-02-05_0756.zip
    79.8 KB · Views: 46
Last edited:
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
No. It doesn't necessary to be a static html file reside in a folder that you can see.
HTML "response" can be written and sent to the client browser dynamically with B4J server.
Got your B4J app and looked thru it.
Have you tested the app on a server?
If so, how and what was copied to the server, and what file and folder structure did you use?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
css and images files can put inside www folder which is same path as the jar.
www folder will become your document root.
but .jar file in the www-folder is ... downloadable. Looks wrong for me...
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Got your B4J app and looked thru it.
Have you tested the app on a server?
If so, how and what was copied to the server, and what file and folder structure did you use?
Just unzip the content to a folder preserving the same folder structure.

About what files you need to copy,
1. Compile the project in release mode and stop (Kill process)
2. Click the Distribute macro title button on top
3. Find the dist.zip
4. The .jar and the www folder inside it are the only file and folder you need to upload to a directory on your server.

Note: Remember to edit the server port.

dist
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
but .jar file in the www-folder is ... downloadable. Looks wrong for me...
No, you can't download the .jar if it is the document root.
You should also not to put the .jar inside www folder.
I mean they sit side by side on same level.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I also realized if we don't specify any handler, the server will load the index.html inside www directory by default.

This is the most minimal code required.
B4X:
' <link>Click to browse|http://127.0.0.1:8080</link>
Sub AppStart (Args() As String)
    srvr.Initialize("")
    srvr.Start
    StartMessageLoop
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you ask me why we don't put index.html inside www folder?
Answer is because files reside inside www is public accessible directly using browser address bar or through the http GET.

We don't want to expose our source files.
This is why we keep our html template as assets inside the .jar file so it is protected from public access.
By examining the content of Files/world.html, you will realize the following line.
HTML:
<h3 class="success">Hello $USER$!</h3>

When the server app is running, B4J loads the template and replaces the value using the following code:
B4X:
Sub ShowWorld
    Dim Content As String = File.ReadString(File.DirAssets, "world.html") ' Load from assets
    Content = Content.Replace("$USER$", "Mark")
    Response.ContentType = "text/html"
    Response.Write(Content)
End Sub

When a visitor or client browse the route http://127.0.0.1:8080/world, he/she is not accessing the route http://127.0.0.1:8080/world.html but accessing the route handled by the server.
He/She will only see:
HTML:
<h3 class="success">Hello Mark!</h3>
The source and the final output are different.
He/She would not know how the file is loaded or processed.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
Hi y'all,

I now have a "full-stack" application running!

It has been quite the struggle getting the B4J server up and running, as it is running as a web service and hosts my static website.

JSON Data Flow:
B4A client --> B4J server (GET and POST web service) --> MS SQL Server EXPRESS database.

Server Deployment - B4J, Java, and MS SQL Server are running on a DigitalOcean Droplet.

Client apps produce Activity Log JSON data that is sent to B4J, when there is an internet connection.
One of the client apps that produces transactional JSON data is sent to a customer's Alpha Anywhere web service.

This is the first time I have completed a full-stack application for myself, for my new business. To me, it's a big deal!!

Thanx first to Erel for making incredible, easy to use software development tools. Don't stop!

Thank you to Paul (a business owner) for encouraging me and getting me excited about going forward with this business.

And a big thanx to Tom and Lucas for helping me with getting DigitalOcean up, configured, and running. It wouldn't be running without your expertise and knowledge.
Lucas, you are such a wiz with B4J.

Mark Stuart
 
Upvote 0
Top