将数据从Java解析为javascript
下面是我的HTML代码、JavaClient和JavaServer代码。我从客户端程序获得用户输入并将其发送到服务器程序。然后,我想修改用户输入并将其解析为HTML代码中的javascript,这是从服务器中的receive&receive1到HTML中的lati&longi 问题:我可以知道我是否可以将数据从java服务器解析到HTML页面将数据从Java解析为javascript,javascript,java,html,Javascript,Java,Html,下面是我的HTML代码、JavaClient和JavaServer代码。我从客户端程序获得用户输入并将其发送到服务器程序。然后,我想修改用户输入并将其解析为HTML代码中的javascript,这是从服务器中的receive&receive1到HTML中的lati&longi 问题:我可以知道我是否可以将数据从java服务器解析到HTML页面 <html> <head> </head> <body> <p id="latitude&
<html>
<head>
</head>
<body>
<p id="latitude"></p>
<p id="longtitude"></p>
<script>
var lati = 1;
var longti = 20;
document.getElementById("latitude").innerHTML = lati;
document.getElementById("longtitude").innerHTML = longti;
</script>
</body>
</html>
Java UDPBaseClient代码:
//Java program to illustrate Client side
//Implementation using DatagramSocket
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Client
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
// Step 1:Create the socket object for
// carrying the data.
DatagramSocket ds = new DatagramSocket();
DatagramSocket ds1 = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
byte buf1[] = null;
// loop while user not enters "bye"
while (true)
{
System.out.println("Latitude:");
String inp = sc.nextLine();
System.out.println("Longtitude:");
String inp1 = sc.nextLine();
// convert the String input into the byte array.
buf = inp.getBytes();
buf1 = inp1.getBytes();
// Step 2 : Create the datagramPacket for sending
// the data.
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1234);
DatagramPacket DpSend1 =
new DatagramPacket(buf1, buf1.length, ip, 1234);
// Step 3 : invoke the send call to actually send
// the data.
ds.send(DpSend);
ds1.send(DpSend1);
// break the loop if user enters "bye"
if (inp.equals("bye"))
break;
}
}
}
我不知道你对你的情况有什么看法。 如果这是一个web应用程序,您可以在服务器端启动一个web服务器,例如Tomcat,并且您可以在浏览器中的JavaScript中通过fetch获得Java输出 如果您只想在浏览器中的文件协议上简单地运行它,Java需要编写一个带有输出的JavaScript文件,如下例所示 data.js
var data=JSON.parse(“”);
html
var-lati=1;
var longti=20;
document.getElementById(“纬度”).innerHTML=data.lati;
document.getElementById(“longtitude”).innerHTML=data.longti;
您是否正在进行套接字编程,而html页面是客户端?我有一个udpbaseclient java程序,它接受用户输入并将其发送到我的udpbaseserver。之后,我希望将udpbaseserver中的数据解析到我的html页面。@Jason Javascript不支持套接字编程(除了flash,在不久的将来主要浏览器不支持flash),你应该为你的HTML页面提供一个http rest API来获取数据。@QiuZhou我可以知道有什么方法可以从我的服务器接收数据吗?这是因为我想用谷歌地图在谷歌地图上获得坐标和绘图。我可以知道如何用Java实现这一点吗?e、 g创建连接以解析data@Jason避免我们走错方向,这样你就可以告诉我们你的感知细节,你现在想如何运行?我想从服务器上获取坐标并在谷歌地图上绘制它API@Jason在您的情况下,在哪里显示谷歌地图?桌面应用程序?网络应用?还是移动应用程序?我正在使用pc浏览器显示我的谷歌地图
//Java program to illustrate Client side
//Implementation using DatagramSocket
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Client
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
// Step 1:Create the socket object for
// carrying the data.
DatagramSocket ds = new DatagramSocket();
DatagramSocket ds1 = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
byte buf1[] = null;
// loop while user not enters "bye"
while (true)
{
System.out.println("Latitude:");
String inp = sc.nextLine();
System.out.println("Longtitude:");
String inp1 = sc.nextLine();
// convert the String input into the byte array.
buf = inp.getBytes();
buf1 = inp1.getBytes();
// Step 2 : Create the datagramPacket for sending
// the data.
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1234);
DatagramPacket DpSend1 =
new DatagramPacket(buf1, buf1.length, ip, 1234);
// Step 3 : invoke the send call to actually send
// the data.
ds.send(DpSend);
ds1.send(DpSend1);
// break the loop if user enters "bye"
if (inp.equals("bye"))
break;
}
}
}
<html>
<head>
</head>
<body>
<p id="latitude"></p>
<p id="longtitude"></p>
<script src="data.js"></script>
<script>
var lati = 1;
var longti = 20;
document.getElementById("latitude").innerHTML = data.lati;
document.getElementById("longtitude").innerHTML = data.longti;
</script>
</body>
</html>