websocket与struts2冲突
来源:互联网 发布:新手可以做淘宝代销吗 编辑:程序博客网 时间:2024/06/03 14:31
需要在一个老项目中使用websocket。
做了一个websocket的小例子,没有问题。
当把这个例子整合到struts2项目里面的时候,就访问不到这个websocket了。是因为struts2的过滤器把所有的请求都拦截了,导致WebSock无法处理来至/websocket的请求。解决办法:在struts的配置文件中使用excludePattern属性过滤掉不处理的url。
websocket需要javaee-api-7.0.jar。struts2的jar包就不在这里列出,大家自行导入。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div>
<input type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket = new WebSocket('ws://localhost:80/websocket');
webSocket.onerror = function(event) {
onError(event)
};
webSocket.onopen = function(event) {
onOpen(event)
};
webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />'
+ event.data;
}
function onOpen(event) {
document.getElementById('messages').innerHTML = 'Connection established';
}
function onError(event) {
alert(event.data);
}
function start() {
webSocket.send('hello');
return false;
}
</script>
</body>
</html>
WebSocketApp.java
package com.ssh.action;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketApp {
@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException {
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");
// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while (sentMessages < 3) {
Thread.sleep(5000);
session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);
sentMessages++;
}
// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}
@OnOpen
public void onOpen() {
System.out.println("Client connected");
}
@OnClose
public void onClose() {
System.out.println("Connection closed");
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.action.excludePattern" value="/websocket"></constant>
<package name="mypackage" extends="struts-default">
<action name="userAction" class="com.ssh.action.UserAction"
method="save">
</action>
</package>
</struts>
做了一个websocket的小例子,没有问题。
当把这个例子整合到struts2项目里面的时候,就访问不到这个websocket了。是因为struts2的过滤器把所有的请求都拦截了,导致WebSock无法处理来至/websocket的请求。解决办法:在struts的配置文件中使用excludePattern属性过滤掉不处理的url。
websocket需要javaee-api-7.0.jar。struts2的jar包就不在这里列出,大家自行导入。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div>
<input type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket = new WebSocket('ws://localhost:80/websocket');
webSocket.onerror = function(event) {
onError(event)
};
webSocket.onopen = function(event) {
onOpen(event)
};
webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />'
+ event.data;
}
function onOpen(event) {
document.getElementById('messages').innerHTML = 'Connection established';
}
function onError(event) {
alert(event.data);
}
function start() {
webSocket.send('hello');
return false;
}
</script>
</body>
</html>
WebSocketApp.java
package com.ssh.action;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketApp {
@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException {
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");
// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while (sentMessages < 3) {
Thread.sleep(5000);
session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);
sentMessages++;
}
// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}
@OnOpen
public void onOpen() {
System.out.println("Client connected");
}
@OnClose
public void onClose() {
System.out.println("Connection closed");
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.action.excludePattern" value="/websocket"></constant>
<package name="mypackage" extends="struts-default">
<action name="userAction" class="com.ssh.action.UserAction"
method="save">
</action>
</package>
</struts>
阅读全文
0 0
- websocket与struts2冲突
- Struts2与Websocket配置UrlPattern冲突问题
- WebSocket在Struts2框架下发生配置冲突
- jbpm4与struts2整合包冲突错误
- jQuery-standalone与Struts2-jquery-plugin冲突
- javabean属性与struts2冲突的问题
- struts2 ognl 与 jsp2.1 el 的冲突问题
- spring 注解事务代理 与 struts2 chain 的冲突
- Struts2中Action标签与workflow拦截器的冲突。
- Kindeditor编辑器 jsp上传错误解决方法 与struts2冲突整合
- Struts2与Axis2整合配置UrlPattern冲突问题
- Struts2与FCK整合 请求冲突的解决方法
- 关于commons-fileupload与struts2过滤器冲突的解决方法
- Kindeditor编辑器 jsp上传错误解决方法 与struts2冲突整合
- 解决Struts2 与 hibernate 中 javassist 冲突问题
- org.dom4j 2.0.0-RC1 与struts2.3.24冲突解决办法
- Kindeditor编辑器 jsp上传错误解决方法 与struts2冲突整合
- websocket 与 tomcat websocket通信
- ARouter 路由框架
- 使用 Task 简化异步编程
- C++笔记
- Android IPC机制学习笔记(一)
- apache Shiro学习笔记(三)Authorization
- websocket与struts2冲突
- iOS 11开发教程(十三)iOS11应用编辑界面添加视图
- jsp自定义行数 表格填充
- 解决方案:python version 2.7 required,which was not found in
- LogIntercep
- 先知创新大会议题第二弹:你不知道的网络安全黑科技
- maven install 失败
- 滚动的数字
- 基于Django的微信公众号开发(2) -- 在sae添加第三方包