my dog learned polymorphism
The moose likes JSF and the fly likes How to access jspx file from HTTP servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to access jspx file from HTTP servlet" Watch "How to access jspx file from HTTP servlet" New topic

How to access jspx file from HTTP servlet

soni lal

Joined: Jan 31, 2011
Posts: 29
Hi All,

My application has been developed using following tools:
JSF, JBOSS server, seam framework
Now, there is a requirement to make one Http servlet call which will redirect to one jspx page to end user.

In my servlet I am getting FacesContext instance and trying to get access to jspx file. Please find servlet code below :

public class LocationSummaryServlet extends HttpServlet {
public LocationSummaryServlet() {}

private LocationLookUpServiceImpl getLookUpServiceImpl() {
return (LocationLookUpServiceImpl) Component

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String locationID = request.getParameter("locationID");
String flag = request.getParameter("flag");
out.println("locationID::::"+locationID+" flag :::::"+flag);
FacesContext fc=new LocationFacesContextHelper().getFacesContext(request, response);
System.out.println("::::: facesContext ::::: "+facesContext);
UIViewRoot uiViewRoot= fc.getApplication().getViewHandler().createView(fc, "/location/location.jspx");


import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.*;
public class LocationFacesContextHelper {

protected FacesContext getFacesContext(HttpServletRequest request, HttpServletResponse response) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

facesContext = contextFactory.getFacesContext(request.getSession().getServletContext(), request, response, lifecycle);

// Set using our inner class
// set a new viewRoot, otherwise context.getViewRoot returns null
//UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "/location/location.jspx");
// facesContext.setViewRoot(view);
return facesContext;
private abstract static class InnerFacesContext extends FacesContext {
protected static void setFacesContextAsCurrentInstance(FacesContext facesContext) {


I am getting the facesContext instance but the jspx file is not getting called.... meither I am getting any exception.

I am not getting the problem... I wasted my 3 days to find out the root cause....

Please anyone suggest that is my approach is right to access a jspx file from HTTP servlet ... If yes then why it is not working...

Thanks in advance !!!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

You can aid our watering eyes if you use the "Code" button to wrap tags around your code and XML that will present the text in a more readable style.

The FacesContext ONLY exists for the lifetime of a single JSF request/response cycle it is destroyed and re-created each time. If the request isn't routed through the FacesServlet, the FacesContext doesn't exist.

In order to get a standard non-JSF servlet (or JSP) to work with JSF, the servlet has to forward the request as a JSF URL request. You cannot connect code directly.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: How to access jspx file from HTTP servlet
It's not a secret anymore!