简体   繁体   English

web 服务器在本地 web 应用程序中的用途是什么?

[英]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.假设我正在构建一个简单的 java 命令行计算器应用程序,该应用程序将 2 个数字和一个运算符作为输入并将运算结果返回给用户。 Now, I want to build GUI on top of it (similar to like an online calculator).现在,我想在它之上构建 GUI(类似于在线计算器)。 For this, I decided to build a web application so that I can open the application on my browser and use it seamlessly.为此,我决定构建一个 web 应用程序,以便我可以在浏览器上打开该应用程序并无缝使用它。 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?我是否必须在 java 中构建 web 服务器以允许前端/GUI 和后端之间的通信? 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.但是我必须创建一个 web 服务器来完成这项工作,这让我感到困惑。 Or do I need one because I decided to build a web application meant to be opened in a web browser?或者我需要一个,因为我决定构建一个 web 浏览器中打开的web应用程序? 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?如果我决定构建一个 windows/android/mac/ios 应用程序,我还会烘烤一个 web 服务器进行通信吗? 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?或者我可以使用类似swings的东西(我知道它真的很原始)来做到这一点,这基本上可以让GUI直接与业务逻辑通信?
  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.我知道像 javascript 这样的前端语言允许我自己编写整个计算器,因此不需要 web 服务器。 However, what if I was building relatively complex application with a backend database, like spotify?但是,如果我使用后端数据库(如 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?还是我对客户端-服务器的基本理解有缺陷?

If you want to use a web browser as the tool to interface with your Java app, then you need a web server.如果您想使用 web 浏览器作为与您的 Java 应用程序交互的工具,那么您需要一个 web 服务器。 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.浏览器只是一个尝试与另一个应用程序建立网络连接的应用程序,并传递一些由 HTTP 协议定义的文本。

You would have a choice of three scenarios for this:为此,您可以选择三种方案:

  • Write an app that accepts network connections, processes HTTP, and sends back a response.编写一个接受网络连接、处理 HTTP 并返回响应的应用程序。
  • Write a Java app that uses the very basic web server built into Java 18 and later.编写一个 Java 应用程序,该应用程序使用 Java 18 及更高版本中内置的非常基本的 web 服务器 Note that this web server is not intended to be a feature-rich or commercial-grade server.请注意,此 web 服务器并非旨在成为功能丰富或商业级服务器。
  • Write a Jakarta Servlet class that runs on top of a Servlet container with a web server.编写一个在带有 web 服务器的 Servlet 容器之上运行的Jakarta Servlet class。 For your needs, either Apache Tomcat or Eclipse Jetty would work well as both the Servlet container and the 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.对于您的方案,使用 Java JEP 408 的中间选项似乎最合适和最简单。

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.但是我必须创建一个 web 服务器来完成这项工作,这让我感到困惑。

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. web 浏览器建立网络连接。 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?或者我需要一个,因为我决定构建一个 web 浏览器中打开的 web 应用程序?

Yes a web application accessed by a web browser needs a web server, by definition.是的,根据定义,由 web 浏览器访问的 web 应用程序需要 web 服务器。

You said:你说:

If I instead decided to build a windows/android/mac/ios application, would I still to bake a web server for communicating?如果我决定构建一个 windows/android/mac/ios 应用程序,我还会烘烤一个 web 服务器进行通信吗?

Or can I use something like swings (I know its really primitive)或者我可以使用秋千之类的东西(我知道它真的很原始)

There is nothing “primitive” about Swing. Swing 没有任何“原始”。 It is a fully-developed feature-rich GUI framework.它是一个完全开发的功能丰富的 GUI 框架。 Swing comes built in with every JDK. Swing 内置在每个 JDK 中。 And Swing will be supported for many years to come. Swing 将在未来多年内得到支持。

However, the design and features of Swing may or may not suit your tastes.但是,Swing 的设计和功能可能适合您的口味,也可能不适合您的口味。 And Swing is now in maintenance-mode. Swing 现在处于维护模式。

An alternative is JavaFX , now actively developed as the OpenJFX libraries.另一种方法是JavaFX ,现在作为 OpenJFX 库积极开发。

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.).要使用 JavaFX,您必须将OpenJFX库添加到您的项目中,或者部署到与 OpenJFX 库( ZuluFX 、LibericaFX 等)捆绑在一起的 JDK。

to do this which essentially would allow the GUI to directly communicate with the business logic?这样做本质上将允许 GUI 直接与业务逻辑通信?

Yes the GUI and and your calculator business logic would all be plain Java classes, all peers, all running together within the same JVM.是的,GUI 和您的计算器业务逻辑都是简单的 Java 类,所有对等点,都在同一个 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.我知道像 javascript 这样的前端语言允许我自己编写整个计算器,因此不需要 web 服务器。

Yes.是的。 You could cook up JavaScript code to implement your little calculator.您可以编写 JavaScript 代码来实现您的小计算器。 This JavaScript could be placed within the text of a file with the HTML of a web page.此 JavaScript 可以放置在具有 web 页面的 HTML 的文件的文本中。 That web page file could be opened locally by the web browser. web 浏览器可以在本地打开该 web 页面文件。 Your HTML and JavaScript would render.您的 HTML 和 JavaScript 将呈现。 No web server is needed in this scenario.在这种情况下不需要 web 服务器。

But this scenario eliminates Java, and you said you want to (a) write your calculator in Java, and () use a web browser.但是这种情况消除了 Java,并且您说您想要 (a) 在 Java 中编写您的计算器,并且 () 使用 web 浏览器。 So we go back to the three options listed above.所以我们 go 回到上面列出的三个选项。

You said:你说:

However, what if I was building relatively complex application with a backend database, like spotify?但是,如果我使用后端数据库(如 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.然后您可以找到并使用 JavaScript 绑定工具与该数据库对话。

But then you would not be using Java and all its goodness.但是,您将不会使用 Java 及其所有优点。 For that scenario I would choose to write a JavaFX app with an embedded Java-based database engine such as H2 .对于这种情况,我会选择编写一个 JavaFX 应用程序,其中包含基于 Java 的嵌入式数据库引擎,例如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.具有现代操作系统(例如 macOS、BSD、Linux、Windows 等)的传统计算机始终“在线”,因为它们都维护着一个网络堆栈。 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.无论您碰巧插入了以太网电缆,还是打开了 WiFi,对计算机都没有什么影响。 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.如果这两个都不成立,那么我提到的带有 H2 解决方案的 JavaFX 可能更适合 Java 程序员。


By the way, let me put in a plug for Vaadin Flow , a GUI framework for building web apps in pure Java.顺便说一句,让我插入一个Vaadin Flow插件,这是一个 GUI 框架,用于在纯 Java 中构建 web 应用程序。

If you don't want any of those security and all..then separate webserver is not necessary to host in local system.如果您不想要任何这些安全性和所有..那么单独的网络服务器不需要在本地系统中托管。

@Mark Yes Tomcat is also kind of webserver just to make it simple understanding i used word tomcat since its very familiar @Mark 是的 Tomcat 也是一种网络服务器,只是为了简单理解,我使用了 tomcat 这个词,因为它非常熟悉

It's not completely necessary to bake web server to host your application even with embedded tomcat application you can host your application.即使使用嵌入式 tomcat 应用程序,您也可以托管您的应用程序。 However, main purpose of webserver usage is for setting up your reverse proxy so that your application behind that won't be exposed and your webserver will be acting as endpoint to outside requests.但是,使用网络服务器的主要目的是设置您的反向代理,以便您的应用程序不会被暴露,并且您的网络服务器将充当外部请求的端点。

In case for hosting application in local webserver is not at all required webserver will come handy when your application hosted in production environment.如果在本地网络服务器中托管应用程序根本不需要网络服务器,那么当您的应用程序托管在生产环境中时,它会派上用场。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Swing 应用程序中实现 web 套接字本地服务器 - Implement web socket local server in Swing application 如何将 Web 套接字从 Internet Web 应用程序发送到本地服务器 - How to send web sockets from an internet web application to a local server glassfish应用程序服务器中的Web服务器是什么? - what is the web server in glassfish application server? Java Web应用程序中主要方法的目的 - Purpose of main method in Java web application 应用服务器应用程序客户端的目的是什么? - What is the purpose of an Application Server Application-Client? 是什么目的 <filter> web.xml中的标签? - What is the purpose of <filter> tag in web.xml? 如何从Linux服务器Java Web应用程序访问本地路径? - How to access local path from linux server java web application? 如何在Web应用程序中获取服务器本地驱动器路径? - How to get server local drive path in web application? 使用本地Web服务器使用Java应用程序对Google驱动器进行身份验证 - Authenticating google drive with a java application using a local web server 想要在本地机器的JBoss Server中分析Web应用程序 - Want to profiling web application in JBoss Server in local machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM