在spring3.2.0+hibernate3.3.2+struts2 2.2.3中使用websocket,运行环境是tomcat8.0.36,获取HttpSession

本文介绍了如何在Spring、Hibernate和Struts2的环境中使用WebSocket,并在Tomcat8.0.36上运行,特别强调了在Struts2拦截器下排除WebSocket请求的配置方法,以成功获取HttpSession。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接看如何获取HttpSession对象。


首先必须得写一个继承javax.websocket.server.ServerEndpointConfig.Configurator类的类来获取HttpSession:

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

public class GetHttpSessionConfigurator extends Configurator{

	@Override
	public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
		// TODO Auto-generated method stub
		HttpSession httpSession=(HttpSession) request.getHttpSession();
        sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
	}
}

接着在websocket服务器类中:

注意由于使用了struts2,所以所有的请求都会被拦截,需要在strus.xml加上:<constant name="struts.action.excludePattern" value="/chat/server"></constant>

就可以放过该请求。

</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值