I’m just getting started on JavaServer Faces and it looks very attractive. However I’d like to use my own servlets in the same web application as JSF.
This might be really obvious, but what are the best practices for integrating JSF with a “normal” servlets-based application? This would include accessing JSF data from the servlets (read and write).
If your servlets are well-written, they should already not have any business logic inside, but just pure request/response controlling/preprocessing/postprocessing logic. The business logic should already be placed in standalone javabean-like domain/model classes. The database logic should already be placed in standalone DAO classes. And so on. You can just reuse them all in JSF.
That said, it may be good to know that JSF (when running on top of Servlet API –the common case) manages request scoped beans as attributes of
HttpServletRequest, the session scoped beans as attributes of theHttpSession, the application scoped beans as attributes ofServletContext. It may also be good to know that all of those request, session and application attributes are accessible byExternalContext#getRequestMap(),#getSessionMap()and#getApplicationMap(). You should now realize that you can just access them the usual way from inside a servlet.In any case, when there is technical need to access the
FacesContextinside aServletor aFilter, then immediately stop coding it and rethink your approach based on the above facts. Shouldn’t it better be done in a new managed bean? Or maybe aPhaseListener?