Search code examples
javawebserverweb-applications

What is the purpose of web server in a local web application?


I am sure this sounds silly, but for a beginner like me, this is cooking my brain up and I am not able to proceed in my quest without clearing this.

Lets say I am building a simple java command line calculator application that takes 2 numbers and an operator as input and returns the operation's result back to the user. Now, I want to build GUI on top of it (similar to like an online calculator). For this, I decided to build a web application so that I can open the application on my browser and use it seamlessly. It leads to the following questions in my head

  1. Will I have to build a web server in java to allow communications between the front end/GUI and the back end? There is no communication with any other networks, its strictly local; yet I am having to create a web server to make this work and that is confusing me. Or do I need one because I decided to build a web application meant to be opened in a web browser? Or is the reason something else entirely?
  2. If I instead decided to build a windows/android/mac/ios application, would I still to bake a web server for communicating? Or can I use something like swings (I know its really primitive) to do this which essentially would allow the GUI to directly communicate with the business logic?
  3. I know front-end languages like javascript allow me to code the entirety of the calculator in itself, and as a result eliminate the need of a web server. However, what if I was building relatively complex application with a backend database, like spotify? (within the same constraint that the application doesn't need to communicate with other applications and all data is stored locally) Would this solution still work?

In brief, I am failing to understand the purpose of building an entire client-server infrastructure even though the entire thing is local and offline(if that makes sense?). Or is my very basic understanding of client-server flawed?


Solution

  • If you want to use a web browser as the tool to interface with your Java app, then you need a web server. The browser is simply an app that tries to make a network connection with another app, and passes some text as defined by the HTTP protocol.

    You would have a choice of three scenarios for this:

    • Write an app that accepts network connections, processes HTTP, and sends back a response.
    • Write a Java app that uses the very basic web server built into Java 18 and later. Note that this web server is not intended to be a feature-rich or commercial-grade server.
    • Write a Jakarta Servlet class that runs on top of a Servlet container with a web server. For your needs, either Apache Tomcat or Eclipse Jetty would work well as both the Servlet container and the web server.

    For your scenario, the middle option using Java JEP 408 seems most appropriate and easiest.

    You said:

    There is no communication with any other networks, its strictly local; yet I am having to create a web server to make this work and that is confusing me.

    A Border Collie dog herds sheep. That is its most basic basic core mission. Those sheep can be herded across the hills of a mountain range or they can be herded locally within your own farm.

    A web browser makes network connections. That is its most basic core mission. Those network connections can be made over the actual network or they can be made locally within a single computer ( a “localhost”).

    You said:

    Or do I need one because I decided to build a web application meant to be opened in a web browser?

    Yes a web application accessed by a web browser needs a web server, by definition.

    You said:

    If I instead decided to build a windows/android/mac/ios application, would I still to bake a web server for communicating?

    Or can I use something like swings (I know its really primitive)

    There is nothing “primitive” about Swing. It is a fully-developed feature-rich GUI framework. Swing comes built in with every JDK. And Swing will be supported for many years to come.

    However, the design and features of Swing may or may not suit your tastes. And Swing is now in maintenance-mode.

    An alternative is JavaFX, now actively developed as the OpenJFX libraries.

    To use JavaFX, you must either add the OpenJFX libraries to your project or else deploy to a JDK that comes bundled with the OpenJFX libraries (ZuluFX, LibericaFX, etc.).

    to do this which essentially would allow the GUI to directly communicate with the business logic?

    Yes the GUI and and your calculator business logic would all be plain Java classes, all peers, all running together within the same JVM.

    You said:

    I know front-end languages like javascript allow me to code the entirety of the calculator in itself, and as a result eliminate the need of a web server.

    Yes. You could cook up JavaScript code to implement your little calculator. This JavaScript could be placed within the text of a file with the HTML of a web page. That web page file could be opened locally by the web browser. Your HTML and JavaScript would render. No web server is needed in this scenario.

    But this scenario eliminates Java, and you said you want to (a) write your calculator in Java, and () use a web browser. So we go back to the three options listed above.

    You said:

    However, what if I was building relatively complex application with a backend database, like spotify? (within the same constraint that the application doesn't need to communicate with other applications and all data is stored locally) Would this solution still work?

    Yes you could deploy a database server on your local machine. Then you could find and use a JavaScript binding tool to talk to that database.

    But then you would not be using Java and all its goodness. For that scenario I would choose to write a JavaFX app with an embedded Java-based database engine such as H2. This would result in a single all-in-one double-clickable-app solution.

    But that would be my choice based on my own preferences and skill set. Other people might choose other solutions.

    the purpose of building an entire client-server infrastructure even though the entire thing is local and offline

    👉 The “offline” part may be distracting you.

    Conventional computers with modern operating systems such as macOS, BSD, Linux, Windows, etc. are always “online” in that they all maintain a networking stack. That network stack can be used locally by processes within the one computer to communicate with each other. Whether you happen to have an Ethernet cable plugged in, or WiFi turned on, makes little difference to the computer. The networking stack in the OS is still available and active regardless of outside network access.

    So client-server architecture works just as well within a single computer as it does between computers. (Actually, it’s works faster locally, as hopping on and off the network is terribly slow.)

    Why choose client-server architecture for a local app? Either:

    • You are skilled in, and prefer, the tools for a client-server architecture.
    • You want to eventually move from local-only to being networked.

    If neither of those two is true, then the JavaFX with H2 solution I mentioned is likely a better fit for a Java programmer.


    By the way, let me put in a plug for Vaadin Flow, a GUI framework for building web apps in pure Java.