简体   繁体   English

如何将简单的消息从网页(HTML / Javascript)发送到Java应用程序?

[英]How can I send simple messages from a webpage (HTML/Javascript) to a Java application?

I have a Java application which expects to be sent either an integer and a String in order to carry out operations. 我有一个Java应用程序,它希望发送一个整数和一个String以便执行操作。 This program is already written and functional. 该程序已经编写并且可以运行。 However, I wish to use a webpage to control it. 但是,我希望使用网页来控制它。 Therefore, I am hoping to have a webpage which can send this data as controlled by the user. 因此,我希望有一个可以在用户控制下发送此数据的网页。 However, I do not know how to go about doing this. 但是,我不知道该怎么做。

What is the simplest way of sending messages from HTML/Javascript to a Java program? 从HTML / Javascript向Java程序发送消息的最简单方法是什么?

I have access to Java SE only. 我只能访问Java SE。

It depends on protocol (adapter) your application is using. 这取决于您的应用程序正在使用的协议(适配器)。 It can be a HTTP, websockets or something else. 它可以是HTTP,websockets或其他东西。

I can assume that you are talking about HTTP, so the easiest way is to use form html element and POST method. 我可以假设你正在谈论HTTP,所以最简单的方法就是使用form html元素和POST方法。

check this 检查这个

Please slice your project into two(three, if there is a database management) parts: 请将您的项目分成两部分(如果有数据库管理,则分为三部分):

  1. Server side - core side. 服务器端-核心端。 Here you are able to implement your java code. 在这里,您可以实现Java代码。 That means you must have running a server like tomcat. 这意味着您必须运行诸如tomcat之类的服务器。 Create new web app project. 创建新的Web应用程序项目。 Include tomcat or something else as server. 包括tomcat或其他东西作为服务器。 Create a Servlet (do not ask how to create but search through internet). 创建一个Servlet(不要问如何创建,而是通过Internet搜索)。 In that servlet override doPost method. 在该servlet中重写doPost方法。 Including this code inthat method gives you an assumption about server side: 在该方法中包含此代码可为您提供有关服务器端的假设:

response.getWriter().write("hello from server"); response.getWriter()。write(“来自服务器的问候”);

  1. Client side. 客户端。 Client side is where you implement html, js and css code. 客户端是实现html,js和css代码的地方。 Submit form to that servlet by post method. 通过post方法将表单提交到该servlet。

If you have correctly configured web.xml then on submitting this form you should receive "hello from server" as a reponse. 如果您已经正确配置了web.xml,那么在提交此表单时,您应该收到“来自服务器的问候”。 Use eclipse as IDE that is easier to configure. 将eclipse用作更易于配置的IDE。

You can use JSP / Servlet to communicate between client and server. 您可以使用JSP / Servlet进行客户端和服务器之间的通信。

See here for some basic stuff 看到这里一些基本的东西

Question: Do you have maven? 问题:你有行家吗? If not I would encourage you to download it. 如果没有,我鼓励您下载它。 It's simple: https://maven.apache.org/ 很简单: https//maven.apache.org/

If yes, add these to your pom file: 如果是,请将它们添加到您的pom文件中:

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Now in the class which has your main application add some annotations and a controller (method) to handle your POST request: 现在,在具有您的主应用程序的类中,添加一些注释和一个控制器(方法)来处理您的POST请求:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@EnableAutoConfiguration
public class SpringBootSampleApplication {

    @RequestMapping("/")
    @ResponseBody
    @CrossOrigin
    String home() {
        return "Hello World!";
    }

    /** Receives a POST request on path / with parameter name. Will return parameter
     * 
     * @param request
     * @return */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin // this will allow requests origination from any domain. see CORS
    String greet(HttpServletRequest request) {
        return "Greetings: " + request.getParameter("name");
    }

    /** starts the application
     * 
     * @param args
     * @throws Exception */
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootSampleApplication.class, args);
    }
}

This will start a web server on port 8080. 这将在端口8080上启动Web服务器。

Check out this code sample here: https://github.com/mikibrv/spring.boot.sample 在此处查看此代码示例: https : //github.com/mikibrv/spring.boot.sample

I have also added a small index.html file in the resources as an example of AJAX. 我还在资源中添加了一个小的index.html文件作为AJAX的示例。

It's a full project, generating 2 endpoints ( a GET and POST ) with CORS enabled and when you build it with Maven (mvn clean install) you get a fat JAR which you can call using : java -jar target/spring.boot.sample.jar 这是一个完整的项目,在启用了CORS的情况下生成2个端点(GET和POST),并在使用Maven(mvn全新安装)进行构建时,会得到一个胖JAR,可以使用以下命令进行调用:java -jar target / spring.boot.sample 。罐

For more info: http://projects.spring.io/spring-boot/ 有关更多信息: http : //projects.spring.io/spring-boot/

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

相关问题 如何将简单数据从JavaScript发送到Java应用程序 - How to send simple data from javascript to java application 如何将消息从Javascript发送到Java? - How do I send messages from Javascript to Java? 如何从网页上的javascript文件中删除html标签? - How can I remove html tags from a javascript file on a webpage? 如何使用 vanilla JavaScript 将 HTML 元素插入到 Angular 应用程序网页中? - How can I use vanilla JavaScript to insert HTML elements into an Angular application webpage? 如何从我的Swing应用程序向javascript发送字符串? - How can i send a string from my Swing application to javascript? 如何将字符串消息从可视C#表单应用程序发送到网页 - How to send string messages from a visual c# form application to a webpage 如何使用 ActiveMQ-Artemis 从 Java 程序向 Javascript STOMP 客户端发送消息? - How do I send messages from Java program to a Javascript STOMP client using ActiveMQ-Artemis? 如何将 React 组件添加到由 HTML 和 CSS 组成的简单网页? - How can I add a react component to a simple webpage made up of HTML and CSS? 如何从网页中删除一些JavaScript? - How can I remove a some javascript from my webpage? 如何发出消息,用户可以在不运行它的情况下发送php,javascript或html - how to make messages, where users can send php, javascript or html without running it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM