How can I share a variable or object between two or more Servlets?

后端 未结 6 1198
星月不相逢
星月不相逢 2020-11-28 21:32

I would like to know if there is some way to share a variable or an object between two or more Servlets, I mean some \"standard\" way. I suppose that this is not a good prac

6条回答
  •  臣服心动
    2020-11-28 21:49

    Depends on the scope of the intended use of the data.

    If the data is only used on a per-user basis, like user login info, page hit count, etc. use the session object (httpServletRequest.getSession().get/setAttribute(String [,Object]))

    If it is the same data across multiple users (total web page hits, worker threads, etc) use the ServletContext attributes. servlet.getServletCongfig().getServletContext().get/setAttribute(String [,Object])). This will only work within the same war file/web applicaiton. Note that this data is not persisted across restarts either.

提交回复
热议问题