耀眼的橘子的gravatar头像
耀眼的橘子 2016-10-19 10:23:49
java WebSocket之获取HttpSession,登录用户的所有信息
package per.zww.web;

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;
/*
 * 获取HttpSession
 * 
 */

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);
    }
    
}

第一步:新建一个GetHttpSessionConfigurator类并继承Configurator类

@ServerEndpoint(value="/socketTest",configurator=GetHttpSessionConfigurator.class)

第二步:然后在@ServerEndpoint注解里面添加configurator属性

@OnOpen
    public void onOpen(Session session,EndpointConfig config) {
        HttpSession httpSession= (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        System.out.println( httpSession.getAttribute("name"));
        sessionMap.put(session.getId(), session);
    }

第三步:在onOpen方法里加入参数EndpointConfig config即可获取HttpSession

 

 

 

转自:http://www.cnblogs.com/zhaoww/p/5119706.html?utm_source=tuicool&utm_medium=referral

非常感谢!


打赏
最近浏览
amwbbso 2023年8月10日
暂无贡献等级
海鹏qqq  LV2 2022年8月6日
lixiaobing 2022年4月27日
暂无贡献等级
crosa_Don  LV18 2022年3月9日
czwzjj 2021年12月8日
暂无贡献等级
zhangzhigang  LV2 2021年11月23日
paynotify  LV1 2021年4月1日
yedeng1  LV1 2021年2月22日
GuPi9599  LV3 2021年1月13日
1792624151 2021年1月2日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友