JSF - Access Managed-Bean in a servlet



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

0 Kommentare:

Post a Comment