| Author |
How to access jspx file from HTTP servlet
|
soni lal
Greenhorn
Joined: Jan 31, 2011
Posts: 25
|
|
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
Joined: Jun 25, 2001
Posts: 14456
|
|
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.
|
Customer surveys are for companies who didn't pay proper attention to begin with.
|
 |
 |
|
|
subject: How to access jspx file from HTTP servlet
|
|
|