JSF stores application scoped managed beans just in the ServletContext. In servlets, the ServletContext
is just available by the inherited getServletContext() method. You don't need to manually create a whole FacesContext
around it. That's only an unnecessarily expensive task for this purpose.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Bean bean = (Bean) getServletContext().getAttribute("bean");
// ...
}
If it returns null
, then it simply means that JSF hasn't kicked in yet to auto-create the bean for you (i.e. the servlet is called too early). You would then need to create and store it yourself. It will be used by JSF if the managed bean name (the attribute key) is the same.
if (bean == null) {
bean = new Bean();
getServletContext().setAttribute("bean", bean);
}
That said, what's the purpose of this servlet? Aren't you trying to achieve some functional requirement the wrong way?