📜  Java Applet-Communication(1)

📅  最后修改于: 2023-12-03 15:01:28.421000             🧑  作者: Mango

Java Applet-Communication

Java applet-communication refers to the process of data sharing and communication between Java applets and other applications or systems. The communication can be bi-directional or uni-directional, and can be achieved through different protocols and APIs.

Bi-Directional Communication

Bi-directional communication allows applets to send and receive data from other applications or systems. Some of the common protocols used for bi-directional communication are:

Java Remote Method Invocation (RMI)

Java RMI provides a way for applets and other Java applications to invoke methods on remote objects. It uses Java serialization to transfer data between the client and server.

interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

public class HelloImpl extends UnicastRemoteObject implements Hello {
    public HelloImpl() throws RemoteException { }

    public String sayHello() throws RemoteException {
        return "Hello world!";
    }
}

// Client
Hello obj = (Hello) Naming.lookup("//localhost/Hello");
String message = obj.sayHello();
Java Message Service (JMS)

Java JMS is a messaging standard that provides a way for applets to communicate with other applications or systems asynchronously. It uses a messaging broker to route messages between the client and server.

Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("QueueName");
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello world!");
producer.send(message);

// Consumer
MessageConsumer consumer = session.createConsumer(destination);
connection.start();
TextMessage receivedMessage = (TextMessage) consumer.receive();
String message = receivedMessage.getText();
Uni-Directional Communication

Uni-directional communication only allows applets to send data to other applications or systems. Some of the common protocols used for uni-directional communication are:

Hypertext Transfer Protocol (HTTP)

Applets can send data to a web server using HTTP POST or GET requests. The server can then process the data and send a response back to the applet.

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("data=Hello world!");
writer.flush();

// Reading response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    response.append(line);
}
String message = response.toString();
JavaScript Object Notation (JSON)

JSON is a lightweight data interchange format that allows applets to send and receive data in a platform-independent manner.

// Sending JSON message
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("{\"data\":\"Hello world!\"}");
writer.flush();

// Receiving JSON response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    response.append(line);
}
JsonObject obj = new JsonParser().parse(response.toString()).getAsJsonObject();
String message = obj.get("message").getAsString();
Conclusion

Java applets can communicate with other applications or systems using different protocols and APIs. Bi-directional communication allows applets to send and receive data, while uni-directional communication only allows applets to send data. The choice of protocol depends on the use case and requirements of the system.