您好,登錄后才能下訂單哦!
JSP(JavaServer Pages)是一種基于服務器的用戶界面組件框架,用于生成動態網頁內容。WebSocket是一種網絡通信協議,它允許在單個TCP連接上進行全雙工通信。JSP與WebSocket通信實踐是指將JSP用于生成網頁內容,同時使用WebSocket實現實時數據交互。
以下是一個簡單的JSP與WebSocket通信實踐示例:
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@ServerEndpoint("/websocket")
public class Server {
@OnOpen
public void onOpen(Session session) {
System.out.println("Client connected: " + session.getId());
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Received message from client: " + message);
try {
session.getBasicRemote().sendText("Message received: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose(Session session) {
System.out.println("Client disconnected: " + session.getId());
}
@OnError
public void onError(Throwable error) {
System.out.println("Error occurred: " + error.getMessage());
}
}
import javax.websocket.*;
import java.io.IOException;
@ClientEndpoint
public class Client {
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected to server");
}
@OnMessage
public void onMessage(String message) {
System.out.println("Received message from server: " + message);
}
@OnClose
public void onClose(Session session) {
System.out.println("Disconnected from server");
}
@OnError
public void onError(Throwable error) {
System.out.println("Error occurred: " + error.getMessage());
}
public static void main(String[] args) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.connectToServer(Client.class, new URI("ws://localhost:8080/websocket"));
Client client = new Client();
client.sendMessage("Hello, WebSocket!");
Thread.sleep(1000);
container.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Communication Example</title>
</head>
<body>
<h1>WebSocket Communication Example</h1>
<input type="text" id="messageInput" placeholder="Type your message here">
<button onclick="sendMessage()">Send</button>
<div id="messages"></div>
<script>
function sendMessage() {
var input = document.getElementById("messageInput");
var message = input.value;
var ws = new WebSocket("ws://localhost:8080/websocket");
ws.send(message);
input.value = "";
}
var ws = new WebSocket("ws://localhost:8080/websocket");
ws.onmessage = function(event) {
var messagesDiv = document.getElementById("messages");
var newMessage = document.createElement("p");
newMessage.textContent = event.data;
messagesDiv.appendChild(newMessage);
};
ws.onclose = function() {
console.log("Disconnected from server");
};
ws.onerror = function(error) {
console.error("Error occurred: " + error);
};
</script>
</body>
</html>
部署WebSocket服務器端類到Java Web應用服務器(如Tomcat),并啟動服務器。
在瀏覽器中打開JSP頁面(index.jsp),輸入消息并點擊發送按鈕,觀察瀏覽器控制臺和服務器端的輸出,可以看到實時通信的實現。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。