开发者

Send JSON data from JavaScript to a Java program on the server

开发者 https://www.devze.com 2023-03-28 06:25 出处:网络
I have a webpage that does a lot of processing in JavaScript and creates some nice tables/graphs/etc. I want to export this to Excel and have not found any PHP libraries that can handle charts, so I w

I have a webpage that does a lot of processing in JavaScript and creates some nice tables/graphs/etc. I want to export this to Excel and have not found any PHP libraries that can handle charts, so I wrot开发者_Go百科e a Java program using using Apachi's POI library. Now the problem is, how can I send the JSON data to the Java program. Initially, I thought I could just send the JSON data in a POST request to a PHP page, then have PHP do a system call and pass the data on the command line. But 700,000 characters might be a bit too much for the command line. I looked into JSP and Servlets but have no experience with those (note my server doesn't have this installed either). Any suggestions on how to send data directly to the Java program?

Update: I found out you can invoke method calls in a Java Applet from JavaScript. I think this will be the easiest way to use my existing Java program with minor modifications.


You don't need to have JSP, one way of achieving this is writing a HTTP Servlet, drop it into a servlet container such as Tomcat, and have your PHP do a POST to the Servlet.

This opens a whole Java EE paradigm. ( :

Otherwise, you may check if PHP allows you to run command line batchs, which runs your Java program, then in your Java program output the results of the call to a text file or database, then have your PHP program read it.

No, 700k (600kb?) characters is not too much for a "command line", i have seen batch jobs running on way more megabytes of data.


Although I'd favour the solution with a Servlet there is a relatively easy way to communicate streaming data between different programmming languages. In your case, you could open a TCP socket in Java in listening mode and connect to it from PHP and then forward the data from PHP to Java. There is socket support built-in in PHP (cf. socket_write) as well as in Java (Socket). Have a look at the official tutorial for an introduction in Java socket programming.


DWR can be used to send JSON object to java program .

Ajax for Java developers: Ajax with Direct Web Remoting

0

精彩评论

暂无评论...
验证码 换一张
取 消