Accessing ServletContext and HttpSession in @OnMessage of a JSR-356 @ServerEndpoint

前端 未结 4 2183
小鲜肉
小鲜肉 2020-12-05 04:29

I need to get the ServletContext from inside a @ServerEndpoint in order to find Spring ApplicationContext and lookup for a Bean.

4条回答
  •  难免孤独
    2020-12-05 05:30

    I tried out BalusC's answer on Tomcat (Versions 7.0.56 and 8.0.14). On both containers, the modifyHandshake's request parameter does not contain a HttpSession (and thus no servletContext). As I needed the servlet context only to access "global" variables (web-application global, that is), I just stored these variables in an ordinary static field of a holder class. This is inelegant, but it worked.

    That ooks like a bug in this specific tomcat versions - has anyone out there also seen this?

提交回复
热议问题