你如何将 Java 后端与 html/css 前端连接起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50671420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:44:53  来源:igfitidea点击:

How do you connect Java backend with html/css frontend?

javahtmlcssfrontendbackend

提问by A. Mis

Hi I know the language of HTML, CSS, and Java pretty well, but I have absolutely no idea on how to connect the java backend to frontend. I'm a complete amateur when it comes to programming, I just know the basics of each of these languages pretty well. I have my HTML and CSS coded to take in input, and I have all the calculations written in Java, so I have the functions of what I want to do all ready. I just don't know how to pass the user's input from the HTML, to the calculations in Java. I just run my Java code using scanner as the user input, but I want to integrate all my codes to make it a real website, and I think I'm going to need more than scanner for a user to input something on a website. I researched this topic multiple times, and I found information about serverlets and IntelliJ, but the information was confusing to me because again, I am a complete amateur in computer science (other than knowing these languages). Some also said the best way was to convert my java code to javascript, but I'm not sure if that's the best way. Any suggestions/information on what to do? Here's some of my java code (this part takes in user input, searches for the numbers before the last slash, then adds it to double firsum). Thanks!

嗨,我非常了解 HTML、CSS 和 Java 语言,但我完全不知道如何将 Java 后端连接到前端。在编程方面,我是一个完全的业余爱好者,我只是非常了解每种语言的基础知识。我已经将 HTML 和 CSS 编码为接收输入,并且我已经用 Java 编写了所有计算,所以我已经准备好了我想要做的所有功能。我只是不知道如何将用户的输入从 HTML 传递到 Java 中的计算。我只是使用扫描仪作为用户输入来运行我的 Java 代码,但我想整合我的所有代码以使其成为一个真正的网站,而且我认为我需要的不仅仅是扫描仪让用户在网站上输入一些东西。我多次研究这个话题,我找到了关于serverlets和IntelliJ的信息,但是这些信息让我感到困惑,因为我是计算机科学领域的完全业余爱好者(除了了解这些语言)。有些人还说最好的方法是将我的 java 代码转换为 javascript,但我不确定这是否是最好的方法。关于该怎么做的任何建议/信息?这是我的一些 java 代码(这部分接受用户输入,搜索最后一个斜杠之前的数字,然后将其添加到 double firsum)。谢谢!

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double firsum = 0; 
    while (sc.hasNext()) {
        String ip = sc.nextLine();
        int def = ip.lastIndexOf("/");
        double firnum = 0;
        for (int i = 1; i < 7; i++) {
            if (ip.charAt(def-i) != '\t') {
                firnum = Double.parseDouble(ip.substring(def - i, def));
                out.println(firnum);
            }
        }
        firsum += firnum;
    }
} 

回答by OneCricketeer

You can use a Java micro web frameworks like Spring, Dropwizard, SparkJava to respond to HTTP requests and produce HTML content, but the common denominator to all frameworks is Servlets. Servlets respond to HTTP requests and can send "static files" (html, css, js, images, etc). Other options include full web application servers such as Tomcat, JBoss, GlassFish, Java EE

您可以使用 Spring、Dropwizard、SparkJava 等 Java 微型 Web 框架来响应 HTTP 请求并生成 HTML 内容,但所有框架的共同点是 Servlet。Servlet 响应 HTTP 请求并可以发送“静态文件”(html、css、js、图像等)。其他选项包括完整的 Web 应用程序服务器,例如 Tomcat、JBoss、GlassFish、Java EE

In order for dynamic form actions to occur, most web developers would use AJAX (Javascript), and have the front-end communicate via a backend API.

为了发生动态表单操作,大多数 Web 开发人员会使用 AJAX (Javascript),并让前端通过后端 API 进行通信。

You'll also find that majority of websites are not using Java for hosting HTML/CSS/JS, as you can use any backend language with any front-end library via RESTful actions, which, on the web, are interacted with via Javascript (not Java Scanner objects)

您还会发现大多数网站都没有使用 Java 来托管 HTML/CSS/JS,因为您可以通过 RESTful 操作将任何后端语言与任何前端库一起使用,这些操作在 Web 上通过 Javascript 进行交互(不是 Java Scanner 对象)

回答by YAHsaves

You're going to need javascriptas a connecting bridge between your front end website and backend java.

您将需要javascript作为前端网站和后端 java 之间的连接桥梁。

Javascript is the processing language of the web, and it will run natively on a clients browser. You can use its ajaxfeature to take user input and upload the value to your server.

Javascript 是网络的处理语言,它将在客户端浏览器上本地运行。您可以使用其ajax功能获取用户输入并将值上传到您的服务器。

However because you mention you are new to programming, I might suggest looking into a more "standard" server language than java. Don't get me wrong it is commonly used, but for beginners languages like php and python have much more front end connection "built in" to make your life easier.

但是,因为您提到您是编程新手,所以我可能建议您研究一种比 java 更“标准”的服务器语言。不要误会我的意思,它是常用的,但是对于初学者来说,像 php 和 python 这样的语言“内置”了更多的前端连接,让您的生活更轻松。

回答by Mattias Zurkovic

Like cricket said, you can use a Java based web framework, I suggest Spring. This will provide all the functionality you need such as allowing you to save, index, search, write routes, etc for you application. Depending on how new you are, you probably want to make a basic CRUD appto get to know Spring.

就像板球说的那样,您可以使用基于 Java 的 Web 框架,我建议使用Spring。这将提供您需要的所有功能,例如允许您为您的应用程序保存、索引、搜索、写入路由等。根据您的新程度,您可能想要制作一个基本的CRUD 应用程序来了解 Spring。

You also want to think of your app from an architectural perspective. Do you want to make a single monolithic application that handles all your business logic, services, and the frontend - or do you want to make it more modular with microservices? (The second option is probably better). For this you are going to want to write an API that handles all your application's data (using Spring), and then make a small frontend app that gets that information and presents it.

您还想从架构的角度考虑您的应用程序。您是想制作一个处理所有业务逻辑、服务和前端的单一应用程序 - 还是想通过微服务使其更加模块化?(第二种选择可能更好)。为此,您将需要编写一个 API 来处理您的所有应用程序数据(使用 Spring),然后制作一个小型前端应用程序来获取该信息并呈现它。

Hope it helped :)

希望它有所帮助:)