File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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
Author

How to access jspx file from HTTP servlet

soni lal
Greenhorn

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
.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: 16160
    
  21

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to access jspx file from HTTP servlet