JSF - Access Managed-Bean in a servlet

时间:2023-04-07 15:14:26

When you have to access your Managed Bean in a servlet, it depends on the scope you set for the Bean.

Request-Scope:

HttpServletRequest httpRequest =(HttpServletRequest) request;YourBean bean =(YourBean) request.getAttribute("yourBean");

Session-Scope:

HttpServletRequest httpRequest =(HttpServletRequest) request;HttpSession httpSession = httpRequest.getSession();YourBean bean =(YourBean) httpSession.getAttribute("yourBean");

Application-Scope:

HttpServletRequest httpRequest =(HttpServletRequest) request;HttpSession httpSession = httpRequest.getSession();ServletContext ctx = httpSession.getServletContext();YourBean bean =(YourBean) ctx.getAttribute("yourBean");