O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning a JSF View Response to a Non-JSF Request

A less common scenario is that a request is first processed by a regular servlet, but a JSF view is used to generate the response. One example of where this approach is needed is for an application framework that needs to process the request before passing it on to a JSF view.

To let a JSF view generate a response to a non-JSF request, the servlet must create a FacesContext instance, configure it with the view, and ask JSF to render the response. Here’s the code for doing this:

import javax.faces.FactoryFinder; import javax.faces.application.Application; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import javax.faces.context.FacesContextFactory; import javax.faces.lifecycle.LifecycleFactory; import javax.faces.lifecycle.Lifecycle; ... public class MyServet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { // Whatever processing is needed ... LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); FacesContext context = fcFactory.getFacesContext(context, request, response, lifecycle); Application application = context.getApplication( ); ViewHandler ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required