• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access jspx file from HTTP servlet

 
soni lal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
.getInstance("LocationLookUp");
}

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");
fc.setViewRoot(uiViewRoot);
}

}


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
InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
// 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) {
FacesContext.setCurrentInstance(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
Pie
Posts: 18282
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic