Reputation: 3389
I'm working on a JSF (v1.2) application. In my application I need a generic servlet which could serve any resource (PDF, Images, Excel, etc). My idea is to ask the caller to send the required information so that I can find out the correct delegator class using some configurations.
This delegator class will take care of serving the correct resource.
For example this is the request url
http://example.com/servlet?delegatorid=abcd
My Servlet code is something like this.
protected void doGet(HttpServletRequest request, HttpServletResponse response){
String delegatorID=request.getParameter("delegatorid");
//Get the configuration from Configuration table
configuration=getConfiguration(delegatorID);
//invoke the method of the delegator class based on this configuration
Object result=invokeMethod(configuration);
//write the response to the stream
}
My question is what is the best way to do this in a JSF project?
Could you please let me your thoughts on this?
Upvotes: 4
Views: 7482
Reputation: 1108722
The FacesContext
(and ExternalContext
) is just a facade over HttpServletRequest
, HttpServletResponse
, HttpSession
, ServletContext
, etcetara along with some JSF specifics which you don't need at all in a plain vanilla servlet. The ExternalContext#getSessionMap()
is nothing more than an abstract mapping of HttpSession#get/setAttribute()
.
In a plain vanilla servlet, the session is just available by request.getSession()
and the application by getServletContext()
the usual way. See also among others this related question: Get JSF managed bean by name in any Servlet related class.
You can also just refactor code which needs to be shared by JSF and Servlet into an utility method which doesn't have any dependencies on javax.faces.*
nor javax.servlet.*
classes (or at most only javax.servlet.*
) and finally let the callers each pass the necessary information through.
Upvotes: 8