从 Java - Tomcat websocket 编辑 .jsp 文件
Edit .jsp file from Java - Tomcat websocket
经过大量努力,我设法使客户端与 tomcat 服务器通信。我终于在服务器 handleMessage
中收到了消息。我现在必须更新 index.jsp
文件的内容以添加收到的消息。
这是我的服务器java class:
package network.server;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@ApplicationScoped
@ServerEndpoint("/status")
public class WebSocket {
private Set<Session> sessions = new HashSet<>();
private Session session;
@OnOpen
public void onOpen(Session session){
System.out.println("Session opened in WebSocket ");
this.session = session;
sessions.add(session);
}
@OnMessage
public void handleMessage(String message) {
if (session.isOpen() && session != null){
try {
System.out.println("Received this message in the server: " + message);
//TODO edit jsp
session.getBasicRemote().sendText("This is a totally unnecessary answer from the server.");
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("Session is not opened or null");
}
}
@OnClose
public void close(Session session) {
System.out.println("Session closed ==>");
sessions.remove(session);
}
@OnError
public void onError(Throwable e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
默认的index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>My title</title>
</head>
<body>
Hello world!
</body>
</html>
我怎样才能做到这一点?
想通了。要编辑 jsp,您必须先在 java class 中添加 extends HttpServlet
,然后在其上添加 @WebServlet("/uri")
。这使它成为一个 servlet,您可以使用 doPost
和 doGet
.
的可覆盖函数
完成后,然后在 jsp 文件中,您可以使用 method
标记访问这些方法,例如:
<form action="servlet" method="get" name="form">
<input type="submit" value="View nodes"/>
</form>
操作应匹配您在@WebServlet 中使用的uri
。
或者创建一个你自己的 html,关联到一个 java 脚本文件,该脚本文件有一个 websocket,以便像这样与 Tomcat 对话:
const socket = new WebSocket("ws://localhost:8080/test_war_exploded/status");
socket.send("Hello there");
并从 tomcat 回答(s 是会话):
s.getBasicRemote().sendText("General kenobi");
不是以这种方式使用 jsp 文件,但您也可以通过这种方式编辑网站。
在这里看到 servlet example here and a websocket example。
经过大量努力,我设法使客户端与 tomcat 服务器通信。我终于在服务器 handleMessage
中收到了消息。我现在必须更新 index.jsp
文件的内容以添加收到的消息。
这是我的服务器java class:
package network.server;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@ApplicationScoped
@ServerEndpoint("/status")
public class WebSocket {
private Set<Session> sessions = new HashSet<>();
private Session session;
@OnOpen
public void onOpen(Session session){
System.out.println("Session opened in WebSocket ");
this.session = session;
sessions.add(session);
}
@OnMessage
public void handleMessage(String message) {
if (session.isOpen() && session != null){
try {
System.out.println("Received this message in the server: " + message);
//TODO edit jsp
session.getBasicRemote().sendText("This is a totally unnecessary answer from the server.");
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("Session is not opened or null");
}
}
@OnClose
public void close(Session session) {
System.out.println("Session closed ==>");
sessions.remove(session);
}
@OnError
public void onError(Throwable e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
默认的index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>My title</title>
</head>
<body>
Hello world!
</body>
</html>
我怎样才能做到这一点?
想通了。要编辑 jsp,您必须先在 java class 中添加 extends HttpServlet
,然后在其上添加 @WebServlet("/uri")
。这使它成为一个 servlet,您可以使用 doPost
和 doGet
.
完成后,然后在 jsp 文件中,您可以使用 method
标记访问这些方法,例如:
<form action="servlet" method="get" name="form">
<input type="submit" value="View nodes"/>
</form>
操作应匹配您在@WebServlet 中使用的uri
。
或者创建一个你自己的 html,关联到一个 java 脚本文件,该脚本文件有一个 websocket,以便像这样与 Tomcat 对话:
const socket = new WebSocket("ws://localhost:8080/test_war_exploded/status");
socket.send("Hello there");
并从 tomcat 回答(s 是会话):
s.getBasicRemote().sendText("General kenobi");
不是以这种方式使用 jsp 文件,但您也可以通过这种方式编辑网站。
在这里看到 servlet example here and a websocket example。