[英]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方法。
Please slice your project into two(three, if there is a database management) parts: 请将您的项目分成两部分(如果有数据库管理,则分为三部分):
response.getWriter().write("hello from server"); response.getWriter()。write(“来自服务器的问候”);
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进行客户端和服务器之间的通信。
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.