简体   繁体   English

将 java 代码嵌入 html 文件的最佳方法?

[英]Best way to embed java code into an html file?

I'd rather not use javascript. I have some code that includes scanner input and was wanting to implement it into my website(html).我不想使用 javascript。我有一些包含扫描仪输入的代码,并想将其实现到我的网站 (html) 中。 I've tried SpringBoot but those tutorials weren't what I was looking for.我试过 SpringBoot,但那些教程不是我想要的。 I've uploading a.jsp file but the syntax was not correct.我上传了一个 .jsp 文件,但语法不正确。 Do you have any tips?你有什么建议吗?

You cannot.你不能。 The browsers only understand javascript. Java is used as a server side language for server side applications inside an application server like Tomcat or JBoss (if you use Spring Boot you can use an embedded Tomcat), the same (but more powerful in my opinion) way you need an Apache server with a PHP interpreter to serve php pages.浏览器只理解 javascript。Java 用作应用程序服务器内部服务器端应用程序的服务器端语言,如 Tomcat 或 JBoss(如果使用 Spring Boot,则可以使用嵌入式 Tomcat),相同(但我认为更强大)你需要一个 Apache 服务器和一个 PHP 解释器来服务 php 个页面。

You can use a JSP page.您可以使用 JSP 页面。 But they are intended for generating HTML in the server side.但它们用于在服务器端生成 HTML。 When you load the HTML in the browser the Java code has ended its execution and you cannot do any interaction with the Java side other than reload the same or other page entirely or using ajax requests .当您在浏览器中加载 HTML 时,Java 代码已结束执行,您无法与 Java 端进行任何交互,除非完全重新加载相同或其他页面或使用ajax 请求 In any case, the Java code is executed in a server, not in the browser.无论如何,Java 代码是在服务器中执行的,而不是在浏览器中执行的。

You don't need to create a .jsp file even that it's outdated.您无需创建.jsp文件,即使它已过时。 You can add a <script> …</script> like this:你可以像这样添加一个<script> …</script>

<script language=JavaScript>
<!--type the javascript code here-->
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM