简体   繁体   English

在嵌入式HTML而非JavaScript上使用Java代码?

[英]Using Java code on embedded HTML instead of JavaScript?

I am working on a project which is basically a Java application with an embedded IE browser (using JDIC 0.9.5) to display custom HTML files (stored locally that I created). 我正在研究一个项目,该项目基本上是带有嵌入式IE浏览器(使用JDIC 0.9.5)的Java应用程序,用于显示自定义HTML文件(存储在我创建的本地文件中)。 I have a test HTML file with a JavaScript function that checks a simple form with checkboxes and alerts the user with a dialog stating which checkboxes are checked. 我有一个带有JavaScript函数的测试HTML文件,该文件可以检查带有复选框的简单表单,并通过对话框提示用户选中了哪些复选框来提醒用户。

My question is, is there a way for my Java application to do the same procedure on the embedded HTML form instead of using JavaScript. 我的问题是,我的Java应用程序是否可以在嵌入式HTML表单上执行相同的过程,而不是使用JavaScript。 I want to keep my application and HTML files simple without the clutter of JavaScript in my HTMLs or a pile of .js files. 我想使我的应用程序和HTML文件保持简单,而不会在我的HTML或一堆.js文件中造成JavaScript混乱。 Thanks for any help and guidance! 感谢您的帮助和指导!

You have two options. 您有两个选择。 Either shift the project to run the Java on the server side using JSP technology inside a web container like Apache Tomcat or Jetty, or write you web page to open up a Java applet. 可以通过在Apache Tomcat或Jetty之类的Web容器内使用JSP技术转移项目以在服务器端运行Java,或者编写您的网页来打开Java小程序。

The applet route allows you to run the code on someone else's machine, and as a trade off you will have to run the application in a strongly security constrained environment. applet路由允许您在其他人的机器上运行代码,但作为权衡,您将不得不在安全性受到严格限制的环境中运行应用程序。 After all, if someone were to run code on your machine, you wouldn't want it able to access your disk, etc. 毕竟,如果有人要在您的计算机上运行代码,则您不希望它能够访问您的磁盘等。

The JSP solution will have you running the code on the same machine as your web server, since you (probably) control your own web server, the code will not be ran with as many security constraints enabled. JSP解决方案将让您在与Web服务器相同的机器上运行代码,因为您(可能)控制自己的Web服务器,因此不会在启用了那么多安全性约束的情况下运行代码。 This means the code can make requests to other machines, write and read files, etc. 这意味着代码可以向其他计算机发出请求,写入和读取文件等。

您可以使用Java Server Pages(JSP)将其模型替换为客户端服务器模型。

If you are displaying the page through an embedded browser it is very unlikely that you will be able to get access to the DOM via Java. 如果要通过嵌入式浏览器显示页面,则极不可能通过Java访问DOM。

One option is to use GWT javascript compiler to code in java and then convert to javascript. 一种选择是使用GWT javascript编译器在Java中进行编码,然后转换为javascript。 If it will only use IE, then you will only need to keep one of the generated .js files, so the clutter will be low. 如果仅使用IE,则仅需保留其中一个生成的.js文件,因此杂乱无章。

Since you're embedding your HTML files in your own Java program, I would recommend you to use one of these 2 approaches: 由于您将HTML文件嵌入自己的Java程序中,因此建议您使用以下两种方法之一:

1.- Use Javascript and structure the files cleanly, is not that complex. 1.-使用Javascript干净地构造文件,并不复杂。

2.- When you do the POST check the values in your Java code and return a new dynamically generated HTML file with needed info 2.-执行POST时,请检查Java代码中的值,并返回带有所需信息的新动态生成的HTML文件

I sincerely recommend you to follow the first option. 我真诚地建议您遵循第一个选择。 Other options to work with Java in HTML would be JSP or GWT, but both will require a proper J2EE server, which would be overkill in your application 在HTML中与Java一起使用的其他选项是JSP或GWT,但是两者都需要适当的J2EE服务器,这在您的应用程序中会显得过大

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

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