get HttpSession|Request from simple java class not servlet class

前端 未结 3 413
醉梦人生
醉梦人生 2020-12-03 19:23

i want session Object not in servlet class but ordinary from we application.

WEB.XML


                 


        
3条回答
  •  感动是毒
    2020-12-03 19:48

    There are multiple ways to do that, but.. don't. Only your web layer should have access to the session. The other layers should only get the parameters from the session that it needs. For example:

    service.doSomeBusinessLogic(
         session.getAttribute("currentUser"), 
         session.getAttribute("foo"));
    

    The options that you have to obtain the request, and from it - the session in a non-servlet class, that is still in the web layer:

    • store the request in a ThreadLocal in a Filter (and clean it afterwards)
    • pass it as argument - either in constructor (if the object is instantiated on each request) or as method argument.

提交回复
热议问题